{"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n\nimport matplotlib.pyplot as plt # plotting library\n%matplotlib inline\n\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense , Activation, Dropout\nfrom keras.optimizers import Adam ,RMSprop\nfrom keras import  backend as K\n\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n\n# Any results you write to the current directory are saved as output.\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-20T22:11:09.99759Z","iopub.execute_input":"2021-09-20T22:11:09.997892Z","iopub.status.idle":"2021-09-20T22:11:10.018712Z","shell.execute_reply.started":"2021-09-20T22:11:09.997839Z","shell.execute_reply":"2021-09-20T22:11:10.017448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import dataset\nfrom keras.datasets import mnist\n\n\n# load dataset\n(x_train, y_train),(x_test, y_test) = mnist.load_data()\n\n\n\n# count the number of unique train labels\nunique, counts = np.unique(y_train, return_counts=True)\nprint(\"Train labels: \", dict(zip(unique, counts)))\n\n\n\n# count the number of unique test labels\nunique, counts = np.unique(y_test, return_counts=True)\nprint(\"\\nTest labels: \", dict(zip(unique, counts)))\n","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:11:21.5791Z","iopub.execute_input":"2021-09-20T22:11:21.5794Z","iopub.status.idle":"2021-09-20T22:11:23.101404Z","shell.execute_reply.started":"2021-09-20T22:11:21.579352Z","shell.execute_reply":"2021-09-20T22:11:23.100608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Data visualization <a class=\"anchor\" id=\"4\"></a>","metadata":{}},{"cell_type":"code","source":"# sample 25 mnist digits from train dataset\nindexes = np.random.randint(0, x_train.shape[0], size=25)\nimages = x_train[indexes]\nlabels = y_train[indexes]\n\n\n# plot the 25 mnist digits\nplt.figure(figsize=(5,5))\nfor i in range(len(indexes)):\n    plt.subplot(5, 5, i + 1)\n    image = images[i]\n    plt.imshow(image, cmap='gray')\n    plt.axis('off')\n    \nplt.show()\nplt.savefig(\"mnist-samples.png\")\nplt.close('all')","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:11:48.407088Z","iopub.execute_input":"2021-09-20T22:11:48.407386Z","iopub.status.idle":"2021-09-20T22:11:49.408099Z","shell.execute_reply.started":"2021-09-20T22:11:48.40734Z","shell.execute_reply":"2021-09-20T22:11:49.407415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5.1 Import Keras layers <a class=\"anchor\" id=\"5.1\"></a>","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Activation, Dropout\nfrom keras.utils import to_categorical, plot_model","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-09-20T22:12:10.098565Z","iopub.execute_input":"2021-09-20T22:12:10.098882Z","iopub.status.idle":"2021-09-20T22:12:10.104524Z","shell.execute_reply.started":"2021-09-20T22:12:10.098829Z","shell.execute_reply":"2021-09-20T22:12:10.103722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5.2 Compute the number of labels <a class=\"anchor\" id=\"5.2\"></a>\n\n- Now, the data must be in the correct shape and format. \n\n- After loading the MNIST dataset, the number of labels is computed as:","metadata":{}},{"cell_type":"code","source":"# compute the number of labels\nnum_labels = len(np.unique(y_train))","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:12:13.482459Z","iopub.execute_input":"2021-09-20T22:12:13.482769Z","iopub.status.idle":"2021-09-20T22:12:13.489443Z","shell.execute_reply.started":"2021-09-20T22:12:13.482716Z","shell.execute_reply":"2021-09-20T22:12:13.488225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert to one-hot vector\ny_train = to_categorical(y_train)\ny_test = to_categorical(y_test)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:12:21.437413Z","iopub.execute_input":"2021-09-20T22:12:21.437713Z","iopub.status.idle":"2021-09-20T22:12:21.445341Z","shell.execute_reply.started":"2021-09-20T22:12:21.43766Z","shell.execute_reply":"2021-09-20T22:12:21.444344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image dimensions (assumed square)\nimage_size = x_train.shape[1]\ninput_size = image_size * image_size\ninput_size","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:12:27.415304Z","iopub.execute_input":"2021-09-20T22:12:27.415598Z","iopub.status.idle":"2021-09-20T22:12:27.421121Z","shell.execute_reply.started":"2021-09-20T22:12:27.415548Z","shell.execute_reply":"2021-09-20T22:12:27.420069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# resize and normalize\nx_train = np.reshape(x_train, [-1, input_size])\nx_train = x_train.astype('float32') / 255\nx_test = np.reshape(x_test, [-1, input_size])\nx_test = x_test.astype('float32') / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:12:31.414031Z","iopub.execute_input":"2021-09-20T22:12:31.414331Z","iopub.status.idle":"2021-09-20T22:12:31.600435Z","shell.execute_reply.started":"2021-09-20T22:12:31.414282Z","shell.execute_reply":"2021-09-20T22:12:31.599607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5.5 Setting network parameters <a class=\"anchor\" id=\"5.5\"></a>\n\n- Now, we will set the network parameters as follows:","metadata":{}},{"cell_type":"code","source":"# network parameters\nbatch_size = 64\nhidden_units = 125\ndropout = 0.45","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:12:56.716095Z","iopub.execute_input":"2021-09-20T22:12:56.716377Z","iopub.status.idle":"2021-09-20T22:12:56.723384Z","shell.execute_reply.started":"2021-09-20T22:12:56.71633Z","shell.execute_reply":"2021-09-20T22:12:56.722143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model is a 3-layer MLP with ReLU and dropout after each layer\nmodel = Sequential()\nmodel.add(Dense(hidden_units, input_dim=input_size))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(dropout))\nmodel.add(Dense(hidden_units))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(dropout))\nmodel.add(Dense(num_labels))\nmodel.add(Activation('softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:13:12.006832Z","iopub.execute_input":"2021-09-20T22:13:12.007138Z","iopub.status.idle":"2021-09-20T22:13:14.392935Z","shell.execute_reply.started":"2021-09-20T22:13:12.007087Z","shell.execute_reply":"2021-09-20T22:13:14.392151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:13:21.586525Z","iopub.execute_input":"2021-09-20T22:13:21.586817Z","iopub.status.idle":"2021-09-20T22:13:21.595581Z","shell.execute_reply.started":"2021-09-20T22:13:21.586767Z","shell.execute_reply":"2021-09-20T22:13:21.594716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', \n              optimizer='adam',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:13:45.855234Z","iopub.execute_input":"2021-09-20T22:13:45.8556Z","iopub.status.idle":"2021-09-20T22:13:45.942583Z","shell.execute_reply.started":"2021-09-20T22:13:45.855553Z","shell.execute_reply":"2021-09-20T22:13:45.941901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 6.2 Train the model with fit() method <a class=\"anchor\" id=\"6.2\"></a>","metadata":{}},{"cell_type":"code","source":"model.fit(x_train, y_train, epochs=20, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:13:54.1508Z","iopub.execute_input":"2021-09-20T22:13:54.151128Z","iopub.status.idle":"2021-09-20T22:14:57.505898Z","shell.execute_reply.started":"2021-09-20T22:13:54.151074Z","shell.execute_reply":"2021-09-20T22:14:57.505122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 6.3 Evaluating model performance with evaluate() method <a class=\"anchor\" id=\"6.3\"></a>","metadata":{}},{"cell_type":"code","source":"loss, acc = model.evaluate(x_test, y_test, batch_size=batch_size)\nprint(\"\\nTest accuracy: %.1f%%\" % (100.0 * acc))","metadata":{"execution":{"iopub.status.busy":"2021-09-20T22:15:04.804464Z","iopub.execute_input":"2021-09-20T22:15:04.804756Z","iopub.status.idle":"2021-09-20T22:15:05.2091Z","shell.execute_reply.started":"2021-09-20T22:15:04.804705Z","shell.execute_reply":"2021-09-20T22:15:05.207011Z"},"trusted":true},"execution_count":null,"outputs":[]}]}