{"cells":[{"metadata":{},"cell_type":"markdown","source":"# **MNIST Handwritten Digit Recognition using Keras**\n","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Hello! Ever since Neural Network has been open sourced, things have changed far more than before. Now they launched [GPT-3](https://www.technologyreview.com/2020/07/20/1005454/openai-machine-learning-language-generator-gpt-3-nlp/) with almost 175 billion features and things are accelerating!\n\nBut, for a beginner, things will always start from the very beginning and accelerate thereafter. We will not delay, nor accelerate, but will begin from the scratch.\n\nHere I will demonstrate the very basics of Keras (as they call it the hello world of neural network) implementation in our very popular mnist dataset.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Without any further ado, let's see the implementation.\n","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from tensorflow import keras\nfrom keras import regularizers\nimport numpy as np\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"For ease to the newbies, I will use keras datasets ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"data=keras.datasets.mnist\n(x_train,y_train),(x_test,y_test)=data.load_data()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let's scale it by dividing with 255 since the color value is given in the range of zero to two-fiftyfive","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train=x_train/255\nx_test=x_test/255","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"And ofcourse, Validation data is a must. \nPeople don't seem to use the separate validation data, but that is not the standard way to go with, cause that way our test data won't be very new to the model!","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train,y_train=x_train[5000:],y_train[5000:]\nx_valid,y_valid=x_train[:5000],y_train[:5000]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model creation\nTime for creating a very robust model!!🤘","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"#Note: I have commented those lines that are used for regularizing the model. Don't touch it unless you want to play around with it!\nmodel=keras.models.Sequential()\nmodel.add(keras.layers.Input(shape=[28,28]))\nmodel.add(keras.layers.Flatten())\nmodel.add(keras.layers.Dense(300,activation='relu'))\n#model.add(keras.layers.Dense(300,activation='relu'),kernel_regularizer=regularizers.l2(0.02))\nmodel.add(keras.layers.Dense(200,activation='relu'))\n#model.add(keras.layers.Dense(300,activation='relu'),kernel_regularizer=regularizers.l2(0.02))\nmodel.add(keras.layers.Dense(10,activation='softmax'))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We will make the use of plot_model from keras.util library. This will visualize the model in a more sound manner as I guess.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"keras.utils.plot_model(model,show_shapes=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Compile time!","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Let's compile it with very simple and the most used optimizer as adam and metrics as accuracy. Besides, we are gonna use loss as sparse categorical crossentropy as this suits the best to the model's requirement","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam',metrics=['accuracy'],loss='sparse_categorical_crossentropy')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Fit the model this way","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"history=model.fit(x_train,y_train,batch_size=32,epochs=20,validation_data=(x_valid,y_valid))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Okay. Done with fitting. Let's analyze our model's performance using some graphs.**","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"For analyzing, I will use history as the major parameter since it kept track of each incident while model was being trained","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['loss'],'r--')\nplt.plot(history.history['val_loss'],'b--')\nplt.grid()\nplt.xlabel('epochs')\nplt.ylabel('losses')\nplt.legend(['training','validating'])\nplt.title('Loss analysis')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['accuracy'],'r--')\nplt.plot(history.history['val_accuracy'],'b--')\nplt.grid()\nplt.xlabel('epochs')\nplt.ylabel('accuracies')\nplt.legend(['training','validating'])\nplt.title('Accuracy analysis')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Why not evaluate with the accuracy score!\n","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"mse=model.evaluate(x_test,y_test)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The accuracy came to be 0.9808. Pretty high! We might have overfitted the model","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Well, to check if the model was overfitted, I used l2 regularizer with lambda value of 0.05. \nThat gave me a bit little accuracy. However, it is okay to have that accuracy.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## A Final Check!\nLet's check the model with some of the test data ourselves.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test[:10]\ny_test[:10]\ny_pred=model.predict(x_test[:10])\ny_pred\ny_pred=np.round(y_pred)\nprint(y_pred)\n#here y_pred gave me 10 values. So the value which is the highest one is the corresponding number's location.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Hmm. Seems fine! This concludes our model development.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## See you next time. Adieu!","execution_count":null}],"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}