{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "25e6f97a-9403-c15b-64e4-b27162d41674"
      },
      "outputs": [],
      "source": [
        "from __future__ import absolute_import\n",
        "from __future__ import division\n",
        "from __future__ import print_function\n",
        "\n",
        "# Import data\n",
        "from tensorflow.examples.tutorials.mnist import input_data\n",
        "\n",
        "import tensorflow as tf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "360630fa-2a72-31e9-d4a7-f984395813fc"
      },
      "outputs": [],
      "source": [
        "flags = tf.app.flags\n",
        "FLAGS = flags.FLAGS\n",
        "flags.DEFINE_string('data_dir', '/tmp/data/', 'Directory for storing data')\n",
        "\n",
        "mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)\n",
        "\n",
        "sess = tf.InteractiveSession()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "904e0efe-14dc-23bf-1aa1-fac5898e92d9"
      },
      "outputs": [],
      "source": [
        "# Create the model\n",
        "HIDDEN_1 = 25\n",
        "HIDDEN_2 = 25\n",
        "\n",
        "x = tf.placeholder(tf.float32, [None, 784])\n",
        "\n",
        "W0 = tf.Variable(tf.zeros([784, HIDDEN_1]))\n",
        "b0 = tf.Variable(tf.zeros([HIDDEN_1]))\n",
        "\n",
        "W1 = tf.Variable(tf.zeros([HIDDEN_1, HIDDEN_2]))\n",
        "b1 = tf.Variable(tf.zeros([HIDDEN_2]))\n",
        "\n",
        "W2 = tf.Variable(tf.zeros([HIDDEN_2, 10]))\n",
        "b2 = tf.Variable(tf.zeros([10]))\n",
        "\n",
        "h1 = tf.sigmoid(tf.matmul(x, W0) + b0)\n",
        "h2 = tf.sigmoid(tf.matmul(h1, W1) + b1)\n",
        "y = tf.nn.softmax(tf.matmul(h2, W2) + b2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9dd2a398-d3bd-7a3b-7c7d-69eeb87a2997"
      },
      "outputs": [],
      "source": [
        "# Define loss and optimizer\n",
        "y_ = tf.placeholder(tf.float32, [None, 10])\n",
        "cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.clip_by_value(y,1e-10,1.0)), reduction_indices=[1]))\n",
        "train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "43c01ac2-b03b-cc0d-e441-f1c229ff8faf"
      },
      "outputs": [],
      "source": [
        "# Train\n",
        "tf.initialize_all_variables().run()\n",
        "for i in range(1000):\n",
        "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
        "    train_step.run({x: batch_xs, y_: batch_ys})"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "29468fc8-39c0-f2ac-56cb-c3925092a06e"
      },
      "outputs": [],
      "source": [
        "# Test trained model\n",
        "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(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels})*100,'%')"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "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.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}