{"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":"2022-01-01T08:58:52.851405Z","iopub.execute_input":"2022-01-01T08:58:52.852296Z","iopub.status.idle":"2022-01-01T08:58:52.884126Z","shell.execute_reply.started":"2022-01-01T08:58:52.852179Z","shell.execute_reply":"2022-01-01T08:58:52.883516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = []\ny_train = []\n\ndf_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nfor j in range(len(df_train)):\n    x = np.array(df_train.iloc[j:j+1 , 1:], np.uint8).reshape(28,28,1 )\n    x_train.append(x)\n    y_train.append(df_train.iloc[j]['label'])","metadata":{"execution":{"iopub.status.busy":"2022-01-01T08:59:18.466098Z","iopub.execute_input":"2022-01-01T08:59:18.467012Z","iopub.status.idle":"2022-01-01T08:59:31.934849Z","shell.execute_reply.started":"2022-01-01T08:59:18.46697Z","shell.execute_reply":"2022-01-01T08:59:31.934205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ntrain_in, test_in = x_train[:33600], x_train[33600:]\ntrain_out, test_out = y_train[:33600], y_train[33600:]\ntrain_out = tf.keras.utils.to_categorical( train_out, 10 )\ntest_out = tf.keras.utils.to_categorical( test_out, 10 )","metadata":{"execution":{"iopub.status.busy":"2022-01-01T08:59:31.936454Z","iopub.execute_input":"2022-01-01T08:59:31.936845Z","iopub.status.idle":"2022-01-01T08:59:38.681058Z","shell.execute_reply.started":"2022-01-01T08:59:31.936801Z","shell.execute_reply":"2022-01-01T08:59:38.679875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_in = np.array(train_in)\ntest_in = np.array(test_in) \ntrain_out = np.array(train_out)\ntest_out = np.array(test_out)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T08:59:39.126089Z","iopub.execute_input":"2022-01-01T08:59:39.126391Z","iopub.status.idle":"2022-01-01T08:59:39.175763Z","shell.execute_reply.started":"2022-01-01T08:59:39.12636Z","shell.execute_reply":"2022-01-01T08:59:39.175031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"digit_input = tf.keras.layers.Input( shape = (28,28,1) )\ncnn_1 = tf.keras.layers.Conv2D( filters = 64, kernel_size = (3,3), strides = (1,1),\n                               padding = \"valid\", activation = tf.nn.relu )( digit_input )\ncnn_2 = tf.keras.layers.Conv2D( filters = 64, kernel_size = (3,3), strides = (1,1),\n                               padding = \"valid\", activation = tf.nn.relu )( cnn_1 )\nflatten_image = tf.keras.layers.Flatten()( cnn_2 )\ndropout_1 = tf.keras.layers.Dropout( rate = 0.5 )( flatten_image )\ndense_1 = tf.keras.layers.Dense( units = 50, activation = tf.nn.relu )( dropout_1 )\nlogits = tf.keras.layers.Dense( units = 10, activation = None )( dense_1 )\nprobabilities = tf.keras.layers.Softmax()( logits )\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-01T08:59:51.874906Z","iopub.execute_input":"2022-01-01T08:59:51.875655Z","iopub.status.idle":"2022-01-01T08:59:52.034578Z","shell.execute_reply.started":"2022-01-01T08:59:51.875612Z","shell.execute_reply":"2022-01-01T08:59:52.03398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Model( inputs = digit_input, outputs = probabilities )\nmodel.compile( optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'] )\n","metadata":{"execution":{"iopub.status.busy":"2022-01-01T09:00:07.590025Z","iopub.execute_input":"2022-01-01T09:00:07.590348Z","iopub.status.idle":"2022-01-01T09:00:07.612566Z","shell.execute_reply.started":"2022-01-01T09:00:07.590314Z","shell.execute_reply":"2022-01-01T09:00:07.611907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit( train_in, train_out, epochs = 25 )","metadata":{"execution":{"iopub.status.busy":"2022-01-01T09:00:15.409242Z","iopub.execute_input":"2022-01-01T09:00:15.410225Z","iopub.status.idle":"2022-01-01T09:02:32.718233Z","shell.execute_reply.started":"2022-01-01T09:00:15.410151Z","shell.execute_reply":"2022-01-01T09:02:32.717382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nx_test = []\nfor j in range(len(df_test)):\n    x = np.array(df_test.iloc[j:j+1 , :], np.uint8).reshape(28,28,1 )\n    x_test.append(x)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T09:02:55.758213Z","iopub.execute_input":"2022-01-01T09:02:55.7592Z","iopub.status.idle":"2022-01-01T09:02:59.929594Z","shell.execute_reply.started":"2022-01-01T09:02:55.759152Z","shell.execute_reply":"2022-01-01T09:02:59.928632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax( model.predict( np.array(x_test)  ), axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T09:02:59.931482Z","iopub.execute_input":"2022-01-01T09:02:59.931728Z","iopub.status.idle":"2022-01-01T09:03:03.211493Z","shell.execute_reply.started":"2022-01-01T09:02:59.931699Z","shell.execute_reply":"2022-01-01T09:03:03.210829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_out = pd.DataFrame(columns=['ImageId','Label'])\nfor x in predictions:\n    df_out.loc[len(df_out)] = [len(df_out)+1, x]\ndf_out.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T09:03:03.213282Z","iopub.execute_input":"2022-01-01T09:03:03.213863Z","iopub.status.idle":"2022-01-01T09:04:00.032447Z","shell.execute_reply.started":"2022-01-01T09:03:03.213818Z","shell.execute_reply":"2022-01-01T09:04:00.031416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}