{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# A logistic regression learning algorithm example using TensorFlow library.\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/" ] }, { "cell_type": "code", "execution_count": 2, "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": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Parameters\n", "learning_rate = 0.01\n", "training_epochs = 25\n", "batch_size = 100\n", "display_step = 1\n", "\n", "# tf Graph Input\n", "x = tf.placeholder(tf.float32, [None, 784]) # mnist data image of shape 28*28=784\n", "y = tf.placeholder(tf.float32, [None, 10]) # 0-9 digits recognition => 10 classes\n", "\n", "# Set model weights\n", "W = tf.Variable(tf.zeros([784, 10]))\n", "b = tf.Variable(tf.zeros([10]))\n", "\n", "# Construct model\n", "pred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax\n", "\n", "# Minimize error using cross entropy\n", "cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))\n", "# Gradient Descent\n", "optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)\n", "\n", "# Initializing the variables\n", "init = tf.initialize_all_variables()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch: 0001 cost= 1.182138961\n", "Epoch: 0002 cost= 0.664670898\n", "Epoch: 0003 cost= 0.552613988\n", "Epoch: 0004 cost= 0.498497931\n", "Epoch: 0005 cost= 0.465418769\n", "Epoch: 0006 cost= 0.442546219\n", "Epoch: 0007 cost= 0.425473814\n", "Epoch: 0008 cost= 0.412171735\n", "Epoch: 0009 cost= 0.401359516\n", "Epoch: 0010 cost= 0.392401536\n", "Epoch: 0011 cost= 0.384750201\n", "Epoch: 0012 cost= 0.378185581\n", "Epoch: 0013 cost= 0.372401533\n", "Epoch: 0014 cost= 0.367302442\n", "Epoch: 0015 cost= 0.362702316\n", "Epoch: 0016 cost= 0.358568827\n", "Epoch: 0017 cost= 0.354882155\n", "Epoch: 0018 cost= 0.351430912\n", "Epoch: 0019 cost= 0.348316068\n", "Epoch: 0020 cost= 0.345392556\n", "Epoch: 0021 cost= 0.342737278\n", "Epoch: 0022 cost= 0.340264994\n", "Epoch: 0023 cost= 0.337890242\n", "Epoch: 0024 cost= 0.335708558\n", "Epoch: 0025 cost= 0.333686476\n", "Optimization Finished!\n", "Accuracy: 0.889667\n" ] } ], "source": [ "# Launch the graph\n", "with tf.Session() as sess:\n", " sess.run(init)\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", " # Fit training using batch data\n", " _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs,\n", " y: batch_ys})\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", " correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n", " # Calculate accuracy for 3000 examples\n", " accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", " print \"Accuracy:\", accuracy.eval({x: mnist.test.images[:3000], y: mnist.test.labels[:3000]})" ] } ], "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 }