From bf6f68dcccdaa65d8ec6261f0513f6ba1549e61c Mon Sep 17 00:00:00 2001 From: Saubhagya Bal <53002928+its-sbal@users.noreply.github.com> Date: Fri, 4 Feb 2022 13:11:57 +0530 Subject: [PATCH 1/2] Created using Colaboratory --- mnist_practice_for_book.ipynb | 437 ++++++++++++++++++++++++++++++++++ 1 file changed, 437 insertions(+) create mode 100644 mnist_practice_for_book.ipynb diff --git a/mnist_practice_for_book.ipynb b/mnist_practice_for_book.ipynb new file mode 100644 index 0000000..5f0d11a --- /dev/null +++ b/mnist_practice_for_book.ipynb @@ -0,0 +1,437 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.3" + }, + "colab": { + "name": "mnist practice for book.ipynb", + "provenance": [], + "collapsed_sections": [], + "include_colab_link": true + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nPuPWHif5BIM" + }, + "source": [ + "#
Single Layer Neural Network" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0Bgr_pbo5BIT" + }, + "source": [ + "In this tutorial, we are going to create a single layer neural network. Then we will classify the digits in the MNIST dataset." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UgzOQyVC5BIU" + }, + "source": [ + "### Import Tensorflow and Keras" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VWPOByL-5BIV" + }, + "source": [ + "#import libraries.\n", + "import keras\n", + "import tensorflow as tf" + ], + "execution_count": 13, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hJZg1aeO5BIb" + }, + "source": [ + "### Read in the data\n", + "\n", + "Keras comes pre-loaded with a collection of datasets. MNIST is one of them. So we will load it directly from Keras." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "BqJRnfbX5BId" + }, + "source": [ + "mnist=tf.keras.datasets.mnist" + ], + "execution_count": 14, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dm8wpiQg5BIj" + }, + "source": [ + "Split the data into a training set and test set" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "QnHWpaHf5BIk" + }, + "source": [ + "(x_train, y_train), (x_test, y_test) = mnist.load_data()" + ], + "execution_count": 15, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gckjMpfT5BIn" + }, + "source": [ + "## Data Processing" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "U9xyNe1c5BIp" + }, + "source": [ + "### Flatten Images" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "O2DT2ICP5BIq" + }, + "source": [ + "We turn the images into one dimensional images for convenience." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "GGiTMLMz5BIr" + }, + "source": [ + "x_train, x_test = x_train / 255.0, x_test / 255.0" + ], + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7sF2azfq5BIy" + }, + "source": [ + "### Create the Neural Network" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Lapj6MDy5BIz" + }, + "source": [ + "Create the neural network with a single hidden layer." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "T77rpiOO5BI0" + }, + "source": [ + "digit = tf.keras.models.Sequential([\n", + " tf.keras.layers.Flatten(input_shape=(28, 28)),\n", + " tf.keras.layers.Dense(10, activation='softmax')\n", + "])" + ], + "execution_count": 17, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oZCsExwh5BI4" + }, + "source": [ + "Now we can define the optomizer, loss and metrics." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "DUFYoqFh5BI5" + }, + "source": [ + "digit.compile(optimizer='adam',\n", + " loss='sparse_categorical_crossentropy',\n", + " metrics=['accuracy'])\n" + ], + "execution_count": 18, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1JpkeNE85BI8" + }, + "source": [ + "### Train the Model" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Topvfi185BI_", + "outputId": "0fec2620-7158-46d1-9fbd-85bf2eb1ba87", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "digit.fit(x_train, y_train, epochs=3)" + ], + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Epoch 1/3\n", + "1875/1875 [==============================] - 4s 2ms/step - loss: 0.4705 - accuracy: 0.8782\n", + "Epoch 2/3\n", + "1875/1875 [==============================] - 3s 2ms/step - loss: 0.3037 - accuracy: 0.9147\n", + "Epoch 3/3\n", + "1875/1875 [==============================] - 3s 2ms/step - loss: 0.2832 - accuracy: 0.9212\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 19 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lgBjmvuz5BJE" + }, + "source": [ + "### Test the Model" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tG4s2aJE5BJF", + "outputId": "34ad865c-9209-4b99-cb05-bc2aa77e9fec", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "digit.evaluate(x_test, y_test)" + ], + "execution_count": 20, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "313/313 [==============================] - 1s 2ms/step - loss: 0.2756 - accuracy: 0.9233\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[0.27559980750083923, 0.92330002784729]" + ] + }, + "metadata": {}, + "execution_count": 20 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### Using the Model to Predict" + ], + "metadata": { + "id": "fsiqlbQAnZGt" + } + }, + { + "cell_type": "markdown", + "source": [ + "### Import libraries" + ], + "metadata": { + "id": "ah6B3Cn_nz0t" + } + }, + { + "cell_type": "code", + "metadata": { + "id": "WYj_PeSY5cnE" + }, + "source": [ + "import numpy as np\n", + "import matplotlib.pyplot as plt" + ], + "execution_count": 21, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "Storing predictions of entire Test Data" + ], + "metadata": { + "id": "Qav4kPONoEy9" + } + }, + { + "cell_type": "code", + "source": [ + "predictions = digit.predict([x_test])" + ], + "metadata": { + "id": "tg-nkeBnodCe" + }, + "execution_count": 22, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "Now we check the predicted value of an image" + ], + "metadata": { + "id": "5dpnA-5GopfE" + } + }, + { + "cell_type": "code", + "source": [ + "np.argmax(predictions[12])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "FGillVo1oc9z", + "outputId": "8ace7444-7872-432c-d04a-87746e2130f6" + }, + "execution_count": 23, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "9" + ] + }, + "metadata": {}, + "execution_count": 23 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Let's check whether the Prediction matches the Image" + ], + "metadata": { + "id": "UfMjcMDio1fc" + } + }, + { + "cell_type": "code", + "source": [ + "plt.imshow(x_test[12])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 284 + }, + "id": "UAvZZfiToc5U", + "outputId": "4379a07b-5b2c-4468-ac9f-6ea0356db158" + }, + "execution_count": 24, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 24 + }, + { + "output_type": "display_data", + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm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q5L6utnMKOpO491Jx00z3jP7rpHpz5vFB3RvdXVEXC7pI5I+XZ2u9qQYfg/WS2OnY5rGu1NGmWb857q57xqd/rxZ3Qj7bkkzRzy+sFrWEyJid3W7T9IK9d5U1HuPzaBb3e7rcj8/10vTeI82zbh6YN91c/rzboT9aUkX277I9gRJN0ha1YU+3sL2lOqDE9meIunD6r2pqFdJWlTdXyRpZRd7+QW9Mo13rWnG1eV91/XpzyOi43+S5mv4E/kXJd3RjR5q9PUOST+o/rZ0uzdJD2n4tO6whj/buFHSdElrJD0v6T8lTeuh3r4laZOkjRoO1owu9Xa1hk/RN0raUP3N7/a+K/TVkf3G5bJAEnxAByRB2IEkCDuQBGEHkiDsQBKEHUiCsANJ/D/6oRA5FH+mPgAAAABJRU5ErkJggg==\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + } + } + ] + } + ] +} \ No newline at end of file From 529480250b121d3a7d4e0414f418fe5702a098d8 Mon Sep 17 00:00:00 2001 From: Saubhagya Bal <53002928+its-sbal@users.noreply.github.com> Date: Fri, 4 Feb 2022 13:47:39 +0530 Subject: [PATCH 2/2] Delete mnist practice for book.ipynb --- mnist practice for book.ipynb | 1 - 1 file changed, 1 deletion(-) delete mode 100644 mnist practice for book.ipynb diff --git a/mnist practice for book.ipynb b/mnist practice for book.ipynb deleted file mode 100644 index 717e583..0000000 --- a/mnist practice for book.ipynb +++ /dev/null @@ -1 +0,0 @@ -{"nbformat":4,"nbformat_minor":0,"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.3"},"colab":{"name":"mnist practice for book.ipynb","provenance":[],"collapsed_sections":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"nPuPWHif5BIM","colab_type":"text"},"source":["#
Single Layer Neural Network"]},{"cell_type":"markdown","metadata":{"id":"0Bgr_pbo5BIT","colab_type":"text"},"source":["In this tutorial, we are going to create a single layer neural network. Then we will classify the digits in the MNIST dataset."]},{"cell_type":"markdown","metadata":{"id":"UgzOQyVC5BIU","colab_type":"text"},"source":["### Import Tensorflow and Keras"]},{"cell_type":"code","metadata":{"id":"VWPOByL-5BIV","colab_type":"code","outputId":"6cb7926a-b9a6-472e-e7fa-16672372a5c8","executionInfo":{"status":"ok","timestamp":1581787702023,"user_tz":-330,"elapsed":2647,"user":{"displayName":"Vinita Silaparasetty","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mDFxxWWpOLHxO4yJm7murUsknVM89G3bMZ6SX9B5A=s64","userId":"10666020967003457465"}},"colab":{"base_uri":"https://localhost:8080/","height":80}},"source":["#import libraries.\n","import keras\n","import tensorflow as tf"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Using TensorFlow backend.\n"],"name":"stderr"},{"output_type":"display_data","data":{"text/html":["

\n","The default version of TensorFlow in Colab will soon switch to TensorFlow 2.x.
\n","We recommend you upgrade now \n","or ensure your notebook will continue to use TensorFlow 1.x via the %tensorflow_version 1.x magic:\n","more info.

\n"],"text/plain":[""]},"metadata":{"tags":[]}}]},{"cell_type":"markdown","metadata":{"id":"hJZg1aeO5BIb","colab_type":"text"},"source":["### Read in the data\n","\n","Keras comes pre-loaded with a collection of datasets. MNIST is one of them. So we will load it directly from Keras."]},{"cell_type":"code","metadata":{"id":"BqJRnfbX5BId","colab_type":"code","colab":{}},"source":["mnist=tf.keras.datasets.mnist"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"dm8wpiQg5BIj","colab_type":"text"},"source":["Split the data into a training set and test set"]},{"cell_type":"code","metadata":{"id":"QnHWpaHf5BIk","colab_type":"code","outputId":"788844f1-8fd6-4baf-b076-dd36ed11e20f","executionInfo":{"status":"ok","timestamp":1581787714926,"user_tz":-330,"elapsed":6541,"user":{"displayName":"Vinita Silaparasetty","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mDFxxWWpOLHxO4yJm7murUsknVM89G3bMZ6SX9B5A=s64","userId":"10666020967003457465"}},"colab":{"base_uri":"https://localhost:8080/","height":51}},"source":["(x_train, y_train), (x_test, y_test) = mnist.load_data()"],"execution_count":0,"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":"gckjMpfT5BIn","colab_type":"text"},"source":["## Data Processing"]},{"cell_type":"markdown","metadata":{"id":"U9xyNe1c5BIp","colab_type":"text"},"source":["### Flatten Images"]},{"cell_type":"markdown","metadata":{"id":"O2DT2ICP5BIq","colab_type":"text"},"source":["We turn the images into one dimensional images for convenience."]},{"cell_type":"code","metadata":{"id":"GGiTMLMz5BIr","colab_type":"code","colab":{}},"source":["x_train, x_test = x_train / 255.0, x_test / 255.0"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"7sF2azfq5BIy","colab_type":"text"},"source":["### Create the Neural Network"]},{"cell_type":"markdown","metadata":{"id":"Lapj6MDy5BIz","colab_type":"text"},"source":["Create the neural network with a single hidden layer."]},{"cell_type":"code","metadata":{"id":"T77rpiOO5BI0","colab_type":"code","outputId":"a49b5c83-78c9-49d3-c0e5-25d1a213dae9","executionInfo":{"status":"ok","timestamp":1581787737877,"user_tz":-330,"elapsed":1049,"user":{"displayName":"Vinita Silaparasetty","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mDFxxWWpOLHxO4yJm7murUsknVM89G3bMZ6SX9B5A=s64","userId":"10666020967003457465"}},"colab":{"base_uri":"https://localhost:8080/","height":88}},"source":["digit = tf.keras.models.Sequential([\n"," tf.keras.layers.Flatten(input_shape=(28, 28)),\n"," tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n","])"],"execution_count":0,"outputs":[{"output_type":"stream","text":["WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.\n","Instructions for updating:\n","If using Keras pass *_constraint arguments to layers.\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"oZCsExwh5BI4","colab_type":"text"},"source":["Now we can define the optomizer, loss and metrics."]},{"cell_type":"code","metadata":{"id":"DUFYoqFh5BI5","colab_type":"code","colab":{}},"source":["digit.compile(optimizer='adam',\n"," loss='sparse_categorical_crossentropy',\n"," metrics=['accuracy'])\n"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"1JpkeNE85BI8","colab_type":"text"},"source":["### Train the Model"]},{"cell_type":"code","metadata":{"id":"Topvfi185BI_","colab_type":"code","outputId":"caf56561-bc06-4b6d-c4cf-bccbfd106849","executionInfo":{"status":"ok","timestamp":1581787756411,"user_tz":-330,"elapsed":9517,"user":{"displayName":"Vinita Silaparasetty","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mDFxxWWpOLHxO4yJm7murUsknVM89G3bMZ6SX9B5A=s64","userId":"10666020967003457465"}},"colab":{"base_uri":"https://localhost:8080/","height":153}},"source":["digit.fit(x_train, y_train, epochs=3)"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Train on 60000 samples\n","Epoch 1/3\n","60000/60000 [==============================] - 3s 47us/sample - loss: 0.4670 - acc: 0.8788\n","Epoch 2/3\n","60000/60000 [==============================] - 3s 44us/sample - loss: 0.3036 - acc: 0.9151\n","Epoch 3/3\n","60000/60000 [==============================] - 3s 45us/sample - loss: 0.2834 - acc: 0.9204\n"],"name":"stdout"},{"output_type":"execute_result","data":{"text/plain":[""]},"metadata":{"tags":[]},"execution_count":7}]},{"cell_type":"markdown","metadata":{"id":"lgBjmvuz5BJE","colab_type":"text"},"source":["### Test the Model"]},{"cell_type":"code","metadata":{"id":"tG4s2aJE5BJF","colab_type":"code","outputId":"1721529d-6d68-4def-f0f2-614373c065f9","executionInfo":{"status":"ok","timestamp":1581787762012,"user_tz":-330,"elapsed":735,"user":{"displayName":"Vinita Silaparasetty","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mDFxxWWpOLHxO4yJm7murUsknVM89G3bMZ6SX9B5A=s64","userId":"10666020967003457465"}},"colab":{"base_uri":"https://localhost:8080/","height":51}},"source":["digit.evaluate(x_test, y_test)"],"execution_count":0,"outputs":[{"output_type":"stream","text":["10000/10000 [==============================] - 0s 26us/sample - loss: 0.2765 - acc: 0.9229\n"],"name":"stdout"},{"output_type":"execute_result","data":{"text/plain":["[0.2764906617820263, 0.9229]"]},"metadata":{"tags":[]},"execution_count":8}]},{"cell_type":"code","metadata":{"id":"WYj_PeSY5cnE","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file