{"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":"### Digit Recognizer using ANN\n#### 98% Val Accuracy","metadata":{}},{"cell_type":"markdown","source":"#### Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\nseed = 42\nnp.random.seed(seed)\ntf.random.set_seed(seed)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:34.998734Z","iopub.execute_input":"2022-01-22T12:57:34.999292Z","iopub.status.idle":"2022-01-22T12:57:35.003814Z","shell.execute_reply.started":"2022-01-22T12:57:34.999255Z","shell.execute_reply":"2022-01-22T12:57:35.003101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import  optimizers, initializers, regularizers, activations, layers, losses","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.007489Z","iopub.execute_input":"2022-01-22T12:57:35.007728Z","iopub.status.idle":"2022-01-22T12:57:35.015551Z","shell.execute_reply.started":"2022-01-22T12:57:35.007700Z","shell.execute_reply":"2022-01-22T12:57:35.014883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Load Dataset","metadata":{}},{"cell_type":"code","source":"path = '../input/digit-recognizer/'\ntrain_data = pd.read_csv(path + 'train.csv').values\ntest_data = pd.read_csv(path + 'test.csv').values\nsubmit = pd.read_csv(path + 'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:53.324179Z","iopub.execute_input":"2022-01-22T12:57:53.324851Z","iopub.status.idle":"2022-01-22T12:57:57.033926Z","shell.execute_reply.started":"2022-01-22T12:57:53.324801Z","shell.execute_reply":"2022-01-22T12:57:57.033336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split target from train and test data\ny_train = train_data[:, 0]\ntrain_data = train_data[:, 1:]","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.054481Z","iopub.status.idle":"2022-01-22T12:57:35.055212Z","shell.execute_reply.started":"2022-01-22T12:57:35.055050Z","shell.execute_reply":"2022-01-22T12:57:35.055068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape, test_data.shape, submit.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.056368Z","iopub.status.idle":"2022-01-22T12:57:35.056725Z","shell.execute_reply.started":"2022-01-22T12:57:35.056529Z","shell.execute_reply":"2022-01-22T12:57:35.056552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Explore the data","metadata":{}},{"cell_type":"code","source":"def show_images(x, y):\n    plt.figure(figsize=(15, 15))\n    for i in range(10):\n        plt.subplot(5, 5, i + 1)\n        img = np.array(x[i]).reshape(28, 28)\n        plt.imshow(img, cmap='gray')\n        plt.title(f'label = {y[i]}')\n        plt.axis('off')\n\nshow_images(train_data, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.057704Z","iopub.status.idle":"2022-01-22T12:57:35.057985Z","shell.execute_reply.started":"2022-01-22T12:57:35.057829Z","shell.execute_reply":"2022-01-22T12:57:35.057848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Split Train data to validation and training","metadata":{}},{"cell_type":"code","source":"valid_data = train_data[:5000]\ny_valid = y_train[:5000]\ntrain_data = train_data[5000:]\ny_train = y_train[5000:]\n\nvalid_data.shape, y_valid.shape, train_data.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.059116Z","iopub.status.idle":"2022-01-22T12:57:35.059392Z","shell.execute_reply.started":"2022-01-22T12:57:35.059244Z","shell.execute_reply":"2022-01-22T12:57:35.059263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Scale the images","metadata":{}},{"cell_type":"code","source":"# train_data = train_data/255.0\n# test_data = test_data/255.0\n\nx_means = train_data.mean(axis=0)\nx_std = train_data.std(axis = 0)\n\ntrain_data_scaled = (train_data - x_means) / x_std\ntest_data_scaled = (test_data - x_means) / x_std\nvalid_data_scaled = (valid_data - x_means) / x_std","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.060497Z","iopub.status.idle":"2022-01-22T12:57:35.060843Z","shell.execute_reply.started":"2022-01-22T12:57:35.060640Z","shell.execute_reply":"2022-01-22T12:57:35.060659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Build the Model","metadata":{}},{"cell_type":"code","source":"class MCAlphaDropout(layers.AlphaDropout):\n    def call(self, inputs):\n        return super().call(inputs, training = True)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.069297Z","iopub.execute_input":"2022-01-22T12:57:35.069494Z","iopub.status.idle":"2022-01-22T12:57:35.073249Z","shell.execute_reply.started":"2022-01-22T12:57:35.069468Z","shell.execute_reply":"2022-01-22T12:57:35.072495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.backend.clear_session()","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.074845Z","iopub.execute_input":"2022-01-22T12:57:35.075266Z","iopub.status.idle":"2022-01-22T12:57:35.092191Z","shell.execute_reply.started":"2022-01-22T12:57:35.075229Z","shell.execute_reply":"2022-01-22T12:57:35.091657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential()\n\nfor units in [200, 200, 100]:\n    model.add(layers.Dense(units, activation=activations.elu, kernel_initializer=initializers.he_normal))\n    model.add(MCAlphaDropout(.2))\n\nmodel.add(layers.Dense(10, activation=activations.softmax))\n\nmodel.compile(optimizer=optimizers.Adam(learning_rate=.003), loss = losses.sparse_categorical_crossentropy, metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.132387Z","iopub.execute_input":"2022-01-22T12:57:35.132766Z","iopub.status.idle":"2022-01-22T12:57:35.163907Z","shell.execute_reply.started":"2022-01-22T12:57:35.132728Z","shell.execute_reply":"2022-01-22T12:57:35.163251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import callbacks\nmodel_checkpoint_cb = callbacks.ModelCheckpoint('best_model_regularizer.h5', monitor='val_accuracy', save_best_only= True)\nearlyStop_cb = callbacks.EarlyStopping(patience=300, monitor='val_accuracy')\ncb = [model_checkpoint_cb, earlyStop_cb]\n\nmodel.fit(train_data, y_train, validation_data=(valid_data, y_valid), epochs=500, batch_size=30, callbacks=cb)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.165208Z","iopub.execute_input":"2022-01-22T12:57:35.165376Z","iopub.status.idle":"2022-01-22T12:57:35.186803Z","shell.execute_reply.started":"2022-01-22T12:57:35.165355Z","shell.execute_reply":"2022-01-22T12:57:35.185337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_weights = model.get_weights()","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.187969Z","iopub.status.idle":"2022-01-22T12:57:35.188421Z","shell.execute_reply.started":"2022-01-22T12:57:35.188261Z","shell.execute_reply":"2022-01-22T12:57:35.188278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = pd.DataFrame(model.history.history)\nhistory.plot()","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.189372Z","iopub.status.idle":"2022-01-22T12:57:35.189836Z","shell.execute_reply.started":"2022-01-22T12:57:35.189647Z","shell.execute_reply":"2022-01-22T12:57:35.189681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Evaluate the Model","metadata":{}},{"cell_type":"code","source":"model.evaluate(valid_data, y_valid)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.190770Z","iopub.status.idle":"2022-01-22T12:57:35.191349Z","shell.execute_reply.started":"2022-01-22T12:57:35.191174Z","shell.execute_reply":"2022-01-22T12:57:35.191198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test_data)\ny_pred = np.argmax(y_pred, axis = 1)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.192381Z","iopub.status.idle":"2022-01-22T12:57:35.192702Z","shell.execute_reply.started":"2022-01-22T12:57:35.192516Z","shell.execute_reply":"2022-01-22T12:57:35.192540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Save the solution in submission.csv file","metadata":{}},{"cell_type":"code","source":"submit['Label'] = y_pred\nsubmit.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.193750Z","iopub.status.idle":"2022-01-22T12:57:35.194060Z","shell.execute_reply.started":"2022-01-22T12:57:35.193885Z","shell.execute_reply":"2022-01-22T12:57:35.193906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit","metadata":{"execution":{"iopub.status.busy":"2022-01-22T12:57:35.195053Z","iopub.status.idle":"2022-01-22T12:57:35.195372Z","shell.execute_reply.started":"2022-01-22T12:57:35.195189Z","shell.execute_reply":"2022-01-22T12:57:35.195211Z"},"trusted":true},"execution_count":null,"outputs":[]}]}