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