{"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":"# 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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom PIL import Image\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Flatten, Dense, Dropout, BatchNormalization\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D\n\nfrom tensorflow.keras.optimizers import Adam\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-28T13:38:28.673557Z","iopub.execute_input":"2021-05-28T13:38:28.674207Z","iopub.status.idle":"2021-05-28T13:38:32.167450Z","shell.execute_reply.started":"2021-05-28T13:38:28.674164Z","shell.execute_reply":"2021-05-28T13:38:32.166450Z"},"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-05-28T13:38:32.168686Z","iopub.execute_input":"2021-05-28T13:38:32.168957Z","iopub.status.idle":"2021-05-28T13:38:36.812264Z","shell.execute_reply.started":"2021-05-28T13:38:32.168931Z","shell.execute_reply":"2021-05-28T13:38:36.811387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The train shape is {}\".format(train.shape))\nprint(\"The test shape is {}\".format(test.shape))\n","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:36.813806Z","iopub.execute_input":"2021-05-28T13:38:36.814060Z","iopub.status.idle":"2021-05-28T13:38:36.821532Z","shell.execute_reply.started":"2021-05-28T13:38:36.814036Z","shell.execute_reply":"2021-05-28T13:38:36.820712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=train.iloc[:38000,1:].values\ny_train=train.iloc[:38000,0].values\nX_valid=train.iloc[38000:,1:].values\ny_valid=train.iloc[38000:,0].values\n\n\nX_test=test.iloc[:,:].values","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:36.822764Z","iopub.execute_input":"2021-05-28T13:38:36.823046Z","iopub.status.idle":"2021-05-28T13:38:36.835150Z","shell.execute_reply.started":"2021-05-28T13:38:36.823022Z","shell.execute_reply":"2021-05-28T13:38:36.834098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=np.unique(y_train)\nprint(labels)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:36.836507Z","iopub.execute_input":"2021-05-28T13:38:36.836816Z","iopub.status.idle":"2021-05-28T13:38:36.845328Z","shell.execute_reply.started":"2021-05-28T13:38:36.836787Z","shell.execute_reply":"2021-05-28T13:38:36.844540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=X_train.reshape(-1,28,28,1)\nX_test=X_test.reshape(-1,28,28,1)\nX_valid=X_valid.reshape(-1,28,28,1)\nX_train = X_train.astype('float32')\nX_test = X_test.astype('float32')\nX_valid = X_valid.astype('float32')\nprint(X_train.shape)\nprint(X_test.shape)\nprint(X_valid.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:36.846450Z","iopub.execute_input":"2021-05-28T13:38:36.846706Z","iopub.status.idle":"2021-05-28T13:38:36.971832Z","shell.execute_reply.started":"2021-05-28T13:38:36.846680Z","shell.execute_reply":"2021-05-28T13:38:36.970919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline \nimport matplotlib.pyplot as plt\nfig, axes = plt.subplots(10,10, figsize=(8,8))\nfor i,ax in enumerate(axes.flat):\n    ax.imshow(X_train[i])","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:36.972879Z","iopub.execute_input":"2021-05-28T13:38:36.973119Z","iopub.status.idle":"2021-05-28T13:38:43.121149Z","shell.execute_reply.started":"2021-05-28T13:38:36.973094Z","shell.execute_reply":"2021-05-28T13:38:43.120195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[:100]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:43.123698Z","iopub.execute_input":"2021-05-28T13:38:43.124096Z","iopub.status.idle":"2021-05-28T13:38:43.130114Z","shell.execute_reply.started":"2021-05-28T13:38:43.124054Z","shell.execute_reply":"2021-05-28T13:38:43.129270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nbatch_size = 64\nimg_height = X_train.shape[1]\nimg_width = X_train.shape[2]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:43.131928Z","iopub.execute_input":"2021-05-28T13:38:43.132326Z","iopub.status.idle":"2021-05-28T13:38:43.139711Z","shell.execute_reply.started":"2021-05-28T13:38:43.132288Z","shell.execute_reply":"2021-05-28T13:38:43.138942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_norm=X_train/255\nX_test_norm=X_test/255\nX_valid_norm=X_valid/255","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:43.140901Z","iopub.execute_input":"2021-05-28T13:38:43.141280Z","iopub.status.idle":"2021-05-28T13:38:43.235416Z","shell.execute_reply.started":"2021-05-28T13:38:43.141243Z","shell.execute_reply":"2021-05-28T13:38:43.234579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu', input_shape = [28, 28, 1]))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2,2), strides=2, padding='valid'))\nmodel.add(Dropout(0.5))\n\n\nmodel.add(Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2,2), strides=2, padding='valid'))\nmodel.add(Dropout(0.5))\n\nmodel.add(Flatten())\nmodel.add(Dense(units = 128, activation='relu'))\nmodel.add(Dense(units=10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:43.236596Z","iopub.execute_input":"2021-05-28T13:38:43.236974Z","iopub.status.idle":"2021-05-28T13:38:43.396307Z","shell.execute_reply.started":"2021-05-28T13:38:43.236938Z","shell.execute_reply":"2021-05-28T13:38:43.395383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:43.397410Z","iopub.execute_input":"2021-05-28T13:38:43.397670Z","iopub.status.idle":"2021-05-28T13:38:43.406426Z","shell.execute_reply.started":"2021-05-28T13:38:43.397644Z","shell.execute_reply":"2021-05-28T13:38:43.404852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\ncallback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3)\nmodel.compile(optimizer=Adam(lr=0.005), loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(X_train_norm, y_train, batch_size=128, epochs=50, validation_data=(X_valid_norm, y_valid),callbacks=[callback])","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:39:16.803835Z","iopub.execute_input":"2021-05-28T13:39:16.804193Z","iopub.status.idle":"2021-05-28T13:39:27.023812Z","shell.execute_reply.started":"2021-05-28T13:39:16.804160Z","shell.execute_reply":"2021-05-28T13:39:27.021824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(X):\n    plt.imshow(X)\n    X=X.reshape(-1,28,28,1)\n    X=X/255\n    y=model.predict(X)\n    y_class=np.argmax(y)\n    print(y_class)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:43.583785Z","iopub.status.idle":"2021-05-28T13:38:43.584118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict(X_test[5])","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:43.585206Z","iopub.status.idle":"2021-05-28T13:38:43.585537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig, axes = plt.subplots(10,10, figsize=(8,8))\nfor i,ax in enumerate(axes.flat):\n    ax.imshow(X_test[i])\n    ","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:43.586314Z","iopub.status.idle":"2021-05-28T13:38:43.586640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=model.predict(X_test_norm)\ny_class=np.argmax(y,axis=1)","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-05-28T13:38:43.587510Z","iopub.status.idle":"2021-05-28T13:38:43.587828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_class[:100]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:43.588499Z","iopub.status.idle":"2021-05-28T13:38:43.588822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission = pd.DataFrame({'ImageId': np.arange(1,len(y_class)+1), 'Label': y_class})\n# you could use any filename. We choose submission here\nmy_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:43.589543Z","iopub.status.idle":"2021-05-28T13:38:43.589862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.arange(1,len(y_class)+1)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T13:38:43.590855Z","iopub.status.idle":"2021-05-28T13:38:43.591220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}