{"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)\n\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":"2023-01-31T17:26:14.489715Z","iopub.execute_input":"2023-01-31T17:26:14.490127Z","iopub.status.idle":"2023-01-31T17:26:14.506981Z","shell.execute_reply.started":"2023-01-31T17:26:14.490093Z","shell.execute_reply":"2023-01-31T17:26:14.506063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Importing","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:26:14.509476Z","iopub.execute_input":"2023-01-31T17:26:14.510160Z","iopub.status.idle":"2023-01-31T17:26:14.514607Z","shell.execute_reply.started":"2023-01-31T17:26:14.510125Z","shell.execute_reply":"2023-01-31T17:26:14.513727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load data","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nsample = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:26:14.515988Z","iopub.execute_input":"2023-01-31T17:26:14.516582Z","iopub.status.idle":"2023-01-31T17:26:18.534377Z","shell.execute_reply.started":"2023-01-31T17:26:14.516549Z","shell.execute_reply":"2023-01-31T17:26:18.533249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Spliting data","metadata":{}},{"cell_type":"code","source":"X = train.drop('label',axis=1)\ny = train['label']","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:26:18.537532Z","iopub.execute_input":"2023-01-31T17:26:18.538834Z","iopub.status.idle":"2023-01-31T17:26:18.670698Z","shell.execute_reply.started":"2023-01-31T17:26:18.538784Z","shell.execute_reply":"2023-01-31T17:26:18.669540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:26:18.672091Z","iopub.execute_input":"2023-01-31T17:26:18.673017Z","iopub.status.idle":"2023-01-31T17:26:18.944135Z","shell.execute_reply.started":"2023-01-31T17:26:18.672975Z","shell.execute_reply":"2023-01-31T17:26:18.942676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Preparing data for model","metadata":{}},{"cell_type":"code","source":"X_train = X_train / 255.0\nX_test = X_test / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:26:18.945885Z","iopub.execute_input":"2023-01-31T17:26:18.946610Z","iopub.status.idle":"2023-01-31T17:26:19.012584Z","shell.execute_reply.started":"2023-01-31T17:26:18.946553Z","shell.execute_reply":"2023-01-31T17:26:19.011661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\nX_test = X_test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:26:19.013587Z","iopub.execute_input":"2023-01-31T17:26:19.013893Z","iopub.status.idle":"2023-01-31T17:26:19.019202Z","shell.execute_reply.started":"2023-01-31T17:26:19.013865Z","shell.execute_reply":"2023-01-31T17:26:19.018364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Model","metadata":{}},{"cell_type":"code","source":"#Model\n\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(64,7,activation='relu',padding='same',input_shape=(28,28,1)),\n    tf.keras.layers.MaxPooling2D(2),\n    tf.keras.layers.Conv2D(128,3, activation = 'relu',padding='same'),\n    tf.keras.layers.Conv2D(128,3, activation = 'relu',padding='same'),\n    tf.keras.layers.MaxPooling2D(2),\n    tf.keras.layers.Conv2D(256,3, activation = 'relu',padding='same'),\n    tf.keras.layers.Conv2D(256,3, activation = 'relu',padding='same'),\n    tf.keras.layers.MaxPooling2D(2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128,activation = 'relu'),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Dense(64,activation = 'relu'),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Dense(10,activation = 'softmax')\n])\n# Compile model\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics='Accuracy')\n","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:26:19.021088Z","iopub.execute_input":"2023-01-31T17:26:19.021681Z","iopub.status.idle":"2023-01-31T17:26:19.144549Z","shell.execute_reply.started":"2023-01-31T17:26:19.021650Z","shell.execute_reply":"2023-01-31T17:26:19.143205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:26:19.146239Z","iopub.execute_input":"2023-01-31T17:26:19.146617Z","iopub.status.idle":"2023-01-31T17:26:19.154692Z","shell.execute_reply.started":"2023-01-31T17:26:19.146585Z","shell.execute_reply":"2023-01-31T17:26:19.153413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit\nmodel.fit(x=X_train, \n          y=y_train, \n          epochs=7,\n          validation_data=(X_test, y_test),\n          batch_size=256)","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:26:19.158131Z","iopub.execute_input":"2023-01-31T17:26:19.158887Z","iopub.status.idle":"2023-01-31T17:48:53.998340Z","shell.execute_reply.started":"2023-01-31T17:26:19.158849Z","shell.execute_reply":"2023-01-31T17:48:53.997369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = pd.DataFrame(model.history.history)\nhist.plot()","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:48:53.999559Z","iopub.execute_input":"2023-01-31T17:48:54.000584Z","iopub.status.idle":"2023-01-31T17:48:54.304143Z","shell.execute_reply.started":"2023-01-31T17:48:54.000544Z","shell.execute_reply":"2023-01-31T17:48:54.302344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:48:54.306196Z","iopub.execute_input":"2023-01-31T17:48:54.306767Z","iopub.status.idle":"2023-01-31T17:48:54.315295Z","shell.execute_reply.started":"2023-01-31T17:48:54.306708Z","shell.execute_reply":"2023-01-31T17:48:54.313374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test)\npredictions = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:48:54.317729Z","iopub.execute_input":"2023-01-31T17:48:54.318257Z","iopub.status.idle":"2023-01-31T17:49:45.168638Z","shell.execute_reply.started":"2023-01-31T17:48:54.318148Z","shell.execute_reply":"2023-01-31T17:49:45.167439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample['Label'] = predictions\nsample.to_csv('sample_submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:49:45.170164Z","iopub.execute_input":"2023-01-31T17:49:45.170496Z","iopub.status.idle":"2023-01-31T17:49:45.176871Z","shell.execute_reply.started":"2023-01-31T17:49:45.170465Z","shell.execute_reply":"2023-01-31T17:49:45.175611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample","metadata":{"execution":{"iopub.status.busy":"2023-01-31T17:50:14.270865Z","iopub.execute_input":"2023-01-31T17:50:14.271538Z","iopub.status.idle":"2023-01-31T17:50:14.290580Z","shell.execute_reply.started":"2023-01-31T17:50:14.271501Z","shell.execute_reply":"2023-01-31T17:50:14.289073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}