Use tensorflow_graph_in_jupyter.py in notebooks, fixes #223
parent
ef2a7b5bd0
commit
703815bca9
|
@ -114,9 +114,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"sess.close()"
|
"sess.close()"
|
||||||
|
@ -168,9 +166,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 10,
|
"execution_count": 10,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"init = tf.global_variables_initializer()"
|
"init = tf.global_variables_initializer()"
|
||||||
|
@ -191,9 +187,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 12,
|
"execution_count": 12,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"sess.close()"
|
"sess.close()"
|
||||||
|
@ -383,9 +377,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 23,
|
"execution_count": 23,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from sklearn.preprocessing import StandardScaler\n",
|
"from sklearn.preprocessing import StandardScaler\n",
|
||||||
|
@ -491,9 +483,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 28,
|
"execution_count": 28,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"gradients = tf.gradients(mse, [theta])[0]"
|
"gradients = tf.gradients(mse, [theta])[0]"
|
||||||
|
@ -533,9 +523,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 30,
|
"execution_count": 30,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def my_func(a, b):\n",
|
"def my_func(a, b):\n",
|
||||||
|
@ -620,9 +608,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 35,
|
"execution_count": 35,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\n",
|
"optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\n",
|
||||||
|
@ -680,9 +666,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 38,
|
"execution_count": 38,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate,\n",
|
"optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate,\n",
|
||||||
|
@ -692,9 +676,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 39,
|
"execution_count": 39,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"training_op = optimizer.minimize(mse)\n",
|
"training_op = optimizer.minimize(mse)\n",
|
||||||
|
@ -780,9 +762,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 44,
|
"execution_count": 44,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"reset_graph()\n",
|
"reset_graph()\n",
|
||||||
|
@ -794,9 +774,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 45,
|
"execution_count": 45,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name=\"theta\")\n",
|
"theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name=\"theta\")\n",
|
||||||
|
@ -812,9 +790,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 46,
|
"execution_count": 46,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"n_epochs = 10"
|
"n_epochs = 10"
|
||||||
|
@ -823,9 +799,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 47,
|
"execution_count": 47,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"batch_size = 100\n",
|
"batch_size = 100\n",
|
||||||
|
@ -947,9 +921,7 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 54,
|
"execution_count": 54,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"saver = tf.train.Saver({\"weights\": theta})"
|
"saver = tf.train.Saver({\"weights\": theta})"
|
||||||
|
@ -1003,50 +975,20 @@
|
||||||
"## inside Jupyter"
|
"## inside Jupyter"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"To visualize the graph within Jupyter, we will use a TensorBoard server available online at https://tensorboard.appspot.com/ (so this will not work if you do not have Internet access). As far as I can tell, this code was originally written by Alex Mordvintsev in his [DeepDream tutorial](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/deepdream/deepdream.ipynb). Alternatively, you could use a tool like [tfgraphviz](https://github.com/akimach/tfgraphviz)."
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 57,
|
"execution_count": 57,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from IPython.display import clear_output, Image, display, HTML\n",
|
"from tensorflow_graph_in_jupyter import show_graph"
|
||||||
"\n",
|
|
||||||
"def strip_consts(graph_def, max_const_size=32):\n",
|
|
||||||
" \"\"\"Strip large constant values from graph_def.\"\"\"\n",
|
|
||||||
" strip_def = tf.GraphDef()\n",
|
|
||||||
" for n0 in graph_def.node:\n",
|
|
||||||
" n = strip_def.node.add() \n",
|
|
||||||
" n.MergeFrom(n0)\n",
|
|
||||||
" if n.op == 'Const':\n",
|
|
||||||
" tensor = n.attr['value'].tensor\n",
|
|
||||||
" size = len(tensor.tensor_content)\n",
|
|
||||||
" if size > max_const_size:\n",
|
|
||||||
" tensor.tensor_content = b\"<stripped %d bytes>\"%size\n",
|
|
||||||
" return strip_def\n",
|
|
||||||
"\n",
|
|
||||||
"def show_graph(graph_def, max_const_size=32):\n",
|
|
||||||
" \"\"\"Visualize TensorFlow graph.\"\"\"\n",
|
|
||||||
" if hasattr(graph_def, 'as_graph_def'):\n",
|
|
||||||
" graph_def = graph_def.as_graph_def()\n",
|
|
||||||
" strip_def = strip_consts(graph_def, max_const_size=max_const_size)\n",
|
|
||||||
" code = \"\"\"\n",
|
|
||||||
" <script>\n",
|
|
||||||
" function load() {{\n",
|
|
||||||
" document.getElementById(\"{id}\").pbtxt = {data};\n",
|
|
||||||
" }}\n",
|
|
||||||
" </script>\n",
|
|
||||||
" <link rel=\"import\" href=\"https://tensorboard.appspot.com/tf-graph-basic.build.html\" onload=load()>\n",
|
|
||||||
" <div style=\"height:600px\">\n",
|
|
||||||
" <tf-graph-basic id=\"{id}\"></tf-graph-basic>\n",
|
|
||||||
" </div>\n",
|
|
||||||
" \"\"\".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))\n",
|
|
||||||
"\n",
|
|
||||||
" iframe = \"\"\"\n",
|
|
||||||
" <iframe seamless style=\"width:1200px;height:620px;border:0\" srcdoc=\"{}\"></iframe>\n",
|
|
||||||
" \"\"\".format(code.replace('\"', '"'))\n",
|
|
||||||
" display(HTML(iframe))"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -524,44 +524,7 @@
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Works on Chrome, not guaranteed on other browsers\n",
|
"from tensorflow_graph_in_jupyter import show_graph"
|
||||||
"\n",
|
|
||||||
"from IPython.display import clear_output, Image, display, HTML\n",
|
|
||||||
"\n",
|
|
||||||
"def strip_consts(graph_def, max_const_size=32):\n",
|
|
||||||
" \"\"\"Strip large constant values from graph_def.\"\"\"\n",
|
|
||||||
" strip_def = tf.GraphDef()\n",
|
|
||||||
" for n0 in graph_def.node:\n",
|
|
||||||
" n = strip_def.node.add() \n",
|
|
||||||
" n.MergeFrom(n0)\n",
|
|
||||||
" if n.op == 'Const':\n",
|
|
||||||
" tensor = n.attr['value'].tensor\n",
|
|
||||||
" size = len(tensor.tensor_content)\n",
|
|
||||||
" if size > max_const_size:\n",
|
|
||||||
" tensor.tensor_content = b\"<stripped %d bytes>\"%size\n",
|
|
||||||
" return strip_def\n",
|
|
||||||
"\n",
|
|
||||||
"def show_graph(graph_def, max_const_size=32):\n",
|
|
||||||
" \"\"\"Visualize TensorFlow graph.\"\"\"\n",
|
|
||||||
" if hasattr(graph_def, 'as_graph_def'):\n",
|
|
||||||
" graph_def = graph_def.as_graph_def()\n",
|
|
||||||
" strip_def = strip_consts(graph_def, max_const_size=max_const_size)\n",
|
|
||||||
" code = \"\"\"\n",
|
|
||||||
" <script>\n",
|
|
||||||
" function load() {{\n",
|
|
||||||
" document.getElementById(\"{id}\").pbtxt = {data};\n",
|
|
||||||
" }}\n",
|
|
||||||
" </script>\n",
|
|
||||||
" <link rel=\"import\" href=\"https://tensorboard.appspot.com/tf-graph-basic.build.html\" onload=load()>\n",
|
|
||||||
" <div style=\"height:600px\">\n",
|
|
||||||
" <tf-graph-basic id=\"{id}\"></tf-graph-basic>\n",
|
|
||||||
" </div>\n",
|
|
||||||
" \"\"\".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))\n",
|
|
||||||
"\n",
|
|
||||||
" iframe = \"\"\"\n",
|
|
||||||
" <iframe seamless style=\"width:1200px;height:620px;border:0\" srcdoc=\"{}\"></iframe>\n",
|
|
||||||
" \"\"\".format(code.replace('\"', '"'))\n",
|
|
||||||
" display(HTML(iframe))"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -676,7 +639,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 38,
|
"execution_count": 37,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
@ -697,7 +660,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 39,
|
"execution_count": 38,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
|
|
@ -1066,44 +1066,7 @@
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Works on Chrome, not guaranteed on other browsers\n",
|
"from tensorflow_graph_in_jupyter import show_graph"
|
||||||
"\n",
|
|
||||||
"from IPython.display import clear_output, Image, display, HTML\n",
|
|
||||||
"\n",
|
|
||||||
"def strip_consts(graph_def, max_const_size=32):\n",
|
|
||||||
" \"\"\"Strip large constant values from graph_def.\"\"\"\n",
|
|
||||||
" strip_def = tf.GraphDef()\n",
|
|
||||||
" for n0 in graph_def.node:\n",
|
|
||||||
" n = strip_def.node.add() \n",
|
|
||||||
" n.MergeFrom(n0)\n",
|
|
||||||
" if n.op == 'Const':\n",
|
|
||||||
" tensor = n.attr['value'].tensor\n",
|
|
||||||
" size = len(tensor.tensor_content)\n",
|
|
||||||
" if size > max_const_size:\n",
|
|
||||||
" tensor.tensor_content = b\"<stripped %d bytes>\"%size\n",
|
|
||||||
" return strip_def\n",
|
|
||||||
"\n",
|
|
||||||
"def show_graph(graph_def, max_const_size=32):\n",
|
|
||||||
" \"\"\"Visualize TensorFlow graph.\"\"\"\n",
|
|
||||||
" if hasattr(graph_def, 'as_graph_def'):\n",
|
|
||||||
" graph_def = graph_def.as_graph_def()\n",
|
|
||||||
" strip_def = strip_consts(graph_def, max_const_size=max_const_size)\n",
|
|
||||||
" code = \"\"\"\n",
|
|
||||||
" <script>\n",
|
|
||||||
" function load() {{\n",
|
|
||||||
" document.getElementById(\"{id}\").pbtxt = {data};\n",
|
|
||||||
" }}\n",
|
|
||||||
" </script>\n",
|
|
||||||
" <link rel=\"import\" href=\"https://tensorboard.appspot.com/tf-graph-basic.build.html\" onload=load()>\n",
|
|
||||||
" <div style=\"height:600px\">\n",
|
|
||||||
" <tf-graph-basic id=\"{id}\"></tf-graph-basic>\n",
|
|
||||||
" </div>\n",
|
|
||||||
" \"\"\".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))\n",
|
|
||||||
"\n",
|
|
||||||
" iframe = \"\"\"\n",
|
|
||||||
" <iframe seamless style=\"width:1200px;height:620px;border:0\" srcdoc=\"{}\"></iframe>\n",
|
|
||||||
" \"\"\".format(code.replace('\"', '"'))\n",
|
|
||||||
" display(HTML(iframe))"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -236,42 +236,7 @@
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from IPython.display import clear_output, Image, display, HTML\n",
|
"from tensorflow_graph_in_jupyter import show_graph"
|
||||||
"\n",
|
|
||||||
"def strip_consts(graph_def, max_const_size=32):\n",
|
|
||||||
" \"\"\"Strip large constant values from graph_def.\"\"\"\n",
|
|
||||||
" strip_def = tf.GraphDef()\n",
|
|
||||||
" for n0 in graph_def.node:\n",
|
|
||||||
" n = strip_def.node.add() \n",
|
|
||||||
" n.MergeFrom(n0)\n",
|
|
||||||
" if n.op == 'Const':\n",
|
|
||||||
" tensor = n.attr['value'].tensor\n",
|
|
||||||
" size = len(tensor.tensor_content)\n",
|
|
||||||
" if size > max_const_size:\n",
|
|
||||||
" tensor.tensor_content = \"b<stripped %d bytes>\"%size\n",
|
|
||||||
" return strip_def\n",
|
|
||||||
"\n",
|
|
||||||
"def show_graph(graph_def, max_const_size=32):\n",
|
|
||||||
" \"\"\"Visualize TensorFlow graph.\"\"\"\n",
|
|
||||||
" if hasattr(graph_def, 'as_graph_def'):\n",
|
|
||||||
" graph_def = graph_def.as_graph_def()\n",
|
|
||||||
" strip_def = strip_consts(graph_def, max_const_size=max_const_size)\n",
|
|
||||||
" code = \"\"\"\n",
|
|
||||||
" <script>\n",
|
|
||||||
" function load() {{\n",
|
|
||||||
" document.getElementById(\"{id}\").pbtxt = {data};\n",
|
|
||||||
" }}\n",
|
|
||||||
" </script>\n",
|
|
||||||
" <link rel=\"import\" href=\"https://tensorboard.appspot.com/tf-graph-basic.build.html\" onload=load()>\n",
|
|
||||||
" <div style=\"height:600px\">\n",
|
|
||||||
" <tf-graph-basic id=\"{id}\"></tf-graph-basic>\n",
|
|
||||||
" </div>\n",
|
|
||||||
" \"\"\".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))\n",
|
|
||||||
"\n",
|
|
||||||
" iframe = \"\"\"\n",
|
|
||||||
" <iframe seamless style=\"width:1200px;height:620px;border:0\" srcdoc=\"{}\"></iframe>\n",
|
|
||||||
" \"\"\".format(code.replace('\"', '"'))\n",
|
|
||||||
" display(HTML(iframe))"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -2527,7 +2492,7 @@
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.6.2"
|
"version": "3.5.2"
|
||||||
},
|
},
|
||||||
"nav_menu": {},
|
"nav_menu": {},
|
||||||
"toc": {
|
"toc": {
|
||||||
|
|
|
@ -23,7 +23,7 @@ def strip_consts(graph_def, max_const_size=32):
|
||||||
tensor = n.attr['value'].tensor
|
tensor = n.attr['value'].tensor
|
||||||
size = len(tensor.tensor_content)
|
size = len(tensor.tensor_content)
|
||||||
if size > max_const_size:
|
if size > max_const_size:
|
||||||
tensor.tensor_content = "<stripped %d bytes>"%size
|
tensor.tensor_content = b"<stripped %d bytes>"%size
|
||||||
return strip_def
|
return strip_def
|
||||||
|
|
||||||
def show_graph(graph_def, max_const_size=32):
|
def show_graph(graph_def, max_const_size=32):
|
||||||
|
|
Loading…
Reference in New Issue