{"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\nimport pandas as pd \nimport tensorflow as tf\nimport matplotlib.pyplot as plt\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":"2023-03-03T20:45:06.369265Z","iopub.execute_input":"2023-03-03T20:45:06.369769Z","iopub.status.idle":"2023-03-03T20:45:09.900067Z","shell.execute_reply.started":"2023-03-03T20:45:06.369724Z","shell.execute_reply":"2023-03-03T20:45:09.898439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ny_train = X[\"label\"]\nX_train = X.drop(columns=[\"label\"])\ny_train = tf.keras.utils.to_categorical(y_train)\nX_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\nprint(X_train.shape, y_train.shape, X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T20:45:09.902474Z","iopub.execute_input":"2023-03-03T20:45:09.903248Z","iopub.status.idle":"2023-03-03T20:45:14.357187Z","shell.execute_reply.started":"2023-03-03T20:45:09.903200Z","shell.execute_reply":"2023-03-03T20:45:14.354967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train / 255\nX_test = X_test / 255","metadata":{"execution":{"iopub.status.busy":"2023-03-03T20:45:14.358730Z","iopub.execute_input":"2023-03-03T20:45:14.359342Z","iopub.status.idle":"2023-03-03T20:45:14.475740Z","shell.execute_reply.started":"2023-03-03T20:45:14.359285Z","shell.execute_reply":"2023-03-03T20:45:14.474639Z"},"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)\n\nprint(X_train.shape, X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T20:45:14.478399Z","iopub.execute_input":"2023-03-03T20:45:14.478777Z","iopub.status.idle":"2023-03-03T20:45:14.487731Z","shell.execute_reply.started":"2023-03-03T20:45:14.478741Z","shell.execute_reply":"2023-03-03T20:45:14.486221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[42], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-03-03T20:45:14.489816Z","iopub.execute_input":"2023-03-03T20:45:14.490791Z","iopub.status.idle":"2023-03-03T20:45:14.723801Z","shell.execute_reply.started":"2023-03-03T20:45:14.490730Z","shell.execute_reply":"2023-03-03T20:45:14.722006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model():\n    model = tf.keras.Sequential()\n    model.add(tf.keras.Input(shape=tuple(X_train.shape[1:])))\n    model.add(tf.keras.layers.Conv2D(3, 5, activation='relu'))       \n    model.add(tf.keras.layers.MaxPool2D())    \n    model.add(tf.keras.layers.Conv2D(3, 3, activation='relu'))\n    model.add(tf.keras.layers.Flatten())       \n    model.add(tf.keras.layers.Dense(64, activation='relu'))    \n    model.add(tf.keras.layers.Dense(10))\n    model.summary()\n    model.compile(optimizer='adam', \n                  loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True), \n                  metrics=['accuracy', 'categorical_accuracy'])\n    return model\n\nmodel = get_model()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T20:45:14.725856Z","iopub.execute_input":"2023-03-03T20:45:14.726273Z","iopub.status.idle":"2023-03-03T20:45:14.921405Z","shell.execute_reply.started":"2023-03-03T20:45:14.726235Z","shell.execute_reply":"2023-03-03T20:45:14.920112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, batch_size=128, epochs=50, validation_split=0.1, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T20:45:14.922993Z","iopub.execute_input":"2023-03-03T20:45:14.923544Z","iopub.status.idle":"2023-03-03T20:51:38.172471Z","shell.execute_reply.started":"2023-03-03T20:45:14.923488Z","shell.execute_reply":"2023-03-03T20:51:38.171329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T20:51:38.174275Z","iopub.execute_input":"2023-03-03T20:51:38.174796Z","iopub.status.idle":"2023-03-03T20:51:38.416749Z","shell.execute_reply.started":"2023-03-03T20:51:38.174739Z","shell.execute_reply":"2023-03-03T20:51:38.415443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test)\npredictions = np.argmax(predictions,axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T20:51:38.418354Z","iopub.execute_input":"2023-03-03T20:51:38.418760Z","iopub.status.idle":"2023-03-03T20:51:42.950345Z","shell.execute_reply.started":"2023-03-03T20:51:38.418720Z","shell.execute_reply":"2023-03-03T20:51:42.948791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = list(enumerate(predictions, start=1))\nresults_df = pd.DataFrame(results, columns=[\"ImageId\", \"Label\"])\nresults_df.to_csv(\"results.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T20:51:42.953939Z","iopub.execute_input":"2023-03-03T20:51:42.954405Z","iopub.status.idle":"2023-03-03T20:51:47.547875Z","shell.execute_reply.started":"2023-03-03T20:51:42.954357Z","shell.execute_reply":"2023-03-03T20:51:47.546566Z"},"trusted":true},"execution_count":null,"outputs":[]}]}