How can TensorFlow be used to make predictions for Fashion MNIST dataset in Python?

PythonServer Side ProgrammingProgramming

Tensorflow is a machine learning framework that is provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes. It has optimization techniques that help in performing complicated mathematical operations quickly. This is because it uses NumPy and multi−dimensional arrays. These multi−dimensional arrays are also known as ‘tensors’.

The ‘tensorflow’ package can be installed on Windows using the below line of code −

pip install tensorflow

Tensor is a data structure used in TensorFlow. It helps connect edges in a flow diagram. This flow diagram is known as the ‘Data flow graph’. Tensors are nothing but multidimensional array or a list.

The ‘Fashion MNIST’ dataset contains images of clothing of different kinds. It contains grayscale images of more than 70 thousand clothes that belong to 10 different categories. These images are of low resolution (28 x 28 pixels).

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

Following is the code snippet to make predictions −

Example

probability_model = tf.keras.Sequential([model,
                                         tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
print("The predictions are being made ")
print(predictions[0])

np.argmax(predictions[0])
print("The test labels are")
print(test_labels[0])
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(‘green’)

Code credithttps://www.tensorflow.org/tutorials/keras/classification

Output

The predictions are being made
[1.3008227e−07 9.4930819e−10 2.0181861e−09 5.4944155e−10 3.8257373e−11
1.3896286e−04 1.4776078e−08 3.1724274e−03 9.4210514e−11 9.9668854e−01]
The test labels are
9

Explanation

  • Once the model has been trained, it needs to be tested.

  • This can be done by using the built model to make predictions about images.

  • The linear output, logits and a softmax layers is attached to it.

  • The softmax layer helps in the conversion of logits to probabilities.

  • This is done so that it is easier to interpret the predictions made.

  • The ‘plot_value_array’ method is defined which displays the actual values and the predicted values.

raja
Published on 20-Jan-2021 12:29:34
Advertisements