{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "cff138ea-73cd-e5d5-b094-4fd7b1317d5d"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4f8196a4-b433-68cf-4b92-c3eb5bbdf813"
      },
      "outputs": [],
      "source": [
        "import json\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "import os\n",
        "import pandas as pd\n",
        "import tensorflow as tf\n",
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ececde47-de1f-c8d9-5443-ef8d5fd723ac"
      },
      "outputs": [],
      "source": [
        "train_data = pd.read_csv('../input/train.csv')\n",
        "test_data = pd.read_csv('../input/test.csv')\n",
        "\n",
        "print(train_data.shape)\n",
        "print(test_data.shape)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "61d44394-a7e7-34a8-cf43-c5b1d0fca334",
        "collapsed": true
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "715c02cc-917b-c165-da46-305f6bc62c74"
      },
      "outputs": [],
      "source": [
        "f, axarr = plt.subplots(10, 10)\n",
        "for row in range(10):\n",
        "    for column in range(10):\n",
        "        entry = train_data[train_data['label']==column].iloc[row].drop('label').as_matrix()\n",
        "        axarr[row, column].imshow(entry.reshape([28, 28]))\n",
        "        axarr[row, column].get_xaxis().set_visible(False)\n",
        "        axarr[row, column].get_yaxis().set_visible(False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "652255c7-749b-ae95-e959-a38da0326896"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "713c8059-97e4-3502-1e23-24f8cc7e4fa6"
      },
      "outputs": [],
      "source": [
        "# input tensor\n",
        "x = tf.placeholder(tf.float32, [None, 784])\n",
        "\n",
        "# weights (w) and biases (s) \n",
        "W = tf.Variable(tf.zeros([784, 10]))\n",
        "b = tf.Variable(tf.zeros([10]))\n",
        "\n",
        "# model output (y)\n",
        "y = tf.nn.softmax(tf.matmul(x, W) + b)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "524a701d-6c13-5ec0-7146-8a98817cc25c",
        "collapsed": true
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a72c174e-d5ed-6696-05c7-a0e2a3a77dd6"
      },
      "outputs": [],
      "source": [
        "# target\n",
        "y_ = tf.placeholder(tf.float32, [None, 10])\n",
        "# cross entropy\n",
        "cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "cbafb893-3ea4-1c80-9b9a-67bc2c599591"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b544a343-db20-e087-adde-f8993f49f594",
        "collapsed": true
      },
      "outputs": [],
      "source": [
        "train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n",
        "correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\n",
        "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "4a1ee75d-d9d9-d759-2f64-188959a7493c"
      },
      "source": [
        "In order to train teh model we first need to initialize all variables. Second we will iterate over many epochs and evaluate the model using a train and test set. Finally, to keep this trained model for further use, we can just save the session."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6ec9b6ad-ff9c-e262-4281-016f2efa831c"
      },
      "outputs": [],
      "source": [
        "train_val_ratio = 0.7\n",
        "train_data_size = len(train_data)\n",
        "train_set = train_data[:int(train_data_size*train_val_ratio)]\n",
        "val_set = train_data[int(train_data_size*train_val_ratio)+1:]\n",
        "\n",
        "init = tf.initialize_all_variables()\n",
        "saver = tf.train.Saver()\n",
        "sess = tf.Session()\n",
        "sess.run(init)\n",
        "\n",
        "train_eval_list = []\n",
        "val_eval_list = []\n",
        "for i in range(1000):\n",
        "    batch = train_set.sample(frac=0.1)\n",
        "    batch_xs = batch.drop('label', axis=1).as_matrix()/255.0\n",
        "    batch_ys = pd.get_dummies(batch['label']).as_matrix()\n",
        "    val_xs = val_set.drop('label', axis=1).as_matrix()/255.0\n",
        "    val_ys = pd.get_dummies(val_set['label']).as_matrix()\n",
        "\n",
        "    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\n",
        " \n",
        "    train_eval = sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys})\n",
        "    val_eval = sess.run(accuracy, feed_dict={x: val_xs, y_: val_ys})\n",
        "    \n",
        "    train_eval_list.append(train_eval)\n",
        "    val_eval_list.append(val_eval)\n",
        "\n",
        "saver.save(sess, \"logistic_regression.ckpt\")\n",
        "sess.close()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "735184ef-844e-2f74-df48-b9a7c7c2022e"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2b828202-8f64-c49c-12b4-7ef7aeaac4f2"
      },
      "outputs": [],
      "source": [
        "plt.plot(train_eval_list, label='Train set')\n",
        "plt.plot(val_eval_list, label='Validation set')\n",
        "plt.xlabel('Epoch')\n",
        "plt.ylabel('Accuracy')\n",
        "plt.legend(loc=4)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "c1b4b8ca-7e9f-c0f8-a463-019d0d598071"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8bf89a0b-7a61-8321-f33a-5eabefc1a98c"
      },
      "outputs": [],
      "source": [
        "saver = tf.train.Saver()\n",
        "with tf.Session() as sess:\n",
        "    saver.restore(sess, \"logistic_regression.ckpt\")\n",
        "    predict = sess.run(y, feed_dict={x: test_data.as_matrix() / 255.0})\n",
        "    pred = [[i + 1, np.argmax(one_hot_list)] for i, one_hot_list in enumerate(predict)]\n",
        "    submission = pd.DataFrame(pred, columns=['ImageId', 'Label'])\n",
        "    submission.to_csv('submission_logreg.csv', index=False)"
      ]
    }
  ],
  "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
}