How can Tensorflow be used to create a sequential model using Python?

A sequential model in TensorFlow can be created using the Keras Sequential API, which stacks layers linearly where each layer has exactly one input and one output tensor. This is ideal for building straightforward neural networks like convolutional neural networks (CNNs).

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

Creating a Sequential CNN Model

Let's create a sequential model for image classification with convolutional and dense layers ?

import tensorflow as tf
from tensorflow.keras import layers, Sequential

print("Sequential model is being created")

# Define image dimensions and number of classes
img_height, img_width = 180, 180
num_classes = 5

model = Sequential([
    layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
    layers.Conv2D(16, 3, padding='same', activation='relu'),
    layers.MaxPooling2D(),
    layers.Conv2D(32, 3, padding='same', activation='relu'),
    layers.MaxPooling2D(),
    layers.Conv2D(64, 3, padding='same', activation='relu'),
    layers.MaxPooling2D(),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(num_classes)
])

print("Model created successfully!")
print(f"Total layers: {len(model.layers)}")

The output of the above code is ?

Sequential model is being created
Model created successfully!
Total layers: 9

Model Architecture Breakdown

This sequential model consists of the following components:

  • Rescaling Layer: Normalizes pixel values from [0,255] to [0,1] range
  • Three Convolution Blocks: Each contains a Conv2D layer followed by MaxPooling2D
  • Flatten Layer: Converts 2D feature maps to 1D vector
  • Dense Layers: Fully connected layers with 128 units (ReLU) and output layer

Alternative Creation Methods

You can also create a sequential model by adding layers incrementally ?

import tensorflow as tf
from tensorflow.keras import layers, Sequential

# Alternative method: adding layers one by one
model = Sequential()
model.add(layers.Rescaling(1./255, input_shape=(180, 180, 3)))
model.add(layers.Conv2D(16, 3, padding='same', activation='relu'))
model.add(layers.MaxPooling2D())
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(5))

print("Sequential model created using add() method")
print(model.summary())

Key Features of Sequential Models

Feature Description Best Use Case
Linear Stack Layers connected sequentially Simple feedforward networks
Easy to Build Straightforward API Prototyping and learning
Single Input/Output Each layer has one input, one output Standard CNN/RNN architectures

Conclusion

TensorFlow's Sequential API provides a simple way to create neural networks by stacking layers linearly. Use it for straightforward architectures like CNNs for image classification where layers flow sequentially from input to output.

---
Updated on: 2026-03-25T16:12:51+05:30

211 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements