{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "import pandas as pd\r\n\r\n# The competition datafiles are in the directory ../input\r\n# Read competition data files:\r\ntrain = pd.read_csv(\"../input/train.csv\")\r\ntest  = pd.read_csv(\"../input/test.csv\")\r\n\r\n# Write to the log:\r\nprint(\"Training set has {0[0]} rows and {0[1]} columns\".format(train.shape))\r\nprint(\"Test set has {0[0]} rows and {0[1]} columns\".format(test.shape))\r\n# Any files you write to the current directory get shown as outputs"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "import tensorflow as tf\nimport numpy as np\nimport matplotlib.pyplot as plt\n% matplotlib inline"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train_labels = train.iloc[:, 0].values\ntrain_images = train.iloc[:, 1:].values\ntrain_images = np.multiply(train_images, 1./255)\n\ntest_images = test.values\ntest_images = np.multiply(test_images, 1./255)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "n_input = 784\nn_class = 10\nn_hidden1 = 256\nn_hidden2 = 256"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "x = tf.placeholder(\"float\", shape=[None, n_input]) \ny = tf.placeholder(\"float\", shape=[None, n_class])"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "def neural_network(_X, _weights, _biases):\n    \n    layer1 = tf.nn.relu(tf.add(tf.matmul(_X, _weights['w1']), _biases['b1']))\n    \n    layer2 = tf.nn.relu(tf.add(tf.matmul(layer1, _weights['w2']), _biases['b2']))\n    \n    out = tf.add(tf.matmul(layer2, _weights['out']), _biases['out'])\n    \n    return out"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "weights = {\n    'w1': tf.Variable(tf.zeros(shape=[n_input, n_hidden1])),\n    'w2': tf.Variable(tf.zeros(shape=[n_hidden1, n_hidden2])),\n    'out': tf.Variable(tf.zeros(shape=[n_hidden2, n_class]))\n}\n\nbiases = {\n    'b1': tf.Variable(tf.zeros(shape=[n_hidden1])),\n    'b2': tf.Variable(tf.zeros(shape=[n_hidden2])),\n    'out': tf.Variable(tf.zeros(shape=[n_class]))\n}"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "nn_2_layers = neural_network(x, weights, biases)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "cost = tf.nn.softmax_cross_entropy_with_logits(nn_2_layers, y)\ntrain = tf.train.AdamOptimizer(0.001).minimize(cost)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "def dense_to_one_hot(labels_dense, num_classes=10):\n    num_labels = labels_dense.shape[0]\n    index_offset = np.arange(num_labels) * num_classes\n    labels_one_hot = np.zeros((num_labels, num_classes))\n    labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1\n    return labels_one_hot"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train_labels = dense_to_one_hot(train_labels)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "init = tf.initialize_all_variables()\ncorrect_prediction = tf.equal(tf.argmax(nn_2_layers,1), tf.argmax(y,1))\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "with tf.Session() as sess:\n    sess.run(init)\n    step = 0\n    while step * 100 < 42000:\n        batch_x = train_images[step*100:(step+1)*100]\n        batch_y = train_labels[step*100:(step+1)*100]\n        cost = sess.run(train, feed_dict={x: batch_x, y: batch_y})\n        if step % 10 == 0:\n            print(\"Iter:\", step*100, \"Accuracy:\", sess.run(accuracy, feed_dict={x: batch_x, y: batch_y}))\n        step += 1"
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}