{"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 keras\nimport numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom keras.datasets import mnist\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, AveragePooling2D\nfrom keras.layers import Dense, Flatten\nfrom keras.losses import categorical_crossentropy\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-03T01:49:55.296701Z","iopub.execute_input":"2021-06-03T01:49:55.297095Z","iopub.status.idle":"2021-06-03T01:49:55.3071Z","shell.execute_reply.started":"2021-06-03T01:49:55.297057Z","shell.execute_reply":"2021-06-03T01:49:55.305681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-03T01:49:58.503855Z","iopub.execute_input":"2021-06-03T01:49:58.504276Z","iopub.status.idle":"2021-06-03T01:50:04.632947Z","shell.execute_reply.started":"2021-06-03T01:49:58.504239Z","shell.execute_reply":"2021-06-03T01:50:04.63175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train.drop(\"label\", axis = 1)\ny_train = train[\"label\"]\nx_test = test\nprint(x_train.shape, y_train.shape, x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T20:48:45.95772Z","iopub.execute_input":"2021-06-02T20:48:45.958092Z","iopub.status.idle":"2021-06-02T20:48:46.048446Z","shell.execute_reply.started":"2021-06-02T20:48:45.95806Z","shell.execute_reply":"2021-06-02T20:48:46.047358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, Y_train, Y_val = train_test_split(x_train, y_train, test_size = 0.2, random_state = 0)\n\nprint('Training data:')\nprint(X_train.shape, Y_train.shape)\n\nprint('\\nValidation data:')\nprint(X_val.shape, Y_val.shape)\n\nprint('\\nTest data:')\nprint(x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T21:08:17.190552Z","iopub.execute_input":"2021-06-02T21:08:17.190903Z","iopub.status.idle":"2021-06-02T21:08:17.439242Z","shell.execute_reply.started":"2021-06-02T21:08:17.190858Z","shell.execute_reply":"2021-06-02T21:08:17.438178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_train).astype(\"float32\")/255\nX_val = np.array(X_val).astype(\"float32\")/255\nX_test = np.array(x_test).astype(\"float32\")/255","metadata":{"execution":{"iopub.status.busy":"2021-06-02T21:08:18.505869Z","iopub.execute_input":"2021-06-02T21:08:18.506244Z","iopub.status.idle":"2021-06-02T21:08:18.794923Z","shell.execute_reply.started":"2021-06-02T21:08:18.506211Z","shell.execute_reply":"2021-06-02T21:08:18.793797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(-1, 28, 28, 1)\nX_val = X_val.reshape(-1, 28, 28, 1)\nX_test = X_test.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T21:08:19.876381Z","iopub.execute_input":"2021-06-02T21:08:19.876758Z","iopub.status.idle":"2021-06-02T21:08:19.88182Z","shell.execute_reply.started":"2021-06-02T21:08:19.876726Z","shell.execute_reply":"2021-06-02T21:08:19.880791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = keras.utils.to_categorical(Y_train, 10)\nY_val = keras.utils.to_categorical(Y_val, 10)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T21:08:22.54851Z","iopub.execute_input":"2021-06-02T21:08:22.548853Z","iopub.status.idle":"2021-06-02T21:08:22.554463Z","shell.execute_reply.started":"2021-06-02T21:08:22.548824Z","shell.execute_reply":"2021-06-02T21:08:22.55307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize = (8, 8))\nfor i in range(9):\n    plt.subplot(330 + 1 + i)\n    plt.imshow(X_train[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-02T20:50:53.698553Z","iopub.execute_input":"2021-06-02T20:50:53.698913Z","iopub.status.idle":"2021-06-02T20:50:54.538254Z","shell.execute_reply.started":"2021-06-02T20:50:53.698884Z","shell.execute_reply":"2021-06-02T20:50:54.537092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of dimensions: ' + str(X_train.ndim))\nprint('Shape of image data: ' + str(X_train.shape[1:]))","metadata":{"execution":{"iopub.status.busy":"2021-06-02T20:52:52.480932Z","iopub.execute_input":"2021-06-02T20:52:52.481369Z","iopub.status.idle":"2021-06-02T20:52:52.487448Z","shell.execute_reply.started":"2021-06-02T20:52:52.481328Z","shell.execute_reply":"2021-06-02T20:52:52.48642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_rows, img_cols, img_channel = X_train.shape[1:]\ninput_shape = (img_rows, img_cols, img_channel)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T20:54:39.237101Z","iopub.execute_input":"2021-06-02T20:54:39.23752Z","iopub.status.idle":"2021-06-02T20:54:39.243269Z","shell.execute_reply.started":"2021-06-02T20:54:39.237483Z","shell.execute_reply":"2021-06-02T20:54:39.24205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lenet = Sequential()\nlenet.add(Conv2D(6, kernel_size=(5,5), activation='tanh', input_shape=input_shape, padding='same', name='C1'))\nlenet.add(AveragePooling2D(pool_size=(2,2), strides=(1,1), padding='valid'))\nlenet.add(Conv2D(16, kernel_size=(5,5), activation='tanh', padding='valid'))\nlenet.add(AveragePooling2D(pool_size=(2,2), strides=(1,1), padding='valid'))\nlenet.add(Conv2D(120, kernel_size=(5,5), activation='tanh', padding='valid', name = 'C5'))\nlenet.add(Flatten())\nlenet.add(Dense(84, activation='tanh', name='FC6'))\nlenet.add(Dense(10, activation='softmax', name='OUT'))","metadata":{"execution":{"iopub.status.busy":"2021-06-02T21:00:03.52744Z","iopub.execute_input":"2021-06-02T21:00:03.527787Z","iopub.status.idle":"2021-06-02T21:00:03.682607Z","shell.execute_reply.started":"2021-06-02T21:00:03.527759Z","shell.execute_reply":"2021-06-02T21:00:03.681528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lenet.compile(loss=categorical_crossentropy, optimizer='SGD', metrics=['accuracy'])\nlenet.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-02T21:00:45.531216Z","iopub.execute_input":"2021-06-02T21:00:45.531645Z","iopub.status.idle":"2021-06-02T21:00:45.729082Z","shell.execute_reply.started":"2021-06-02T21:00:45.531608Z","shell.execute_reply":"2021-06-02T21:00:45.72785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\nepochs = 50\nhistory = lenet.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, validation_data=(X_val, Y_val))","metadata":{"execution":{"iopub.status.busy":"2021-06-02T21:14:03.668423Z","iopub.execute_input":"2021-06-02T21:14:03.668754Z","iopub.status.idle":"2021-06-02T22:09:42.81382Z","shell.execute_reply.started":"2021-06-02T21:14:03.668726Z","shell.execute_reply":"2021-06-02T22:09:42.812851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = lenet.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T22:30:18.319643Z","iopub.execute_input":"2021-06-02T22:30:18.320174Z","iopub.status.idle":"2021-06-02T22:30:37.201884Z","shell.execute_reply.started":"2021-06-02T22:30:18.320127Z","shell.execute_reply":"2021-06-02T22:30:37.201015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_x[3]","metadata":{"execution":{"iopub.status.busy":"2021-06-02T22:30:42.417086Z","iopub.execute_input":"2021-06-02T22:30:42.417441Z","iopub.status.idle":"2021-06-02T22:30:42.423719Z","shell.execute_reply.started":"2021-06-02T22:30:42.417413Z","shell.execute_reply":"2021-06-02T22:30:42.423031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId':test.index.values + 1, 'Label':np.argmax(pred_x, axis=1)})\nsubmission.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T22:30:49.380752Z","iopub.execute_input":"2021-06-02T22:30:49.381228Z","iopub.status.idle":"2021-06-02T22:30:49.387117Z","shell.execute_reply.started":"2021-06-02T22:30:49.381197Z","shell.execute_reply":"2021-06-02T22:30:49.386449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('mnist_submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T22:31:05.805044Z","iopub.execute_input":"2021-06-02T22:31:05.805561Z","iopub.status.idle":"2021-06-02T22:31:05.815861Z","shell.execute_reply.started":"2021-06-02T22:31:05.805514Z","shell.execute_reply":"2021-06-02T22:31:05.814658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-06-02T22:31:17.633531Z","iopub.execute_input":"2021-06-02T22:31:17.63388Z","iopub.status.idle":"2021-06-02T22:31:17.790718Z","shell.execute_reply.started":"2021-06-02T22:31:17.633851Z","shell.execute_reply":"2021-06-02T22:31:17.789585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}