{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Import Tensorflow 2.0\nimport tensorflow as tf \n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport random\nfrom tqdm import tqdm\n\n# Check that we are using a GPU, if not switch runtimes\n#   using Runtime > Change Runtime Type > GPU\nassert len(tf.config.list_physical_devices('GPU')) > 0","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nos.listdir('../input/digit-recognizer')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\n\nbase = '../input/digit-recognizer/'\ntrain_df = pd.read_csv(base+'train.csv')\ntest_df = pd.read_csv(base+'test.csv')\n\n# df.head(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.concat([train_df, test_df])\ncols = list(df.columns)\ncols.remove('label')\ndf[cols] = df[cols] / 255.0\ndf.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = df.iloc[:len(train_df)]\ntest = df.iloc[len(train_df):]\ntest.drop(columns=['label'], inplace=True)\n\nX_train = train.drop(columns=['label'])\nY_train = train['label']\nX_train = X_train.values.reshape(-1,28,28,1)\nX_test = test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_sample, X_val, Y_sample, Y_val = train_test_split(X_train, Y_train, test_size=0.25, random_state = 19)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## CNN MODELING\n#### Using Maxpooling"},{"metadata":{"trusted":true},"cell_type":"code","source":"def build_cnn_model():\n    cnn_model = tf.keras.Sequential([\n        \n#         tf.keras.layers.Flatten(),\n        tf.keras.layers.Conv2D(filters=24, kernel_size=(3,3), activation=tf.nn.relu),\n        tf.keras.layers.MaxPool2D(),\n        tf.keras.layers.Conv2D(36, (3,3), activation=tf.nn.relu),\n        tf.keras.layers.MaxPool2D(),\n        tf.keras.layers.Flatten(),\n        tf.keras.layers.Dense(128, activation=tf.nn.relu),\n        tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n        \n    ])\n    \n    return cnn_model\n\ncnn_model = build_cnn_model()\ncnn_model.predict(X_sample[:3])\nprint(cnn_model.summary())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cnn_model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.01), loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\nBATCH_SIZE = 64\n\ncnn_model.fit(X_sample, Y_sample, batch_size = BATCH_SIZE, epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_loss, test_acc = cnn_model.evaluate(X_val, Y_val, batch_size=BATCH_SIZE)\nprint(\"Test Accuracy: {:.4f}\".format(test_acc))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = cnn_model.predict(X_test)\npredictions[1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = [np.argmax(predictions[i]) for i in range(len(predictions))]\npredictions[:3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsample.head(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample['Label'] = predictions\nsample.head(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample.to_csv('CNN_2layers_Adagrad.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}