{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0017650e-5ab6-f2e6-67d3-3319d08cbd41"
      },
      "outputs": [],
      "source": [
        "# This Python 3 environment comes with many helpful analytics libraries installed\n",
        "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n",
        "# For example, here's several helpful packages to load in \n",
        "\n",
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fd2c003b-8071-e2a1-d532-4617d52280f8"
      },
      "outputs": [],
      "source": [
        "from keras.models import Sequential\n",
        "from keras.layers import Dense, Dropout, Flatten, Activation\n",
        "from keras.optimizers import SGD\n",
        "from keras.layers.convolutional import Convolution2D, MaxPooling2D\n",
        "from keras.utils.np_utils import to_categorical"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b45edac3-f30e-849f-ed38-e38240210a9b"
      },
      "outputs": [],
      "source": [
        "train = pd.read_csv(\"../input/train.csv\")\n",
        "\n",
        "test_images = (pd.read_csv(\"../input/test.csv\").values).astype('float32')\n",
        "\n",
        "train_images = (train.ix[:,1:].values).astype('float32')\n",
        "train_labels = train.ix[:,0].values.astype('int32')\n",
        "\n",
        "train_labels = to_categorical(train_labels, num_classes=10)\n",
        "\n",
        "train_images = train_images.reshape([42000, 28, 28])\n",
        "test_images = test_images.reshape([28000, 28, 28])\n",
        "\n",
        "train_images = np.expand_dims(train_images, axis=3)\n",
        "test_images = np.expand_dims(test_images, axis=3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "47338e6a-d68c-394b-94d7-2d689f591c4f"
      },
      "outputs": [],
      "source": [
        "model=Sequential()\n",
        "model.add(Convolution2D(16,(2, 2), input_shape=[28, 28, 1]))\n",
        "model.add(Activation(\"relu\"))\n",
        "model.add(MaxPooling2D())\n",
        "model.add(Convolution2D(64,(2, 2)))\n",
        "model.add(Activation(\"relu\"))\n",
        "model.add(MaxPooling2D())\n",
        "model.add(Flatten())\n",
        "model.add(Dense(64))\n",
        "model.add(Activation(\"relu\"))\n",
        "model.add(Dropout(0.5))\n",
        "model.add(Dense(10))\n",
        "model.add(Activation(\"softmax\"))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "de11b3ee-b0eb-af4d-4a42-e14c7fd6f339"
      },
      "outputs": [],
      "source": [
        "sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\n",
        "model.compile(sgd, \"categorical_crossentropy\", metrics=[\"accuracy\"])\n",
        "model.fit(train_images, train_labels, nb_epoch=10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f03a18df-a8fe-b748-3b97-95e8d2fd76b6"
      },
      "outputs": [],
      "source": [
        "predictions = model.predict(test_images)\n",
        "prediction = np.argmax(predictions, axis=1)"
      ]
    }
  ],
  "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
}