{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\r\nimport pandas as pd\r\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:49:48.301615Z","iopub.execute_input":"2021-07-25T09:49:48.301968Z","iopub.status.idle":"2021-07-25T09:49:48.305908Z","shell.execute_reply.started":"2021-07-25T09:49:48.301935Z","shell.execute_reply":"2021-07-25T09:49:48.305050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\r\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:49:50.053925Z","iopub.execute_input":"2021-07-25T09:49:50.054337Z","iopub.status.idle":"2021-07-25T09:49:53.967855Z","shell.execute_reply.started":"2021-07-25T09:49:50.054306Z","shell.execute_reply":"2021-07-25T09:49:53.966970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:49:59.110877Z","iopub.execute_input":"2021-07-25T09:49:59.111239Z","iopub.status.idle":"2021-07-25T09:49:59.129285Z","shell.execute_reply.started":"2021-07-25T09:49:59.111210Z","shell.execute_reply":"2021-07-25T09:49:59.128325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.to_numpy().shape","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:50:02.821605Z","iopub.execute_input":"2021-07-25T09:50:02.821944Z","iopub.status.idle":"2021-07-25T09:50:02.827644Z","shell.execute_reply.started":"2021-07-25T09:50:02.821913Z","shell.execute_reply":"2021-07-25T09:50:02.826780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.to_numpy().shape","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:50:04.145569Z","iopub.execute_input":"2021-07-25T09:50:04.145892Z","iopub.status.idle":"2021-07-25T09:50:04.154033Z","shell.execute_reply.started":"2021-07-25T09:50:04.145861Z","shell.execute_reply":"2021-07-25T09:50:04.153020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.max(train.to_numpy()[0])","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:50:05.964285Z","iopub.execute_input":"2021-07-25T09:50:05.964722Z","iopub.status.idle":"2021-07-25T09:50:05.977329Z","shell.execute_reply.started":"2021-07-25T09:50:05.964671Z","shell.execute_reply":"2021-07-25T09:50:05.975426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = train.label.to_numpy()\r\ntrain_image=train.to_numpy()[0:,1:].reshape(42000,28,28,1)\r\ntest_image = test.to_numpy().reshape(28000,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:50:07.606238Z","iopub.execute_input":"2021-07-25T09:50:07.606567Z","iopub.status.idle":"2021-07-25T09:50:07.613527Z","shell.execute_reply.started":"2021-07-25T09:50:07.606536Z","shell.execute_reply":"2021-07-25T09:50:07.612249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = train_image.astype(float) / 255.0\r\ntest_image = test_image.astype(float) / 255.0","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:50:09.108662Z","iopub.execute_input":"2021-07-25T09:50:09.109019Z","iopub.status.idle":"2021-07-25T09:50:09.295615Z","shell.execute_reply.started":"2021-07-25T09:50:09.108970Z","shell.execute_reply":"2021-07-25T09:50:09.294770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = train_image[0]\r\nprint(img.shape)\r\nplt.imshow(img.squeeze())","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:50:10.500624Z","iopub.execute_input":"2021-07-25T09:50:10.500956Z","iopub.status.idle":"2021-07-25T09:50:10.658025Z","shell.execute_reply.started":"2021-07-25T09:50:10.500924Z","shell.execute_reply":"2021-07-25T09:50:10.656784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\r\nfrom tensorflow import keras\r\nfrom keras.layers import InputLayer, Conv2D, MaxPool2D, LeakyReLU, BatchNormalization, Dropout, Flatten, Dense\r\nfrom keras.losses import SparseCategoricalCrossentropy\r\nfrom keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:50:12.571280Z","iopub.execute_input":"2021-07-25T09:50:12.571661Z","iopub.status.idle":"2021-07-25T09:50:12.577065Z","shell.execute_reply.started":"2021-07-25T09:50:12.571627Z","shell.execute_reply":"2021-07-25T09:50:12.575897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential()\r\nmodel.add(InputLayer(input_shape=(28,28,1)))\r\nmodel.add(Conv2D(32,3,padding='same'))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(Conv2D(32,3,padding='same'))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(MaxPool2D())\r\nmodel.add(BatchNormalization())\r\nmodel.add(Dropout(0.2))\r\n\r\nmodel.add(Conv2D(64,3,padding='same'))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(Conv2D(64,3,padding='same'))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(Conv2D(64,3,padding='same'))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(MaxPool2D())\r\nmodel.add(BatchNormalization())\r\nmodel.add(Dropout(0.2))\r\n\r\nmodel.add(Conv2D(128,3,padding='same'))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(Conv2D(128,3,padding='same'))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(Conv2D(128,3,padding='same'))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(MaxPool2D())\r\nmodel.add(BatchNormalization())\r\nmodel.add(Dropout(0.2))\r\n\r\nmodel.add(Conv2D(256,3,padding='same'))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(Conv2D(256,3,padding='same'))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(Conv2D(256,3,padding='same'))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(MaxPool2D())\r\nmodel.add(BatchNormalization())\r\nmodel.add(Dropout(0.2))\r\n\r\nmodel.add(Flatten())\r\n\r\nmodel.add(Dense(1024))\r\nmodel.add(LeakyReLU(0.2))\r\nmodel.add(Dropout(0.2))\r\nmodel.add(BatchNormalization())\r\n\r\n\r\nmodel.add(Dense(10))","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:50:13.992300Z","iopub.execute_input":"2021-07-25T09:50:13.992662Z","iopub.status.idle":"2021-07-25T09:50:16.226176Z","shell.execute_reply.started":"2021-07-25T09:50:13.992629Z","shell.execute_reply":"2021-07-25T09:50:16.225300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(0.002),loss=SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])\r\n","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:50:20.396154Z","iopub.execute_input":"2021-07-25T09:50:20.396500Z","iopub.status.idle":"2021-07-25T09:50:20.415063Z","shell.execute_reply.started":"2021-07-25T09:50:20.396470Z","shell.execute_reply":"2021-07-25T09:50:20.414101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:50:22.916391Z","iopub.execute_input":"2021-07-25T09:50:22.916706Z","iopub.status.idle":"2021-07-25T09:50:22.938186Z","shell.execute_reply.started":"2021-07-25T09:50:22.916677Z","shell.execute_reply":"2021-07-25T09:50:22.937301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_image,train_label,batch_size=64,epochs=20,validation_split=0.2,shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:50:26.232068Z","iopub.execute_input":"2021-07-25T09:50:26.232431Z","iopub.status.idle":"2021-07-25T09:52:15.889696Z","shell.execute_reply.started":"2021-07-25T09:50:26.232402Z","shell.execute_reply":"2021-07-25T09:52:15.888823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='accuracy')\r\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\r\nplt.xlabel('Epoch')\r\nplt.ylabel('Accuracy')\r\nplt.ylim([0, 1])\r\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:52:22.237564Z","iopub.execute_input":"2021-07-25T09:52:22.237903Z","iopub.status.idle":"2021-07-25T09:52:22.426005Z","shell.execute_reply.started":"2021-07-25T09:52:22.237873Z","shell.execute_reply":"2021-07-25T09:52:22.425165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='loss')\r\nplt.plot(history.history['val_loss'], label = 'val_loss')\r\nplt.xlabel('Epoch')\r\nplt.ylabel('Loss')\r\nplt.ylim([0, 1])\r\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:52:25.598958Z","iopub.execute_input":"2021-07-25T09:52:25.599335Z","iopub.status.idle":"2021-07-25T09:52:25.772492Z","shell.execute_reply.started":"2021-07-25T09:52:25.599302Z","shell.execute_reply":"2021-07-25T09:52:25.771351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_image)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:52:29.331428Z","iopub.execute_input":"2021-07-25T09:52:29.331764Z","iopub.status.idle":"2021-07-25T09:52:31.634444Z","shell.execute_reply.started":"2021-07-25T09:52:29.331731Z","shell.execute_reply":"2021-07-25T09:52:31.633562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subs = []\r\nfor p in predictions:\r\n    subs.append(p.argmax())\r\n","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:52:34.728152Z","iopub.execute_input":"2021-07-25T09:52:34.728503Z","iopub.status.idle":"2021-07-25T09:52:34.769016Z","shell.execute_reply.started":"2021-07-25T09:52:34.728472Z","shell.execute_reply":"2021-07-25T09:52:34.768090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId' : range(1,28001), 'Label' : list(subs)})\r\nsubmission.head(10)\r\nsubmission.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:52:36.522354Z","iopub.execute_input":"2021-07-25T09:52:36.522697Z","iopub.status.idle":"2021-07-25T09:52:36.547350Z","shell.execute_reply.started":"2021-07-25T09:52:36.522660Z","shell.execute_reply":"2021-07-25T09:52:36.546253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-25T09:52:38.870486Z","iopub.execute_input":"2021-07-25T09:52:38.870816Z","iopub.status.idle":"2021-07-25T09:52:38.925254Z","shell.execute_reply.started":"2021-07-25T09:52:38.870787Z","shell.execute_reply":"2021-07-25T09:52:38.924256Z"},"trusted":true},"execution_count":null,"outputs":[]}]}