{"cells":[{"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)\nfrom keras.datasets import fashion_mnist \nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, BatchNormalization\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.optimizers import SGD, Adam\nfrom keras.utils import to_categorical\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":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')\nprint(train.head())\nprint(test.head())\nX_train = (train.iloc[:,1:]).as_matrix()\nprint(X_train.shape)\nY_train = train.iloc[:,0].as_matrix()\nprint(Y_train.shape)\nX_test = test.iloc[:,:].as_matrix()\nprint(X_test.shape)\nprint(X_train.shape[0])\nimg_rows, img_cols = 28, 28\nX_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)\nX_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)\ninput_shape = (img_rows, img_cols, 1)\n\nX_train = X_train.astype('float32')\nX_test = X_test.astype('float32')\nX_train /= 255\nX_test /= 255\n\nY_train = to_categorical((Y_train))\nprint((Y_train.shape))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5fd744234de141b8806479bdc7f81bbc7adc32e9","_cell_guid":"3e377dc9-c7a7-4a79-b50d-53341c543870","trusted":true},"cell_type":"code","source":"img_rows, img_cols = 28, 28\ninput_shape = (img_rows, img_cols, 1)\ndef create_model():\n    model = Sequential()\n    \n    model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))\n    model.add(Conv2D(32, (3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n    \n    model.add(Conv2D(64, (3, 3), activation='relu'))\n    model.add(Conv2D(64, (3, 3), activation='relu'))\n    model.add(BatchNormalization(axis=1, momentum=0.99, epsilon=0.001))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n\n    model.add(Flatten())\n    model.add(Dense(2500, activation='relu'))\n    model.add(Dense(250, activation='relu'))\n    model.add(BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001))\n    model.add(Dropout(0.5))\n    model.add(Dense(10, activation='softmax'))\n    return model","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8829883bc3acd60083d34932dcccee6b50099701","_cell_guid":"dba9647c-e1a6-458f-b476-583a7e0a9b6f","trusted":true},"cell_type":"code","source":"model = create_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1cdf9a52e9094b06a59ffe0fb137e5d9c8b565a4","scrolled":false,"_cell_guid":"069b46b7-fa4e-4547-8b20-f86b29ae0f8e","trusted":true},"cell_type":"code","source":"def train():\n    model = create_model()\n    model.compile(optimizer=Adam(lr=0.003, beta_1=0.9, beta_2=0.999), loss='categorical_crossentropy', metrics=['accuracy'])\n    model.fit(X_train, Y_train, batch_size= 64, epochs=50, verbose=2,shuffle=False, validation_split=0.35)\n    model.save_weights('./mnist.h5')\ntrain()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f70bd3d7f8323b549797b2221fc6c9d0a100262f","_cell_guid":"e9f3612e-c880-4b98-9db4-5501d9fd67f6","trusted":true},"cell_type":"code","source":"model = create_model()\nmodel.load_weights('./mnist.h5')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = model.predict_classes(X_test, verbose=0)\ndf = pd.DataFrame(preds, columns=['Label'])\nid = pd.DataFrame([range(1,len(preds)+1)]).transpose()\n# id = pd.DataFrame(id, columns=['ImageId'])\npp = pd.concat([id, df],axis = 1)\nsub = pd.DataFrame(pp)\nsub.rename(columns={0:'ImageId'})\nsub.to_csv(\"./Submission.csv\")\nprint(sub.head())\nprint(sub.tail())\n","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}