{"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)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\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":"2021-07-24T09:45:05.524086Z","iopub.execute_input":"2021-07-24T09:45:05.524617Z","iopub.status.idle":"2021-07-24T09:45:05.535026Z","shell.execute_reply.started":"2021-07-24T09:45:05.524561Z","shell.execute_reply":"2021-07-24T09:45:05.534156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/digit-recognizer/train.csv')\ntrain_dt = np.array(df_train.iloc[:40000,1:]).reshape(-1, 28, 28)/255\ntrain_labels = np.array(df_train.iloc[:40000,:1]).reshape(-1,)\n\nval_dt = np.array(df_train.iloc[40000:,1:]).reshape(-1, 28, 28)/255\nval_labels = np.array(df_train.iloc[40000:,:1]).reshape(-1,)\n\ndf_test = pd.read_csv('../input/digit-recognizer/test.csv')\ntest_dt = np.array(df_test).reshape(-1, 28, 28)/255","metadata":{"execution":{"iopub.status.busy":"2021-07-24T09:45:05.536644Z","iopub.execute_input":"2021-07-24T09:45:05.537055Z","iopub.status.idle":"2021-07-24T09:45:09.731022Z","shell.execute_reply.started":"2021-07-24T09:45:05.537014Z","shell.execute_reply":"2021-07-24T09:45:09.730248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = tf.data.Dataset.from_tensor_slices((train_dt, train_labels))\nval_dataset = tf.data.Dataset.from_tensor_slices((val_dt, val_labels))","metadata":{"execution":{"iopub.status.busy":"2021-07-24T09:45:09.732580Z","iopub.execute_input":"2021-07-24T09:45:09.733024Z","iopub.status.idle":"2021-07-24T09:45:10.053594Z","shell.execute_reply.started":"2021-07-24T09:45:09.732979Z","shell.execute_reply":"2021-07-24T09:45:10.052652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 64\nSHUFFLE_BUFFER_SIZE = 100\n\ntrain_dataset = train_dataset.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)\nval_dataset = val_dataset.batch(BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T09:45:10.055170Z","iopub.execute_input":"2021-07-24T09:45:10.055475Z","iopub.status.idle":"2021-07-24T09:45:10.061518Z","shell.execute_reply.started":"2021-07-24T09:45:10.055436Z","shell.execute_reply":"2021-07-24T09:45:10.060574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential()\n\nmodel.add(tf.keras.layers.Flatten(input_shape=(28, 28)))\n\nmodel.add(tf.keras.layers.Dense(512, activation=tf.nn.relu))\nmodel.add(tf.keras.layers.Dropout(0.2))\nmodel.add(tf.keras.layers.Dense(256, activation=tf.nn.relu))\nmodel.add(tf.keras.layers.Dropout(0.2))\nmodel.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\nmodel.add(tf.keras.layers.Dropout(0.2))\n\nmodel.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))\n\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-24T09:45:10.062573Z","iopub.execute_input":"2021-07-24T09:45:10.062887Z","iopub.status.idle":"2021-07-24T09:45:10.121039Z","shell.execute_reply.started":"2021-07-24T09:45:10.062853Z","shell.execute_reply":"2021-07-24T09:45:10.120341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_dataset, epochs=15)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T09:45:10.121925Z","iopub.execute_input":"2021-07-24T09:45:10.122157Z","iopub.status.idle":"2021-07-24T09:45:59.121213Z","shell.execute_reply.started":"2021-07-24T09:45:10.122136Z","shell.execute_reply":"2021-07-24T09:45:59.120337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(val_dt, val_labels)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T09:45:59.122207Z","iopub.execute_input":"2021-07-24T09:45:59.122438Z","iopub.status.idle":"2021-07-24T09:45:59.349176Z","shell.execute_reply.started":"2021-07-24T09:45:59.122415Z","shell.execute_reply":"2021-07-24T09:45:59.348353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId':range(1,28001), 'Label': np.argmax(model.predict(test_dt), axis=1)})\npd.DataFrame(submission).to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T09:45:59.351921Z","iopub.execute_input":"2021-07-24T09:45:59.352167Z","iopub.status.idle":"2021-07-24T09:46:00.570751Z","shell.execute_reply.started":"2021-07-24T09:45:59.352143Z","shell.execute_reply":"2021-07-24T09:46:00.569839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}