Created
August 4, 2016 14:03
-
-
Save volnt/162e88d315a31e28ea02a1c7a346d0cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import tensorflow as tf" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\n", | |
| "Extracting MNIST_data/train-images-idx3-ubyte.gz\n", | |
| "Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\n", | |
| "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n", | |
| "Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\n", | |
| "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n", | |
| "Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\n", | |
| "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Load data\n", | |
| "from tensorflow.examples.tutorials import mnist\n", | |
| "mnist = mnist.input_data.read_data_sets(\"MNIST_data/\", one_hot=True)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Declare variables\n", | |
| "x = tf.placeholder(tf.float32, [None, 784])\n", | |
| "W = tf.Variable(tf.zeros([784, 10]))\n", | |
| "b = tf.Variable(tf.zeros([10]))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Define model\n", | |
| "y = tf.nn.softmax(tf.matmul(x, W) + b)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Define the cost function\n", | |
| "y_ = tf.placeholder(tf.float32, [None, 10])\n", | |
| "cross_entropy = -tf.reduce_sum(y_ * tf.log(y))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Define the optimization function\n", | |
| "train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Initialize session\n", | |
| "init = tf.initialize_all_variables()\n", | |
| "sess = tf.Session()\n", | |
| "sess.run(init)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Run the training\n", | |
| "for _ in xrange(1000):\n", | |
| " xs, ys = mnist.train.next_batch(100)\n", | |
| " sess.run(train_step, feed_dict={x: xs, y_: ys})" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0.9144\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Compare results with test results\n", | |
| "correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n", | |
| "accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n", | |
| "print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.6" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment