{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Install\n", "=====\n", "Installing tensorflow environment from command line:\n", "1. conda create -n tensorflow python=3.5\n", "2. activate tensorflow\n", "3. install jupyter\n", "\n", "Or use Anaconda Navigator to manage environments from there.\n", "\n", "Tutorial\n", "=====\n", "When one learns how to program, there's a tradition that the first thing you do is print \"Hello World.\" Just like programming has Hello World, machine learning has MNIST.\n", "\n", "MNIST is a simple computer vision dataset. It consists of images of handwritten digits. It also includes labels for each image, telling us which digit it is.\n", "\n", "In this tutorial, we're going to train a model to look at images and predict what digits they are. Our goal isn't to train a really elaborate model that achieves state-of-the-art performance but rather to dip a toe into using TensorFlow. As such, we're going to start with a very simple model, called a Softmax Regression.\n", "\n", "The actual code for this tutorial is very short, and all the interesting stuff happens in just three lines. However, it is very important to understand the ideas behind it: both how TensorFlow works and the core machine learning concepts. \n", "\n", "## Read input and test data" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "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": [ "from tensorflow.examples.tutorials.mnist import input_data\n", "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Implementing the model\n", "\n", "$$ y = softmax(x \\cdot W + B) $$" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import tensorflow as tf\n", "\n", "x = tf.placeholder(tf.float32, [None, 784])\n", "W = tf.Variable(tf.zeros([784, 10]))\n", "b = tf.Variable(tf.zeros([10]))\n", "\n", "y = tf.nn.softmax(tf.matmul(x, W) + b)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Training" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "y_ = tf.placeholder(tf.float32, [None, 10])\n", "cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))\n", "train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n", "\n", "init = tf.global_variables_initializer()\n", "\n", "sess = tf.Session()\n", "sess.run(init)\n", "\n", "for i in range(1000):\n", " batch_xs, batch_ys = mnist.train.next_batch(200)\n", " sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Evaluating the Model" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9183\n" ] } ], "source": [ "correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\n", "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", "print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.3" } }, "nbformat": 4, "nbformat_minor": 2 }