{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "'''\n", "Graph and Loss visualization using Tensorboard.\n", "This example is using the MNIST database of handwritten digits\n", "(http://yann.lecun.com/exdb/mnist/)\n", "\n", "Author: Aymeric Damien\n", "Project: https://github.com/aymericdamien/TensorFlow-Examples/\n", "'''" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting /tmp/data/train-images-idx3-ubyte.gz\n", "Extracting /tmp/data/train-labels-idx1-ubyte.gz\n", "Extracting /tmp/data/t10k-images-idx3-ubyte.gz\n", "Extracting /tmp/data/t10k-labels-idx1-ubyte.gz\n" ] } ], "source": [ "import tensorflow as tf\n", "\n", "# Import MINST data\n", "from tensorflow.examples.tutorials.mnist import input_data\n", "mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Parameters\n", "learning_rate = 0.01\n", "training_epochs = 25\n", "batch_size = 100\n", "display_step = 1\n", "logs_path = '/tmp/tensorflow_logs'\n", "\n", "# tf Graph Input\n", "# mnist data image of shape 28*28=784\n", "x = tf.placeholder(tf.float32, [None, 784], name='InputData')\n", "# 0-9 digits recognition => 10 classes\n", "y = tf.placeholder(tf.float32, [None, 10], name='LabelData')\n", "\n", "# Set model weights\n", "W = tf.Variable(tf.zeros([784, 10]), name='Weights')\n", "b = tf.Variable(tf.zeros([10]), name='Bias')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Construct model and encapsulating all ops into scopes, making\n", "# Tensorboard's Graph visualization more convenient\n", "with tf.name_scope('Model'):\n", " # Model\n", " pred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax\n", "with tf.name_scope('Loss'):\n", " # Minimize error using cross entropy\n", " cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))\n", "with tf.name_scope('SGD'):\n", " # Gradient Descent\n", " optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)\n", "with tf.name_scope('Accuracy'):\n", " # Accuracy\n", " acc = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n", " acc = tf.reduce_mean(tf.cast(acc, tf.float32))\n", "\n", "# Initializing the variables\n", "init = tf.initialize_all_variables()\n", "\n", "# Create a summary to monitor cost tensor\n", "tf.scalar_summary(\"loss\", cost)\n", "# Create a summary to monitor accuracy tensor\n", "tf.scalar_summary(\"accuracy\", acc)\n", "# Merge all summaries into a single op\n", "merged_summary_op = tf.merge_all_summaries()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch: 0001 cost= 1.182138957\n", "Epoch: 0002 cost= 0.664735104\n", "Epoch: 0003 cost= 0.552622685\n", "Epoch: 0004 cost= 0.498596912\n", "Epoch: 0005 cost= 0.465510372\n", "Epoch: 0006 cost= 0.442504281\n", "Epoch: 0007 cost= 0.425473650\n", "Epoch: 0008 cost= 0.412175615\n", "Epoch: 0009 cost= 0.401374554\n", "Epoch: 0010 cost= 0.392403109\n", "Epoch: 0011 cost= 0.384748503\n", "Epoch: 0012 cost= 0.378154479\n", "Epoch: 0013 cost= 0.372405099\n", "Epoch: 0014 cost= 0.367272844\n", "Epoch: 0015 cost= 0.362745077\n", "Epoch: 0016 cost= 0.358575674\n", "Epoch: 0017 cost= 0.354862829\n", "Epoch: 0018 cost= 0.351437834\n", "Epoch: 0019 cost= 0.348300697\n", "Epoch: 0020 cost= 0.345401101\n", "Epoch: 0021 cost= 0.342762216\n", "Epoch: 0022 cost= 0.340199728\n", "Epoch: 0023 cost= 0.337916089\n", "Epoch: 0024 cost= 0.335764083\n", "Epoch: 0025 cost= 0.333645939\n", "Optimization Finished!\n", "Accuracy: 0.9143\n", "Run the command line:\n", "--> tensorboard --logdir=/tmp/tensorflow_logs \n", "Then open http://0.0.0.0:6006/ into your web browser\n" ] } ], "source": [ "# Launch the graph\n", "with tf.Session() as sess:\n", " sess.run(init)\n", "\n", " # op to write logs to Tensorboard\n", " summary_writer = tf.train.SummaryWriter(logs_path)\n", "\n", " # Training cycle\n", " for epoch in range(training_epochs):\n", " avg_cost = 0.\n", " total_batch = int(mnist.train.num_examples/batch_size)\n", " # Loop over all batches\n", " for i in range(total_batch):\n", " batch_xs, batch_ys = mnist.train.next_batch(batch_size)\n", " # Run optimization op (backprop), cost op (to get loss value)\n", " # and summary nodes\n", " _, c, summary = sess.run([optimizer, cost, merged_summary_op],\n", " feed_dict={x: batch_xs, y: batch_ys})\n", " # Write logs at every iteration\n", " summary_writer.add_summary(summary, epoch * total_batch + i)\n", " # Compute average loss\n", " avg_cost += c / total_batch\n", " # Display logs per epoch step\n", " if (epoch+1) % display_step == 0:\n", " print \"Epoch:\", '%04d' % (epoch+1), \"cost=\", \"{:.9f}\".format(avg_cost)\n", "\n", " print \"Optimization Finished!\"\n", "\n", " # Test model\n", " # Calculate accuracy\n", " print \"Accuracy:\", acc.eval({x: mnist.test.images, y: mnist.test.labels})\n", "\n", " print \"Run the command line:\\n\" \\\n", " \"--> tensorboard --logdir=/tmp/tensorflow_logs \" \\\n", " \"\\nThen open http://0.0.0.0:6006/ into your web browser\"" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2.0 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }