From 703815bca9f787c6c13507c674f0f3b4215311f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Fri, 18 May 2018 21:22:56 +0200 Subject: [PATCH] Use tensorflow_graph_in_jupyter.py in notebooks, fixes #223 --- 09_up_and_running_with_tensorflow.ipynb | 104 ++++-------------- ...uction_to_artificial_neural_networks.ipynb | 43 +------- 11_deep_learning.ipynb | 39 +------ 14_recurrent_neural_networks.ipynb | 39 +------ tensorflow_graph_in_jupyter.py | 2 +- 5 files changed, 30 insertions(+), 197 deletions(-) diff --git a/09_up_and_running_with_tensorflow.ipynb b/09_up_and_running_with_tensorflow.ipynb index c03d8e2..d38fcf4 100644 --- a/09_up_and_running_with_tensorflow.ipynb +++ b/09_up_and_running_with_tensorflow.ipynb @@ -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\"\"%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", - " \n", - " \n", - "
\n", - " \n", - "
\n", - " \"\"\".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))\n", - "\n", - " iframe = \"\"\"\n", - " \n", - " \"\"\".format(code.replace('\"', '"'))\n", - " display(HTML(iframe))" + "from tensorflow_graph_in_jupyter import show_graph" ] }, { diff --git a/10_introduction_to_artificial_neural_networks.ipynb b/10_introduction_to_artificial_neural_networks.ipynb index 5ab7456..ce625c5 100644 --- a/10_introduction_to_artificial_neural_networks.ipynb +++ b/10_introduction_to_artificial_neural_networks.ipynb @@ -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\"\"%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", - " \n", - " \n", - "
\n", - " \n", - "
\n", - " \"\"\".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))\n", - "\n", - " iframe = \"\"\"\n", - " \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": [ diff --git a/11_deep_learning.ipynb b/11_deep_learning.ipynb index 737a451..50d7228 100644 --- a/11_deep_learning.ipynb +++ b/11_deep_learning.ipynb @@ -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\"\"%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", - " \n", - " \n", - "
\n", - " \n", - "
\n", - " \"\"\".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))\n", - "\n", - " iframe = \"\"\"\n", - " \n", - " \"\"\".format(code.replace('\"', '"'))\n", - " display(HTML(iframe))" + "from tensorflow_graph_in_jupyter import show_graph" ] }, { diff --git a/14_recurrent_neural_networks.ipynb b/14_recurrent_neural_networks.ipynb index 267d821..74360cf 100644 --- a/14_recurrent_neural_networks.ipynb +++ b/14_recurrent_neural_networks.ipynb @@ -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\"%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", - " \n", - " \n", - "
\n", - " \n", - "
\n", - " \"\"\".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))\n", - "\n", - " iframe = \"\"\"\n", - " \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": { diff --git a/tensorflow_graph_in_jupyter.py b/tensorflow_graph_in_jupyter.py index 0e1edfd..d7eaf0b 100644 --- a/tensorflow_graph_in_jupyter.py +++ b/tensorflow_graph_in_jupyter.py @@ -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 = ""%size + tensor.tensor_content = b""%size return strip_def def show_graph(graph_def, max_const_size=32):