From 6b2ce7aa9a921554423790f5c299c0859ee09d72 Mon Sep 17 00:00:00 2001 From: garg-saurav Date: Mon, 12 Oct 2020 00:23:47 +0530 Subject: [PATCH 1/3] Fixed #1 Update architecture to resemble LeNet-5 --- MNIST_CNN.ipynb | 234 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 233 insertions(+), 1 deletion(-) diff --git a/MNIST_CNN.ipynb b/MNIST_CNN.ipynb index d102888..f5d2ca2 100644 --- a/MNIST_CNN.ipynb +++ b/MNIST_CNN.ipynb @@ -1 +1,233 @@ -{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"MNIST_CNN.ipynb","provenance":[{"file_id":"https://github.com/tensorflow/docs/blob/master/site/en/tutorials/quickstart/beginner.ipynb","timestamp":1602355806043}],"collapsed_sections":["rX8mhOLljYeM"]},"kernelspec":{"display_name":"Python 3","name":"python3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"3wF5wszaj97Y"},"source":["# MNIST using Neural Networks"]},{"cell_type":"markdown","metadata":{"id":"nnrWf3PCEzXL"},"source":["Run the statement to import the TensorFlow library:"]},{"cell_type":"code","metadata":{"id":"0trJmd6DjqBZ"},"source":["import tensorflow as tf"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"7NAbSZiaoJ4z"},"source":["Run the statement to load and prepare the [MNIST dataset](http://yann.lecun.com/exdb/mnist/) and to convert the samples from integers to floating-point numbers:"]},{"cell_type":"code","metadata":{"id":"7FP5258xjs-v","executionInfo":{"status":"ok","timestamp":1602354146120,"user_tz":-330,"elapsed":2887,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"1340781f-c5e6-4f7f-88a0-6e1ff220044b","colab":{"base_uri":"https://localhost:8080/","height":50}},"source":["mnist = tf.keras.datasets.mnist\n","\n","(x_train, y_train), (x_test, y_test) = mnist.load_data()\n","x_train, x_test = x_train / 255.0, x_test / 255.0"],"execution_count":null,"outputs":[{"output_type":"stream","text":["Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n","11493376/11490434 [==============================] - 0s 0us/step\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"BPZ68wASog_I"},"source":["Build the model with a single layer and relu activation function:"]},{"cell_type":"code","metadata":{"id":"h3IKyzTCDNGo"},"source":["model = tf.keras.models.Sequential([\n"," tf.keras.layers.Flatten(input_shape=(28, 28)),\n"," tf.keras.layers.Dense(4, activation='relu'),\n"," tf.keras.layers.Dense(10)\n","])\n","# try to add convolutions and change the no of layers and no of nodes/neurons\n","# to add a new layer\n","# tf.keras.layers.Dense(10, activation='relu') \n","# to add convolutions \n","# tf.keras.layers.Conv2D(28, (3, 3), activation='relu', input_shape=(28, 28)) \n","# tf.keras.layers.MaxPooling2D((2, 2))"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"hQyugpgRIyrA"},"source":["Choosing our loss function, in this case crossentropy:"]},{"cell_type":"code","metadata":{"id":"RSkzdv8MD0tT"},"source":["loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"9foNKHzTD2Vo"},"source":["model.compile(optimizer='adam',\n"," loss=loss_fn,\n"," metrics=['accuracy'])"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"ix4mEL65on-w"},"source":["Adjusting the model parameters to minimize the loss of our model: "]},{"cell_type":"code","metadata":{"id":"y7suUbJXVLqP","executionInfo":{"status":"ok","timestamp":1602354953453,"user_tz":-330,"elapsed":12916,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"9a824dbd-073f-4e6f-e021-c10361bf8339","colab":{"base_uri":"https://localhost:8080/","height":202}},"source":["model.fit(x_train, y_train, epochs=5)"],"execution_count":null,"outputs":[{"output_type":"stream","text":["Epoch 1/5\n","1875/1875 [==============================] - 2s 1ms/step - loss: 0.5251 - accuracy: 0.8486\n","Epoch 2/5\n","1875/1875 [==============================] - 2s 1ms/step - loss: 0.5171 - accuracy: 0.8508\n","Epoch 3/5\n","1875/1875 [==============================] - 2s 1ms/step - loss: 0.5112 - accuracy: 0.8515\n","Epoch 4/5\n","1875/1875 [==============================] - 2s 1ms/step - loss: 0.5050 - accuracy: 0.8535\n","Epoch 5/5\n","1875/1875 [==============================] - 2s 1ms/step - loss: 0.4996 - accuracy: 0.8550\n"],"name":"stdout"},{"output_type":"execute_result","data":{"text/plain":[""]},"metadata":{"tags":[]},"execution_count":17}]},{"cell_type":"markdown","metadata":{"id":"4mDAAPFqVVgn"},"source":["Checking the model's performance on the Validation-set."]},{"cell_type":"code","metadata":{"id":"F7dTAzgHDUh7","executionInfo":{"status":"ok","timestamp":1602355574282,"user_tz":-330,"elapsed":1651,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"1c28261b-faed-4cfe-8e8c-423379e58098","colab":{"base_uri":"https://localhost:8080/","height":50}},"source":["model.evaluate(x_test, y_test, verbose=2)"],"execution_count":null,"outputs":[{"output_type":"stream","text":["313/313 - 0s - loss: 0.5010 - accuracy: 0.8553\n"],"name":"stdout"},{"output_type":"execute_result","data":{"text/plain":["[0.5010155439376831, 0.8553000092506409]"]},"metadata":{"tags":[]},"execution_count":18}]},{"cell_type":"markdown","metadata":{"id":"T4JfEh7kvx6m"},"source":["Try to improve the accuracy to 97% by changing the number of layers and/or adding convolutions."]}]} \ No newline at end of file +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "MNIST_CNN.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "accelerator": "GPU" + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "3wF5wszaj97Y" + }, + "source": [ + "# MNIST using Neural Networks" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nnrWf3PCEzXL" + }, + "source": [ + "Run the statement to import the TensorFlow library:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "0trJmd6DjqBZ" + }, + "source": [ + "import tensorflow as tf" + ], + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7NAbSZiaoJ4z" + }, + "source": [ + "Run the statement to load and prepare the [MNIST dataset](http://yann.lecun.com/exdb/mnist/) and to convert the samples from integers to floating-point numbers:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "7FP5258xjs-v" + }, + "source": [ + "mnist = tf.keras.datasets.mnist\n", + "\n", + "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", + "x_train, x_test = x_train / 255.0, x_test / 255.0" + ], + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BPZ68wASog_I" + }, + "source": [ + "Build the model with multiple convolution, pooling, dense layers and relu activation function:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "h3IKyzTCDNGo" + }, + "source": [ + "model = tf.keras.models.Sequential([\n", + " tf.keras.layers.Reshape((28,28,1), input_shape=(28,28)),\n", + " tf.keras.layers.Conv2D(5, (5,5), activation='relu'),\n", + " tf.keras.layers.MaxPool2D((2,2)),\n", + " tf.keras.layers.Conv2D(15, (5,5), activation='relu'),\n", + " tf.keras.layers.MaxPool2D((2,2)), \n", + " tf.keras.layers.Flatten(),\n", + " tf.keras.layers.Dense(120), \n", + " tf.keras.layers.Dense(80), \n", + " tf.keras.layers.Dense(40), \n", + " tf.keras.layers.Dense(10, activation='relu')\n", + "])" + ], + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hQyugpgRIyrA" + }, + "source": [ + "Choosing our loss function, in this case crossentropy:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "RSkzdv8MD0tT" + }, + "source": [ + "loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)" + ], + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "9foNKHzTD2Vo" + }, + "source": [ + "model.compile(optimizer='adam',\n", + " loss=loss_fn,\n", + " metrics=['accuracy'])" + ], + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ix4mEL65on-w" + }, + "source": [ + "Adjusting the model parameters to minimize the loss of our model: " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "y7suUbJXVLqP", + "outputId": "f291c624-3565-4579-8975-079be1d39ee8", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 217 + } + }, + "source": [ + "model.fit(x_train, y_train, epochs=5)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Epoch 1/5\n", + "1875/1875 [==============================] - 5s 3ms/step - loss: 0.2526 - accuracy: 0.9339\n", + "Epoch 2/5\n", + "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0676 - accuracy: 0.9789\n", + "Epoch 3/5\n", + "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0552 - accuracy: 0.9828\n", + "Epoch 4/5\n", + "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0474 - accuracy: 0.9850\n", + "Epoch 5/5\n", + "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0415 - accuracy: 0.9870\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 6 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4mDAAPFqVVgn" + }, + "source": [ + "Checking the model's performance on the Validation-set." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "F7dTAzgHDUh7", + "outputId": "f97f7c5a-f919-4cbd-bfac-fa340c07f7ba", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "model.evaluate(x_test, y_test, verbose=2)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "313/313 - 1s - loss: 0.0502 - accuracy: 0.9848\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[0.05019475892186165, 0.9847999811172485]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 7 + } + ] + } + ] +} \ No newline at end of file From 3a3e7d2906cf9cc391bec43b77a8f5e50866f667 Mon Sep 17 00:00:00 2001 From: garg-saurav Date: Mon, 12 Oct 2020 08:00:18 +0530 Subject: [PATCH 2/3] Added relu and sigmoid activations in the Dense layers --- MNIST_CNN.ipynb | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/MNIST_CNN.ipynb b/MNIST_CNN.ipynb index f5d2ca2..e8eb0b2 100644 --- a/MNIST_CNN.ipynb +++ b/MNIST_CNN.ipynb @@ -88,10 +88,10 @@ " tf.keras.layers.Conv2D(15, (5,5), activation='relu'),\n", " tf.keras.layers.MaxPool2D((2,2)), \n", " tf.keras.layers.Flatten(),\n", - " tf.keras.layers.Dense(120), \n", - " tf.keras.layers.Dense(80), \n", - " tf.keras.layers.Dense(40), \n", - " tf.keras.layers.Dense(10, activation='relu')\n", + " tf.keras.layers.Dense(120, activation='relu'), \n", + " tf.keras.layers.Dense(80, activation='relu'), \n", + " tf.keras.layers.Dense(40, activation='relu'), \n", + " tf.keras.layers.Dense(10, activation='sigmoid')\n", "])" ], "execution_count": 3, @@ -143,7 +143,7 @@ "cell_type": "code", "metadata": { "id": "y7suUbJXVLqP", - "outputId": "f291c624-3565-4579-8975-079be1d39ee8", + "outputId": "679a2a7d-90f2-4827-deb6-b950793b227e", "colab": { "base_uri": "https://localhost:8080/", "height": 217 @@ -158,15 +158,15 @@ "output_type": "stream", "text": [ "Epoch 1/5\n", - "1875/1875 [==============================] - 5s 3ms/step - loss: 0.2526 - accuracy: 0.9339\n", + "1875/1875 [==============================] - 5s 2ms/step - loss: 1.5402 - accuracy: 0.9171\n", "Epoch 2/5\n", - "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0676 - accuracy: 0.9789\n", + "1875/1875 [==============================] - 4s 2ms/step - loss: 1.4864 - accuracy: 0.9688\n", "Epoch 3/5\n", - "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0552 - accuracy: 0.9828\n", + "1875/1875 [==============================] - 5s 2ms/step - loss: 1.4800 - accuracy: 0.9761\n", "Epoch 4/5\n", - "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0474 - accuracy: 0.9850\n", + "1875/1875 [==============================] - 5s 2ms/step - loss: 1.4767 - accuracy: 0.9798\n", "Epoch 5/5\n", - "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0415 - accuracy: 0.9870\n" + "1875/1875 [==============================] - 4s 2ms/step - loss: 1.4751 - accuracy: 0.9813\n" ], "name": "stdout" }, @@ -174,7 +174,7 @@ "output_type": "execute_result", "data": { "text/plain": [ - "" + "" ] }, "metadata": { @@ -197,7 +197,7 @@ "cell_type": "code", "metadata": { "id": "F7dTAzgHDUh7", - "outputId": "f97f7c5a-f919-4cbd-bfac-fa340c07f7ba", + "outputId": "8cc7a2b0-5cbd-49f1-cd9d-544a0d4e2e50", "colab": { "base_uri": "https://localhost:8080/", "height": 54 @@ -211,7 +211,7 @@ { "output_type": "stream", "text": [ - "313/313 - 1s - loss: 0.0502 - accuracy: 0.9848\n" + "313/313 - 1s - loss: 1.4754 - accuracy: 0.9819\n" ], "name": "stdout" }, @@ -219,7 +219,7 @@ "output_type": "execute_result", "data": { "text/plain": [ - "[0.05019475892186165, 0.9847999811172485]" + "[1.4754043817520142, 0.9818999767303467]" ] }, "metadata": { From 129726d8b3ca5c8a0a5720dcbd5fb98af50e3e54 Mon Sep 17 00:00:00 2001 From: Saurav Garg <50629066+garg-saurav@users.noreply.github.com> Date: Tue, 13 Oct 2020 07:15:00 +0530 Subject: [PATCH 3/3] Rename MNIST_CNN.ipynb to TASK1_SG.ipynb --- MNIST_CNN.ipynb => TASK1_SG.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename MNIST_CNN.ipynb => TASK1_SG.ipynb (99%) diff --git a/MNIST_CNN.ipynb b/TASK1_SG.ipynb similarity index 99% rename from MNIST_CNN.ipynb rename to TASK1_SG.ipynb index e8eb0b2..52d78e1 100644 --- a/MNIST_CNN.ipynb +++ b/TASK1_SG.ipynb @@ -230,4 +230,4 @@ ] } ] -} \ No newline at end of file +}