{"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":"markdown","source":"## This does not make use of a CNN but still gets a high accuracy\nNote: In practice, one should use CNNs.\n\nAlso, I would truly appreciate if you gave an upvote! Thanks in advance!","metadata":{}},{"cell_type":"code","source":"# Importing libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport gc\nimport tensorflow as tf\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T14:15:50.600948Z","iopub.execute_input":"2022-07-25T14:15:50.601293Z","iopub.status.idle":"2022-07-25T14:15:51.216888Z","shell.execute_reply.started":"2022-07-25T14:15:50.601266Z","shell.execute_reply":"2022-07-25T14:15:51.215932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading in data\nX = pd.read_csv('../input/digit-recognizer/train.csv')\ny = X['label']\nX.drop('label', axis=1, inplace=True)\nX_test = pd.read_csv('../input/digit-recognizer/test.csv')\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T13:56:47.895793Z","iopub.execute_input":"2022-07-25T13:56:47.896416Z","iopub.status.idle":"2022-07-25T13:56:53.573710Z","shell.execute_reply.started":"2022-07-25T13:56:47.896379Z","shell.execute_reply":"2022-07-25T13:56:53.572821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.random.set_seed(42) # For consistent results\n# Creating a model\nmodel = tf.keras.Sequential(\n    [\n        tf.keras.Input(shape=(784,)),\n        tf.keras.layers.Dense(10000, activation='relu'),\n        tf.keras.layers.Dense(500, activation='relu'),\n        tf.keras.layers.Dense(10, activation='linear'),\n    ], name = 'MLP_Model'\n)\n# Compiling the model\nmodel.compile(optimizer=tf.keras.optimizers.Ftrl(learning_rate=0.001), loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True))\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T14:03:32.169686Z","iopub.execute_input":"2022-07-25T14:03:32.170064Z","iopub.status.idle":"2022-07-25T14:03:32.435621Z","shell.execute_reply.started":"2022-07-25T14:03:32.170031Z","shell.execute_reply":"2022-07-25T14:03:32.434499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting the model\nhistory = model.fit(x=X, y=y, batch_size=512, epochs=151, verbose=1, validation_split=0.1)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T14:03:34.608342Z","iopub.execute_input":"2022-07-25T14:03:34.608690Z","iopub.status.idle":"2022-07-25T14:08:57.567884Z","shell.execute_reply.started":"2022-07-25T14:03:34.608660Z","shell.execute_reply":"2022-07-25T14:08:57.566847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,9), dpi=100)\nplt.plot(history.history['loss'][1:], color='b')\nplt.show()\nplt.figure(figsize=(12,9), dpi=100)\nplt.plot(history.history['val_loss'][:], color='b')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T14:18:59.291110Z","iopub.execute_input":"2022-07-25T14:18:59.291556Z","iopub.status.idle":"2022-07-25T14:18:59.715666Z","shell.execute_reply.started":"2022-07-25T14:18:59.291520Z","shell.execute_reply":"2022-07-25T14:18:59.714770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predicting on test data and submitting predictions\npredictions = model.predict(X_test)\npreds = tf.nn.softmax(predictions)\nfinal_preds = [np.argmax(pred) for pred in preds]\nfinal_preds = pd.Series(final_preds, name='Label')\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),final_preds], axis=1)\nsubmission.to_csv('submission.csv', index=False)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T14:00:19.788227Z","iopub.execute_input":"2022-07-25T14:00:19.789428Z","iopub.status.idle":"2022-07-25T14:00:30.672811Z","shell.execute_reply.started":"2022-07-25T14:00:19.789386Z","shell.execute_reply":"2022-07-25T14:00:30.671807Z"},"trusted":true},"execution_count":null,"outputs":[]}]}