{"cells": [{"cell_type": "markdown", "metadata": {"_cell_guid": "654456b6-e648-0379-0d66-1cc97af6d00d", "_uuid": "475e5ffdc620d156b80836be86fecd43a9207a3e"}, "source": ["**Import all required libraries**\n", "==============================="]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "e5b02688-c589-5a89-e11c-837c6a99eb6e", "_uuid": "dd7a86ac56d5aabe8d4e5d3c126b2a36b671270a"}, "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", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "\n", "from keras.models import Sequential\n", "from keras.layers import Dense , Dropout , Lambda, Flatten\n", "from keras.optimizers import Adam ,RMSprop\n", "from sklearn.model_selection import train_test_split\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": "markdown", "metadata": {"_cell_guid": "22a7fd70-ab61-432d-24cb-93e558414495", "_uuid": "e43ebe42f6469d9f70a3d82711cd6f9d199226dd"}, "source": ["**Load Train and Test data**\n", "============================"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "05226b08-226a-1a00-044d-a0e6b2101388", "_uuid": "c7fc96c3b774cec24501752d4167ecedb303573b"}, "source": ["# create the training & test sets, skipping the header row with [1:]\n", "train = pd.read_csv(\"../input/train.csv\")\n", "print(train.shape)\n", "train.head()"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "2ec570a6-b41a-2139-5e0e-4941c4f0a9d0", "_uuid": "c7ab738f900dc71ca05974de119062a3065e53a4"}, "source": ["test= pd.read_csv(\"../input/test.csv\")\n", "print(test.shape)\n", "test.head()"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "1ae10fe0-dde9-7659-f53d-1a1bd625cfb1", "_uuid": "424453ad7888e016e8c9f220d8f6db346a492896"}, "source": ["X_train = (train.ix[:,1:].values).astype('float32') # all pixel values\n", "y_train = train.ix[:,0].values.astype('int32') # only labels i.e targets digits\n", "X_test = test.values.astype('float32')"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "250b1126-ce1d-6d3f-9736-2504f7a1e098", "_uuid": "955f06520161b071a2949fc0b36c4cf818ea53b6"}, "source": ["X_train"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "e0f15f8a-ac08-540a-58db-dab989cc687c", "_uuid": "d77928497a123105b18cf5af181f077bfe2a5652"}, "source": ["y_train"]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "c2c91588-5547-353a-7f92-39600027438e", "_uuid": "e820b4f3f199f40ce3e00802cee2b110d7d11055"}, "source": ["The output variable is an integer from 0 to 9. This is a **multiclass** classification problem."]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "60957d82-c76f-4822-28ff-def7011a34fa", "_uuid": "7cb41aca48beedd453a16d91a2e183aa8ba83020"}, "source": ["Lets look at 3 images from data set with their labels."]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "1541678d-a08b-d2b2-1e1e-eabf882baaec", "_uuid": "95b23a316b57f7b90c17b094dd32bdc20681705f"}, "source": ["#Convert train datset to (num_images, img_rows, img_cols) format \n", "X_train = X_train.reshape(X_train.shape[0], 28, 28)\n", "\n", "for i in range(6, 9):\n", "    plt.subplot(330 + (i+1))\n", "    plt.imshow(X_train[i], cmap=plt.get_cmap('gray'))\n", "    plt.title(y_train[i]);"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "6be2f3e9-42eb-85b6-9162-c25e4d706155", "_uuid": "26118a952ae820fe9e240a186e47f1d4ce93e5d6"}, "source": ["#expand 1 more dimention as 1 for colour channel gray\n", "X_train = X_train.reshape(X_train.shape[0], 28, 28,1)\n", "X_train.shape"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "6949468c-fd27-19c5-15c7-0b357a961003", "_uuid": "aaa6425f6d71192ab5cfdd7fa36061506be5b4d5"}, "source": ["X_test = X_test.reshape(X_test.shape[0], 28, 28,1)\n", "X_test.shape"]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "1232c385-3cb2-56fd-4d1d-f027df7bc78e", "_uuid": "087f31041f8added53feb0cf8e0e0f082abb8201"}, "source": ["**Preprocessing the digit images**\n", "=================================="]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "6fcc1f9e-1586-e393-49ba-50c73564e0ed", "_uuid": "762e0c65d77fe3f50d35fdc4a2750c893fb6adde"}, "source": ["**Feature Standardization**\n", "-------------------------------------\n", "\n", "It is important preprocessing step.\n", "It is used to centre the data around zero mean and unit variance."]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "a3f837ef-0373-8d91-46e6-30992cf73166", "_uuid": "4beb6143b66d9df39c4d51bf320400378e9c308a"}, "source": ["mean_px = X_train.mean().astype(np.float32)\n", "std_px = X_train.std().astype(np.float32)\n", "\n", "def standardize(x): \n", "    return (x-mean_px)/std_px"]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "725c55fc-9742-a63c-9822-c67ab0c773ee", "_uuid": "252911ce36ffc89f5681185e6b23f4d4d52f2cbb"}, "source": ["*One Hot encoding of labels.*\n", "-----------------------------\n", "\n", "A one-hot vector is a vector which is 0 in most dimensions, and 1 in a single dimension. In this case, the nth digit will be represented as a vector which is 1 in the nth dimension. \n", "\n", "For example, 3 would be [0,0,0,1,0,0,0,0,0,0]."]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "c879f076-b3dd-6cb1-e2d9-2f404f2ed132", "_uuid": "f9cd55e296804ee97d5fc67fdbe1971be0f273bc"}, "source": ["from keras.utils.np_utils import to_categorical\n", "y_train= to_categorical(y_train)\n", "num_classes = y_train.shape[1]\n", "num_classes"]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "4d76fb04-57fc-e802-6d91-06ece552686b", "_uuid": "3d3428dbbe6b32523fe9b719628eb057fae9805c"}, "source": ["Lets plot 10th label."]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "1c927e75-08d2-d539-54f3-71ab0308fec1", "_uuid": "7b1db44f185edf504860baa3b2f036b33d692992"}, "source": ["plt.title(y_train[9])\n", "plt.plot(y_train[9])\n", "plt.xticks(range(10));"]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "4e130661-9f09-d9a9-d49b-7274ef13927f", "_uuid": "cc30b3512f81184c9e941610cbc7c39b37fc87f7"}, "source": ["Oh its 3 !"]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "6a89dcdd-7b68-6ed1-2c39-b3a1edb3e7be", "_uuid": "aba6486b79abe11191f513b63196910bf19d4bf4"}, "source": ["**Designing Neural Network Architecture**\n", "========================================="]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "39107235-d87a-af4d-44fb-80c9c3aa0212", "_uuid": "013abae41892fef5e03f510be1865849de8c0faa"}, "source": ["# fix random seed for reproducibility\n", "seed = 43\n", "np.random.seed(seed)"]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "a8b65f54-398b-267f-e31a-313210450f54", "_uuid": "fed205d10337b5ee07125e2517fba49f8ee4a7dd"}, "source": ["*Linear Model*\n", "--------------"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "5dbe450c-845f-aaa2-dbde-21414a91d8c1", "_uuid": "6f438fc74d809e248c4ad2c6a2cbcfd2f14022ee"}, "source": ["from keras.models import  Sequential\n", "from keras.layers.core import  Lambda , Dense, Flatten, Dropout\n", "from keras.callbacks import EarlyStopping\n", "from keras.layers import BatchNormalization, Convolution2D , MaxPooling2D"]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "5c3f674f-f3fc-9614-f2d4-056c3e3ad633", "_uuid": "70c4a029e08ee7e047852b69b8c1d0a6652a88d3"}, "source": ["Lets create a simple model from Keras Sequential layer.\n", "\n", "1. Lambda layer performs simple arithmetic operations like sum, average, exponentiation etc.\n", "\n", " In 1st layer of the model we have to define input dimensions of our data in (rows,columns,colour channel) format.\n", " (In theano colour channel comes first)\n", "\n", "\n", "2. Flatten will transform input into 1D array.\n", "\n", "\n", "3. Dense is fully connected layer that means all neurons in previous layers will be connected to all neurons in fully connected layer.\n", " In the last layer we have to specify output dimensions/classes of the model.\n", " Here it's 10, since we have to output 10 different digit labels."]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "a2c27783-3cfa-e907-4749-1e340a513f26", "_uuid": "fcd16fcf17bb841742fe00dd437819fb23185452"}, "source": ["model= Sequential()\n", "model.add(Lambda(standardize,input_shape=(28,28,1)))\n", "model.add(Flatten())\n", "model.add(Dense(10, activation='softmax'))\n", "print(\"input shape \",model.input_shape)\n", "print(\"output shape \",model.output_shape)"]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "260645fb-61b7-68e9-6826-047b97436c14", "_uuid": "ce4a51d06c54d7ac81a62f5fbeb6ce175b3085b9"}, "source": ["***Compile network***\n", "-------------------\n", "\n", "Before making network ready for training we have to make sure to add below things:\n", "\n", " 1.  A loss function: to measure how good the network is\n", "    \n", " 2.  An optimizer: to update network as it sees more data and reduce loss\n", "    value\n", "    \n", " 3.  Metrics: to monitor performance of network"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "9d1d1af9-b2a8-e3b9-6eaf-100d08fe83aa", "_uuid": "70dac03671b776e90ebe5f316566eee9a824af34"}, "source": ["from keras.optimizers import RMSprop\n", "model.compile(optimizer=RMSprop(lr=0.001),\n", " loss='categorical_crossentropy',\n", " metrics=['accuracy'])"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "db3b4be6-4f72-c6cc-65cd-b45978db2462", "_uuid": "5e623106c783f541b76a41dfa468084c57248f62"}, "source": ["from keras.preprocessing import image\n", "gen = image.ImageDataGenerator()"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "9071d720-da50-8530-e9f3-1f0c37aac7ff", "_uuid": "81fa31bc853c08967c8c6363cea5a3b35ca714fc"}, "source": ["from sklearn.model_selection import train_test_split\n", "X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.10, random_state=42)\n", "batches = gen.flow(X_train, y_train, batch_size=64)\n", "val_batches=gen.flow(X_val, y_val, batch_size=64)"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "20e08e2a-a394-bb70-69f1-be0fdab4f9ab", "_uuid": "5ff2d99ba362a9d714c674ec4b9d079f07d66e7f"}, "source": ["history=model.fit_generator(batches, batches.n, nb_epoch=1, \n", "                    validation_data=val_batches, nb_val_samples=val_batches.n)"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "9f344366-c372-0b04-b7e0-860778d4bfd3", "_uuid": "15c41c9bdad98c239cad269a8c9910446f1660ba"}, "source": ["history_dict = history.history\n", "history_dict.keys()"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "df40f5fc-586a-1fae-025e-ee508a8d9b71", "_uuid": "3fbd7c9c9abe8d42f66c04cd45806bc2834bd39f"}, "source": ["import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "loss_values = history_dict['loss']\n", "val_loss_values = history_dict['val_loss']\n", "epochs = range(1, len(loss_values) + 1)\n", "\n", "# \"bo\" is for \"blue dot\"\n", "plt.plot(epochs, loss_values, 'bo')\n", "# b+ is for \"blue crosses\"\n", "plt.plot(epochs, val_loss_values, 'b+')\n", "plt.xlabel('Epochs')\n", "plt.ylabel('Loss')\n", "\n", "plt.show()"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "1ed6b756-00c2-d08c-c596-0ce496ec3d04", "_uuid": "8606563b3b4bcbe79b04fad4ba27b775e1c4ab21"}, "source": ["plt.clf()   # clear figure\n", "acc_values = history_dict['acc']\n", "val_acc_values = history_dict['val_acc']\n", "\n", "plt.plot(epochs, acc_values, 'bo')\n", "plt.plot(epochs, val_acc_values, 'b+')\n", "plt.xlabel('Epochs')\n", "plt.ylabel('Accuracy')\n", "\n", "plt.show()"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "e9c49f42-26af-73dc-9061-90c50c06f16e", "_uuid": "1f69df2b3c617b3be39afc47276968c75aa13b08"}, "source": ["model.optimizer.lr=0.01\n", "gen = image.ImageDataGenerator()\n", "batches = gen.flow(X_train, y_train, batch_size=64)\n", "history=model.fit_generator(batches, batches.n, nb_epoch=1)"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "c2841d54-f3dd-1ee8-a30d-4457dec0a67a", "_uuid": "8394f421dbe5c3833606fa0049d8cda50ec2ff5c"}, "source": ["predictions = model.predict_classes(X_test, verbose=0)\n", "\n", "submissions=pd.DataFrame({\"ImageId\": list(range(1,len(predictions)+1)),\n", "                         \"Label\": predictions})\n", "submissions.to_csv(\"DR.csv\", index=False, header=True)"]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "0e6213b0-fc56-658d-46e3-4a5dcb7148ce", "_uuid": "4983036fa9b39434f01a5c684919ddfd0c22aa88"}, "source": []}], "nbformat_minor": 1, "nbformat": 4, "metadata": {"_is_fork": false, "language_info": {"nbconvert_exporter": "python", "file_extension": ".py", "codemirror_mode": {"name": "ipython", "version": 3}, "mimetype": "text/x-python", "pygments_lexer": "ipython3", "version": "3.6.3", "name": "python"}, "kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}, "_change_revision": 0}}