This short introduction uses Tensorflow Keras to:¶

  • Build a neural network that classifies images.
  • Train this neural network.
  • And, finally, evaluate the accuracy of the model.
In [ ]:
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in 

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import hashlib  # Standard Python library for creating cryptographic hashes (one-way fingerprints of data)
In [ ]:
# 👇️ disable tensorflow warnings
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
In [ ]:
import tensorflow as tf
print("TensorFlow version:", tf.__version__)

To Check GPU Availability in Tensorflow¶

In [ ]:
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
    print("Name:", gpu.name, "  Type:", gpu.device_type)

Listing Devices including GPU's with Tensorflow¶

In [ ]:
from tensorflow.python.client import device_lib

device_lib.list_local_devices()

To Check GPU in Tensorflow¶

In [ ]:
tf.config.list_physical_devices('GPU')

Load MNiST Dataset¶

Load and prepare the MNIST dataset. The pixel values of the images range from 0 through 255. Scale these values to a range of 0 to 1 by dividing the values by 255.0. This also converts the sample data from integers to floating-point numbers:

In [ ]:
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
In [ ]:
len(y_test)

Each image is mapped to a single label. Since the class names are not included with the dataset, store them here to use later when plotting the images:

In [ ]:
class_names = ['0', '1', '2', '3', '4',
               '5', '6', '7', '8', '9']
In [ ]:
i = 6 ##image index
from matplotlib import pyplot as plt
plt.imshow(x_train[i], interpolation='nearest')
plt.xlabel('number = ' + str(y_train[i]))
plt.show()

Build a machine learning model¶

Architecture of the Network is :-

  1. Input layer for 28x28 images in MNiST dataset

  2. Dense layer with 128 neurons and ReLU activation function

  3. Add a Dropout layer (rate = 0.2) to avoid overfitting.

  4. Output layer with 10 neurons for classification of input images as one of ten digits(0 to 9)

Task 1: Complete TODO parts with the correct parameters¶

In [ ]:
model = tf.keras.models.Sequential([
    # TODO: Add an input layer for 28x28 grayscale images
    tf.keras.Input(TODO),
    # Add a Flatten layer to convert the 2D image into a 1D vector
    tf.keras.layers.Flatten(),
    # TODO: Add a Dense hidden layer with 128 units and ReLU activation
    tf.keras.layers.Dense(TODO),
    # TODO: Add a Dropout layer with rate 0.2 to help reduce overfitting
    tf.keras.layers.Dropout(TODO),
    # TODO: Add the output Dense layer with 10 units (one per digit)
    tf.keras.layers.Dense(TODO)
])
model.summary()

Sequential is useful for stacking layers where each layer has one input tensor and one output tensor. Layers are functions with a known mathematical structure that can be reused and have trainable variables. Most TensorFlow models are composed of layers. This model uses the Flatten, Dense, and Dropout layers.

For each example, the model returns a vector of logits or log-odds scores, one for each class.

In [ ]:
predictions = model(x_train[:1]).numpy()
predictions

The tf.nn.softmax function converts these logits to probabilities for each class:

In [ ]:
tf.nn.softmax(predictions).numpy()

Creating Loss Function¶

In [ ]:
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

Compile the Model Designed Earlier¶

Before the model is ready for training, it needs a few more settings. These are added during the model's compile step:

  • Loss function This measures how accurate the model is during training. You want to minimize this function to "steer" the model in the right direction.

  • Optimizer This is how the model is updated based on the data it sees and its loss function.

  • Metrics Used to monitor the training and testing steps. The following example uses accuracy, the fraction of the images that are correctly classified.

In [ ]:
model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['accuracy'])

Train and evaluate your model¶

Use the Model.fit method to adjust your model parameters and minimize the loss:

In [ ]:
model.fit(x_train, y_train, epochs=5)

The model.evaluate method checks the models performance, usually on a "Validation-set" or "Test-set".

In [ ]:
model.evaluate(x_test,  y_test, verbose=2)
In [ ]:
probability_model = tf.keras.Sequential([model, 
                                         tf.keras.layers.Softmax()])

Here, the model has predicted the label for each image in the testing set. Let's take a look at the first prediction:

In [ ]:
predictions = probability_model.predict(x_test)
In [ ]:
predictions[0]

A prediction is an array of 10 numbers. They represent the model's "confidence" that the image corresponds to each of the 10 different numbers. You can see which label has the highest confidence value:

In [ ]:
np.argmax(predictions[0])

So, the model is most confident that this image is a number 7. Examining the test label shows that this classification is correct:

In [ ]:
y_test[0]

Define functions to graph the full set of 10 class predictions.

In [ ]:
def plot_image(i, predictions_array, true_label, img):
  true_label, img = true_label[i], img[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img, cmap=plt.cm.binary)

  predicted_label = np.argmax(predictions_array)
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'

  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
  true_label = true_label[i]
  plt.grid(False)
  plt.xticks(range(10))
  plt.yticks([])
  thisplot = plt.bar(range(10), predictions_array, color="#777777")
  plt.ylim([0, 1])
  predicted_label = np.argmax(predictions_array)

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')

Verify predictions¶

With the model trained, you can use it to make predictions about some images.

Let's look at any image, predictions, and prediction array. Correct prediction labels are blue and incorrect prediction labels are red. The number gives the percentage (out of 100) for the predicted label.

In [ ]:
i = 9
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], y_test, x_test)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i],  y_test)
plt.show()