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

TensorFlow is a machine learning framework 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 arrays or lists.

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).

Complete Fashion MNIST Model Training

Here's a complete example showing how to load, preprocess, and train a neural network on the Fashion MNIST dataset ?

import tensorflow as tf
from tensorflow import keras
import numpy as np

# Load the Fashion MNIST dataset
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

# Class names for Fashion MNIST
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

# Normalize pixel values to range [0, 1]
train_images = train_images / 255.0
test_images = test_images / 255.0

# Build the model
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10)
])

# Compile the model
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

print("The model is fit to the data")
model.fit(train_images, train_labels, epochs=15)

print("The accuracy is being computed")
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\nThe test accuracy is :', test_acc)

Output

The model is fit to the data
Epoch 1/15
1875/1875 [==============================] - 4s 2ms/step - loss: 0.6337 - accuracy: 0.7799
Epoch 2/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3806 - accuracy: 0.8622
Epoch 3/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3469 - accuracy: 0.8738
Epoch 4/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3131 - accuracy: 0.8853
Epoch 5/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2962 - accuracy: 0.8918
Epoch 6/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2875 - accuracy: 0.8935
Epoch 7/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2705 - accuracy: 0.8998
Epoch 8/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2569 - accuracy: 0.9023
Epoch 9/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2465 - accuracy: 0.9060
Epoch 10/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2440 - accuracy: 0.9088
Epoch 11/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2300 - accuracy: 0.9143
Epoch 12/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2255 - accuracy: 0.9152
Epoch 13/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2114 - accuracy: 0.9203
Epoch 14/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2101 - accuracy: 0.9211
Epoch 15/15
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2057 - accuracy: 0.9224
The accuracy is being computed
313/313 - 0s - loss: 0.3528 - accuracy: 0.8806

The test accuracy is : 0.8805999755859375

Model Architecture Explanation

The neural network consists of three layers ?

  • Flatten layer ? Transforms 2D image arrays (28x28 pixels) into 1D arrays (784 pixels)

  • Dense layer ? Fully connected layer with 128 neurons and ReLU activation function

  • Output layer ? 10 neurons for 10 clothing categories, returns raw prediction scores

Training Process

  • The model is trained by feeding training data and building a model. The 'train_images' and 'train_labels' are arrays of input data.

  • The model learns to map images with respective labels through multiple epochs.

  • The 'test_images' stores the test data for final evaluation.

  • The 'model.fit' method trains the model on the training dataset for 15 epochs.

  • The 'model.evaluate' function gives the accuracy and loss on the test dataset.

Key Parameters

  • Optimizer ? Adam optimizer for efficient gradient descent

  • Loss function ? SparseCategoricalCrossentropy for multi-class classification

  • Metrics ? Accuracy to monitor model performance

  • Epochs ? 15 training iterations through the entire dataset

Conclusion

TensorFlow with Keras provides an easy way to build and train neural networks for image classification. The Fashion MNIST model achieves approximately 88% accuracy on test data, demonstrating effective learning of clothing image patterns.

Updated on: 2026-03-25T15:34:56+05:30

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements