{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "ecc4a61c-d9e8-1943-0e1a-0d5b23b105d9"
      },
      "source": [
        "Initial Setup"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f80b533e-53c2-d220-f544-58578745eb9d"
      },
      "outputs": [],
      "source": [
        "\n",
        "\n",
        "import numpy as np \n",
        "import pandas as pd \n",
        "import tensorflow as tf\n",
        "import sklearn\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1f60376c-6330-d6f7-7ac7-a5d9606c2369"
      },
      "outputs": [],
      "source": [
        "train = pd.read_csv(\"../input/train.csv\")\n",
        "test = data = pd.read_csv(\"../input/test.csv\")\n",
        "print (train.shape)\n",
        "print (test.shape)\n",
        "train.head(5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e031ecca-b93c-c73a-901e-92d104c0dc39"
      },
      "outputs": [],
      "source": [
        "target = train['label']\n",
        "features = train.drop('label',axis=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0cb16903-a8b5-e647-f80b-39bc78cde8cb"
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline\n",
        "\n",
        "def showImage(index):\n",
        "    label = target[index].argmax(axis=0)\n",
        "    img = features.iloc[index].reshape([28,28])\n",
        "    plt.title(\"Index: {} , Label: {}\".format(index, label))\n",
        "    plt.imshow(img, cmap='gray')\n",
        "    plt.show()\n",
        "    \n",
        "showImage(50)    "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "a1965321-c187-be91-9674-6b07ff3cd59f"
      },
      "source": [
        "Building The Network"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e53d3d78-735c-5b2d-03b7-0a855a2a5be8"
      },
      "outputs": [],
      "source": [
        "#Reshape the image to [28,28]\n",
        "#features = np.array(features)\n",
        "#features = np.reshape(features, [-1,28,28,1])\n",
        "for i in range(len(features)):\n",
        "    features.iloc[i].reshape([28,28])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "288b5a4f-264f-c7f6-e0d8-89f4b44af363"
      },
      "outputs": [],
      "source": [
        "def conv_layer(x, height, width, input_channels, output_channels):\n",
        "    weights = tf.Variable(tf.truncated_normal([height, width, input_channels, output_channels], stddev=0.1))\n",
        "    biases  = tf.Variable(tf.zeros(output_channels))\n",
        "    layer = (tf.nn.conv2d(x, weights, strides = [1,2,2,1], padding = 'SAME') + biases)\n",
        "    layer = tf.nn.relu(layer)\n",
        "    layer = tf.nn.max_pool(layer, ksize = [1,2,2,1], strides = [1,2,2,1], padding = 'SAME')\n",
        "    return layer\n",
        "\n",
        "def fully_connected(x, outputs):\n",
        "    weights = tf.Variable(tf.truncated_normal([x.get_shape().as_list()[1], outputs], stddev=0.1))\n",
        "    biases = tf.Variable(tf.zeros(outputs))\n",
        "    layer = tf.matmul(x, weights) + biases\n",
        "    layer = tf.nn.relu(layer)\n",
        "    return layer\n",
        "\n",
        "final_weights = tf.Variable(tf.truncated_normal([56 ,10], stddev=0.1))\n",
        "final_biases = tf.Variable(tf.ones(10))\n",
        "X = tf.placeholder(tf.float32, [None,28,28,1])\n",
        "y = tf.placeholder(tf.float32, [None,10])\n",
        "keep_prob = tf.placeholder(tf.float32)\n",
        "\n",
        "layer1 = conv_layer(X, 1, 1, 1, 4)\n",
        "layer2 = conv_layer(layer1, 2, 2, 4, 8)\n",
        "final = tf.contrib.layers.flatten(layer1)\n",
        "\n",
        "final1 = fully_connected(final, 28)\n",
        "final1 = tf.nn.dropout(final1, keep_prob)\n",
        "\n",
        "final2 = fully_connected(final1, 56)\n",
        "final2 = tf.nn.dropout(final2, keep_prob)\n",
        "\n",
        "logits = tf.matmul(final2, final_weights) + final_biases\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "ca06e220-de41-3af9-ff57-6b18ec33ac70"
      },
      "source": [
        "Optimization"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3af06269-0945-6e1b-ce6d-bad45d304a38"
      },
      "outputs": [],
      "source": [
        "epochs = 5\n",
        "keep_prob = 0.5\n",
        "\n",
        "# Loss and Optimizer\n",
        "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))\n",
        "optimizer = tf.train.AdamOptimizer(0.05).minimize(cost)\n",
        "\n",
        "# Accuracy\n",
        "correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))\n",
        "accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "d628bec3-1214-79ed-1786-4d2bfea97e98"
      },
      "source": [
        "Training the Model"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fb2845d8-7d09-495a-da21-e51767ba7868"
      },
      "outputs": [],
      "source": [
        "with tf.Session() as sess:\n",
        "    sess.run(tf.global_variables_initializer())\n",
        "    for i in range(epochs):\n",
        "        sess.run(optimizer, feed_dict= {X: features, y: target, keep_prob: keep_prob})\n",
        "        print (\"Loss: {}\".format(session.run(cost, feed_dict = {X: features, y: target})))\n",
        "            \n",
        "        "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "da70458f-c292-da77-fc7d-17a03ca8ca9e"
      },
      "outputs": [],
      "source": ""
    }
  ],
  "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.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}