{"cells":[{"metadata":{"_uuid":"963944f856e1ac2a4b11edfe9731828b12de38c1"},"cell_type":"markdown","source":"# Deep NN with Keras:\n\nI developing a series of kernels for different Deep Learning Models:\n\n* [L-Layered Neural Network from scratch](https://www.kaggle.com/curiousprogrammer/l-layered-neural-network-from-scratch)\n* [TensorFlow NN with Augmentation](https://www.kaggle.com/curiousprogrammer/digit-recognizer-tensorflow-nn-with-augmentation)\n* [Data Augmentation in Python, TF, Keras, Imgaug](https://www.kaggle.com/curiousprogrammer/data-augmentation-in-python-tf-keras-imgaug)\n* Deep NN with Keras -This one\n* CNN with TensorFlow\n* CNN with Keras\n* AutoEncoders with TensorFlow\n* AutoEncoders with Keras\n* GANs with TensorFlow\n* GANs with Keras\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","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\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport keras\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Activation, Dropout\nfrom keras.optimizers import Adam\nfrom 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\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c998366a0d0d5a2fb886b7f4bf2b404da3cf67d0"},"cell_type":"markdown","source":"# Load Data"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"df_train = pd.read_csv('../input/train.csv')\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"67c758f47f85625835c907b609fb19adddb912ed"},"cell_type":"code","source":"# Split the training data to X and Y\nX_train = df_train.iloc[:, 1:]\nY_train = df_train.iloc[:, 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1c69906a9062fc4c58d4f55b94af7146b8b16513"},"cell_type":"code","source":"#Convert input to arrays\nX_train = np.array(X_train)\nY_train = np.array(Y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f5f54ee63983b0e992eb451311b44bd62d8d4fb1"},"cell_type":"code","source":"#Normalize inputs\nX_train = X_train/255.0","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bfe261060692ab1eda40e6b6d8f3c861af1f4905"},"cell_type":"markdown","source":"# Plot images"},{"metadata":{"trusted":true,"_uuid":"7195b9bcf00f60f9f42f180fa5e6d05d7d63c471"},"cell_type":"code","source":"def plot_digits(X, Y):\n    for i in range(20):\n        plt.subplot(5, 4, i+1)\n        plt.tight_layout()\n        plt.imshow(X[i].reshape((28, 28)), cmap='gray')\n        plt.title('Digit:{}'.format(Y[i]))\n        plt.xticks([])\n        plt.yticks([])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ebfe2e4113f0bdb46a2935d733ce627a9667d11"},"cell_type":"code","source":"plot_digits(X_train, Y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9479610199a7f3c89b1ff354d707d2553e5410d4"},"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(8, 8))\nsns.countplot(Y_train)\nax.set_title('Distribution of digits', fontsize=12)\nax.set_xlabel('Digits', fontsize=10)\nax.set_ylabel('Count', fontsize=10)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1614b6a95fd185013adbc62729c99c86b56c2df7"},"cell_type":"markdown","source":"# Split data to dev and validation sets"},{"metadata":{"trusted":true,"_uuid":"808ce6bb40d09ec6567a734ba90fc24426751f92"},"cell_type":"code","source":"X_dev, X_val, Y_dev, Y_val = train_test_split(X_train, Y_train, test_size=0.03, shuffle=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6c713104c62983a35b38be6cb0b2747c7f1e72c9"},"cell_type":"markdown","source":"# Build Model"},{"metadata":{"trusted":true,"_uuid":"38dce9594f5e8045b34b07426e14dfe7f5efeed6"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(300, input_dim=784))\nmodel.add(Activation('relu'))\nmodel.add(Dense(100))\nmodel.add(Activation('relu'))\nmodel.add(Dense(100))\nmodel.add(Activation('relu'))\nmodel.add(Dense(100))\nmodel.add(Activation('relu'))\nmodel.add(Dense(200))\nmodel.add(Activation('relu'))\nmodel.add(Dense(10))\nmodel.add(Activation('softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e17ce302ac6fdf8ce1fb0f3e7f2221bf8db45b8"},"cell_type":"code","source":"model.build()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e19adbff1868d86bbf9e9d9ca2690295187fb1ce"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"80de41a501ddf33bada31e4d4cdaa56812e9d76a"},"cell_type":"code","source":"adam = Adam(lr=5e-4)\nmodel.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1c8408f56fe67164e563ea85a4831f790b358323"},"cell_type":"code","source":"T_dev = keras.utils.to_categorical(Y_dev, num_classes=10)\nT_val = keras.utils.to_categorical(Y_val, num_classes=10)\nmodel.fit(X_dev, T_dev, epochs=20, batch_size=100)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"09422d22c072f225e86f19dc0117e8ea58e20905"},"cell_type":"markdown","source":"# Check Validation score"},{"metadata":{"trusted":true,"_uuid":"d5a7daa79eb27c8e3b9c3bf0e93acecfc0801eef"},"cell_type":"code","source":"score = model.evaluate(X_val, T_val, batch_size=100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"997f3d7791d3621386e87e0b58a7a8e6b610d165"},"cell_type":"code","source":"score","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"790a186a3f4e9193b791237eff3a798675a244f7"},"cell_type":"markdown","source":"# Load Test Data and Predict"},{"metadata":{"trusted":true,"_uuid":"2f37505f48959d950b0f37a206d581c6b3b49d35"},"cell_type":"code","source":"df_test = pd.read_csv('../input/test.csv')\nX_test = np.array(df_test)\nX_test = X_test/255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0709452e3f99a7145aabe40ce90ef7d43206bb11"},"cell_type":"code","source":"T_test = model.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e5ff9b84ab1e3a928758e078aae2f2fef1a4d07"},"cell_type":"code","source":"Y_test = np.argmax(T_test, axis=1)\nY_test[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af0f298424d1a0e4944e2f8f472918ff7e56d98e"},"cell_type":"code","source":"df_sample = pd.read_csv('../input/sample_submission.csv')\ndf_sample['Label'] = Y_test\ndf_sample.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf86fb9c20b2c92f016d8a8aff300dfb97fcc123"},"cell_type":"code","source":"df_sample.to_csv('out.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aedc27111d3517db3b7c40ec956f98861cb8ba3a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}