How can TensorFlow be used to build the model for Fashion MNIST dataset in Python?

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.

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 build the model for Fashion MNIST dataset in Python −


model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
print("Sequential model is being built")
model.compile(optimizer='adam',  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),  metrics=['accuracy'])
print("Sequential model is being compiled")

Code credit


Sequential model is being built
Sequential model is being compiled


  • The layers in the model are configured.

  • Layer, which is the basic block of a neural network extracts representations from data which is given to the layer as input data.

  • Many simple layers are grouped together.

  • Some layers also have parameters which are tuned to reach optimal value during the training phase.

  • The first layer ‘Flatten’ transforms the images from 2D to 1D array.

  • This layer doesn’t have any parameters that need to be learnt.

  • Once the pixels are flattened, two ‘Dense’ layers are built, where every dense layer has 128 neurons. The last layer returs a logits array that has length 10.

  • Every neuron/node contains a value which is the score that tells about which class the image belongs to.

  • Then the model is compiled.