diff --git a/MNIST_CNN.ipynb b/MNIST_CNN.ipynb deleted file mode 100644 index d102888..0000000 --- a/MNIST_CNN.ipynb +++ /dev/null @@ -1 +0,0 @@ -{"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 diff --git a/TASK1_SG.ipynb b/TASK1_SG.ipynb new file mode 100644 index 0000000..52d78e1 --- /dev/null +++ b/TASK1_SG.ipynb @@ -0,0 +1,233 @@ +{ + "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, 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, + "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": "679a2a7d-90f2-4827-deb6-b950793b227e", + "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 2ms/step - loss: 1.5402 - accuracy: 0.9171\n", + "Epoch 2/5\n", + "1875/1875 [==============================] - 4s 2ms/step - loss: 1.4864 - accuracy: 0.9688\n", + "Epoch 3/5\n", + "1875/1875 [==============================] - 5s 2ms/step - loss: 1.4800 - accuracy: 0.9761\n", + "Epoch 4/5\n", + "1875/1875 [==============================] - 5s 2ms/step - loss: 1.4767 - accuracy: 0.9798\n", + "Epoch 5/5\n", + "1875/1875 [==============================] - 4s 2ms/step - loss: 1.4751 - accuracy: 0.9813\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": "8cc7a2b0-5cbd-49f1-cd9d-544a0d4e2e50", + "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: 1.4754 - accuracy: 0.9819\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1.4754043817520142, 0.9818999767303467]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 7 + } + ] + } + ] +}