Use tensorflow_graph_in_jupyter.py in notebooks, fixes #223
parent
ef2a7b5bd0
commit
703815bca9
|
@ -114,9 +114,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sess.close()"
|
||||
|
@ -168,9 +166,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"init = tf.global_variables_initializer()"
|
||||
|
@ -191,9 +187,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sess.close()"
|
||||
|
@ -383,9 +377,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from sklearn.preprocessing import StandardScaler\n",
|
||||
|
@ -491,9 +483,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gradients = tf.gradients(mse, [theta])[0]"
|
||||
|
@ -533,9 +523,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def my_func(a, b):\n",
|
||||
|
@ -620,9 +608,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\n",
|
||||
|
@ -680,9 +666,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 38,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate,\n",
|
||||
|
@ -692,9 +676,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 39,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"training_op = optimizer.minimize(mse)\n",
|
||||
|
@ -780,9 +762,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 44,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"reset_graph()\n",
|
||||
|
@ -794,9 +774,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 45,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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",
|
||||
"execution_count": 46,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"n_epochs = 10"
|
||||
|
@ -823,9 +799,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 47,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"batch_size = 100\n",
|
||||
|
@ -947,9 +921,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 54,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"saver = tf.train.Saver({\"weights\": theta})"
|
||||
|
@ -1003,50 +975,20 @@
|
|||
"## 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",
|
||||
"execution_count": 57,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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))"
|
||||
"from tensorflow_graph_in_jupyter import show_graph"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -524,44 +524,7 @@
|
|||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Works on Chrome, not guaranteed on other browsers\n",
|
||||
"\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))"
|
||||
"from tensorflow_graph_in_jupyter import show_graph"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -676,7 +639,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 38,
|
||||
"execution_count": 37,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
|
@ -697,7 +660,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 39,
|
||||
"execution_count": 38,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
|
|
|
@ -1066,44 +1066,7 @@
|
|||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Works on Chrome, not guaranteed on other browsers\n",
|
||||
"\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))"
|
||||
"from tensorflow_graph_in_jupyter import show_graph"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -236,42 +236,7 @@
|
|||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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))"
|
||||
"from tensorflow_graph_in_jupyter import show_graph"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -2527,7 +2492,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.2"
|
||||
"version": "3.5.2"
|
||||
},
|
||||
"nav_menu": {},
|
||||
"toc": {
|
||||
|
|
|
@ -23,7 +23,7 @@ def strip_consts(graph_def, max_const_size=32):
|
|||
tensor = n.attr['value'].tensor
|
||||
size = len(tensor.tensor_content)
|
||||
if size > max_const_size:
|
||||
tensor.tensor_content = "<stripped %d bytes>"%size
|
||||
tensor.tensor_content = b"<stripped %d bytes>"%size
|
||||
return strip_def
|
||||
|
||||
def show_graph(graph_def, max_const_size=32):
|
||||
|
|
Loading…
Reference in New Issue