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

TensorFlow is a machine learning framework provided by Google. It is an open-source framework used with Python to implement algorithms, deep learning applications and much more. It is used in research and production environments.

The Fashion MNIST dataset contains grayscale images of clothing items from 10 different categories. It includes over 70,000 low-resolution images (28 x 28 pixels) of various clothing types like shirts, shoes, bags, and more.

Installing TensorFlow

Install TensorFlow using pip ?

pip install tensorflow

Building the Model

Let's create a sequential neural network model for Fashion MNIST classification ?

import tensorflow as tf

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

print("Sequential model is being built")

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

print("Sequential model is being compiled")

# Display model architecture
model.summary()
Sequential model is being built
Sequential model is being compiled
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 flatten (Flatten)           (None, 784)               0         
                                                                 
 dense (Dense)               (None, 128)               100480    
                                                                 
 dense_1 (Dense)             (None, 10)                1290      
                                                                 
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________

Model Architecture Explanation

The model consists of three layers:

  • Flatten Layer: Transforms 2D images (28x28) into 1D arrays of 784 pixels. This layer has no learnable parameters.

  • Dense Layer (128 neurons): Fully connected layer with 128 neurons using ReLU activation function for non-linearity.

  • Output Layer (10 neurons): Final layer with 10 neurons representing the 10 clothing categories. Returns raw logits (unnormalized scores).

Training the Model

To complete the implementation, you would load the Fashion MNIST dataset and train the model ?

# Load and preprocess the Fashion MNIST dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()

# Normalize pixel values to range [0, 1]
x_train, x_test = x_train / 255.0, x_test / 255.0

# Train the model
model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))

# Evaluate the model
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test accuracy: {test_accuracy:.4f}")

Conclusion

This sequential model architecture is effective for Fashion MNIST classification. The Flatten layer converts 2D images to 1D, the Dense layer with ReLU activation learns feature representations, and the final layer outputs class probabilities for the 10 clothing categories.

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

299 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements