|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "Copyright 2016 Google Inc. All Rights Reserved.\n", |
| 8 | + "Licensed under the Apache License, Version 2.0 (the \"License\");\n", |
| 9 | + "you may not use this file except in compliance with the License.\n", |
| 10 | + "You may obtain a copy of the License at\n", |
| 11 | + "\n", |
| 12 | + " http://www.apache.org/licenses/LICENSE-2.0\n", |
| 13 | + "\n", |
| 14 | + "Unless required by applicable law or agreed to in writing, software\n", |
| 15 | + "distributed under the License is distributed on an \"AS IS\" BASIS,\n", |
| 16 | + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", |
| 17 | + "See the License for the specific language governing permissions and\n", |
| 18 | + "limitations under the License." |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "This notebook builds a model graph to learn XOR, then trains the model. This version shows how to write summary information, that you can view in Tensorboard.\n", |
| 26 | + "\n", |
| 27 | + "First, do some imports." |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": null, |
| 33 | + "metadata": { |
| 34 | + "collapsed": true |
| 35 | + }, |
| 36 | + "outputs": [], |
| 37 | + "source": [ |
| 38 | + "import argparse\n", |
| 39 | + "import math\n", |
| 40 | + "\n", |
| 41 | + "import numpy as np\n", |
| 42 | + "\n", |
| 43 | + "import tensorflow as tf\n", |
| 44 | + "\n", |
| 45 | + "tf.logging.set_verbosity(tf.logging.INFO)\n" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "markdown", |
| 50 | + "metadata": {}, |
| 51 | + "source": [ |
| 52 | + "Now, we'll define a function to build our model graph.\n", |
| 53 | + "Note the definition of the loss and training ops.\n", |
| 54 | + "\n", |
| 55 | + "Note also all the tf.summary information that is being defined." |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": null, |
| 61 | + "metadata": { |
| 62 | + "collapsed": true |
| 63 | + }, |
| 64 | + "outputs": [], |
| 65 | + "source": [ |
| 66 | + "def make_graph(features, labels, num_hidden=8):\n", |
| 67 | + " hidden_weights = tf.Variable(tf.truncated_normal(\n", |
| 68 | + " [2, num_hidden],\n", |
| 69 | + " stddev=1/math.sqrt(2)\n", |
| 70 | + " ))\n", |
| 71 | + " tf.summary.image('hidden_weights', tf.expand_dims([hidden_weights], -1))\n", |
| 72 | + "\n", |
| 73 | + " # Shape [4, num_hidden]\n", |
| 74 | + " hidden_activations = tf.nn.relu(tf.matmul(features, hidden_weights))\n", |
| 75 | + "\n", |
| 76 | + " output_weights = tf.Variable(tf.truncated_normal(\n", |
| 77 | + " [num_hidden, 1],\n", |
| 78 | + " stddev=1/math.sqrt(num_hidden)\n", |
| 79 | + " ))\n", |
| 80 | + "\n", |
| 81 | + " # Shape [4, 1]\n", |
| 82 | + " logits = tf.matmul(hidden_activations, output_weights)\n", |
| 83 | + "\n", |
| 84 | + " # Shape [4]\n", |
| 85 | + " predictions = tf.sigmoid(tf.squeeze(logits))\n", |
| 86 | + " loss = tf.reduce_mean(tf.square(predictions - tf.to_float(labels)))\n", |
| 87 | + " tf.summary.scalar('loss', loss)\n", |
| 88 | + "\n", |
| 89 | + " accuracy, update_acc = tf.contrib.metrics.streaming_accuracy(\n", |
| 90 | + " predictions > 0.5, labels)\n", |
| 91 | + " tf.summary.scalar('accuracy', accuracy)\n", |
| 92 | + "\n", |
| 93 | + " gs = tf.Variable(0, trainable=False)\n", |
| 94 | + " optimizer = tf.train.GradientDescentOptimizer(0.2)\n", |
| 95 | + "\n", |
| 96 | + " grads_and_vars = optimizer.compute_gradients(loss)\n", |
| 97 | + "\n", |
| 98 | + " gradients = zip(grads_and_vars)[0]\n", |
| 99 | + " tf.summary.histogram('gradients', gradients)\n", |
| 100 | + "\n", |
| 101 | + " train_op = optimizer.apply_gradients(grads_and_vars, global_step=gs)\n", |
| 102 | + "\n", |
| 103 | + " return train_op, loss, gs, update_acc\n" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "markdown", |
| 108 | + "metadata": {}, |
| 109 | + "source": [ |
| 110 | + "Build the graph -- define the placeholders, and call `make_graph()`.\n", |
| 111 | + "Then add an op to init the variables.\n", |
| 112 | + "Note also the tf.summary.merge_all() call to create a `summary_op`." |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": null, |
| 118 | + "metadata": { |
| 119 | + "collapsed": true |
| 120 | + }, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "summaries_every = 10\n", |
| 124 | + "num_steps = 5000\n", |
| 125 | + "output_dir = '/tmp/xor/output'\n", |
| 126 | + "\n", |
| 127 | + "graph = tf.Graph()\n", |
| 128 | + "\n", |
| 129 | + "with graph.as_default():\n", |
| 130 | + " features = tf.placeholder(tf.float32, shape=[4, 2])\n", |
| 131 | + " labels = tf.placeholder(tf.int32, shape=[4])\n", |
| 132 | + "\n", |
| 133 | + " train_op, loss, gs, update_acc = make_graph(features, labels)\n", |
| 134 | + " init = tf.global_variables_initializer()\n", |
| 135 | + " init_local = tf.local_variables_initializer()\n", |
| 136 | + " summary_op = tf.summary.merge_all()\n" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "markdown", |
| 141 | + "metadata": {}, |
| 142 | + "source": [ |
| 143 | + "In a Session, run a training loop using a small input dataset.\n", |
| 144 | + "You can adjust the frequency of summary-writing if you like." |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": null, |
| 150 | + "metadata": { |
| 151 | + "collapsed": false |
| 152 | + }, |
| 153 | + "outputs": [], |
| 154 | + "source": [ |
| 155 | + "writer = tf.summary.FileWriter(output_dir, graph=graph, flush_secs=1)\n", |
| 156 | + "\n", |
| 157 | + "with tf.Session(graph=graph) as sess:\n", |
| 158 | + " init.run()\n", |
| 159 | + " init_local.run()\n", |
| 160 | + " step = 0\n", |
| 161 | + " xy = np.array([\n", |
| 162 | + " [True, False],\n", |
| 163 | + " [True, True],\n", |
| 164 | + " [False, False],\n", |
| 165 | + " [False, True]\n", |
| 166 | + " ], dtype=np.float)\n", |
| 167 | + " y_ = np.array([True, False, False, True], dtype=np.int32)\n", |
| 168 | + " while step < num_steps:\n", |
| 169 | + "\n", |
| 170 | + " _, _, step, loss_value, summaries = sess.run(\n", |
| 171 | + " [train_op, update_acc, gs, loss, summary_op],\n", |
| 172 | + " feed_dict={features: xy, labels: y_}\n", |
| 173 | + " )\n", |
| 174 | + " if step % summaries_every == 0:\n", |
| 175 | + " writer.add_summary(summaries, global_step=step)\n", |
| 176 | + " tf.logging.info('Wrote summaries at step {}'.format(step))" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "markdown", |
| 181 | + "metadata": {}, |
| 182 | + "source": [ |
| 183 | + "To see the results, take a look at the summary information in Tensorboard while the training is running, or after it has finished. Run the following in a shell window, pointing the logdir arg to the output directory.\n", |
| 184 | + "\n", |
| 185 | + "$ tensorboard --logdir=/tmp/xor/output" |
| 186 | + ] |
| 187 | + } |
| 188 | + ], |
| 189 | + "metadata": { |
| 190 | + }, |
| 191 | + "nbformat": 4, |
| 192 | + "nbformat_minor": 2 |
| 193 | +} |
0 commit comments