{"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).astype(np.float32)\nX_test = test.values.reshape(-1,28,28,1).astype(np.float32)","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 = 7)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.1, # Randomly zoom image \n        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\n\ndatagen.fit(X_sample)","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=36, kernel_size=(5,5), activation=tf.nn.relu),\n#         tf.keras.layers.Conv2D(filters=36, kernel_size=(3,3), activation=tf.nn.relu),\n        tf.keras.layers.MaxPool2D(),\n#         tf.keras.layers.Dropout(0.1),\n        \n        tf.keras.layers.Conv2D(filters=36, kernel_size=(5,5), activation=tf.nn.relu),\n#         tf.keras.layers.Conv2D(filters=36, kernel_size=(5,5), activation=tf.nn.relu),\n        tf.keras.layers.MaxPool2D(),\n#         tf.keras.layers.Dropout(0.1),\n        \n        tf.keras.layers.Flatten(),\n        tf.keras.layers.Dense(256, activation=tf.nn.relu),\n        tf.keras.layers.Dropout(0.1),\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.05), loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\nBATCH_SIZE = 40\n\n# cnn_model.fit(X_sample, Y_sample, batch_size = BATCH_SIZE, epochs=20)\n\nhistory = cnn_model.fit_generator(datagen.flow(X_sample,Y_sample, batch_size=BATCH_SIZE),\n                              epochs = 50, validation_data = (X_val,Y_val),\n                              verbose = 2, steps_per_epoch=X_sample.shape[0] // BATCH_SIZE)","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_Adagrad4.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}