TensorFlow Sequential Model

The Sequential model is the simplest way to build a neural network in TensorFlow. It arranges layers in a straight line where data flows from the first layer to the last without branching. Most classification and regression problems — from spam detection to house price prediction — fit perfectly into this single-file architecture. The Sequential model handles everything automatically: layer connections, shape inference, and forward-pass computation.

The Train Analogy

A Sequential model works exactly like a train on a single track. Each train car is a layer. Passengers (data) board at the first car, pass through every car in order, and exit at the last car as predictions. There are no branch tracks, no passengers jumping between cars — strict sequential flow from start to finish.

Three Ways to Build a Sequential Model

Method 1 — Pass a List of Layers

import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Dense(128, activation='relu', input_shape=(784,)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

Method 2 — Add Layers One at a Time

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(128, activation='relu', input_shape=(784,)))
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

Both methods produce identical models. Method 2 is useful when you build the architecture programmatically, for example inside a loop that adds layers based on a configuration list.

Method 3 — Named Layers

model = tf.keras.Sequential([
    tf.keras.layers.Dense(128, activation='relu', name='hidden1',
                          input_shape=(784,)),
    tf.keras.layers.Dense(64, activation='relu', name='hidden2'),
    tf.keras.layers.Dense(10, activation='softmax', name='output')
])

Naming layers makes model summaries and debugging easier, especially when you need to retrieve specific layers by name later.

How Sequential Infers Shapes Automatically

When you specify input_shape on the first layer, TensorFlow calculates the output shape of every subsequent layer automatically. You only need to specify input shape once.

Diagram — Automatic Shape Propagation:

Input: (None, 784)
         │
[Dense(128, relu)]
  in: (None, 784)    out: (None, 128)
         │
[Dense(64, relu)]
  in: (None, 128)    out: (None, 64)
         │
[Dense(10, softmax)]
  in: (None, 64)     out: (None, 10)
         │
Output: (None, 10)

"None" = flexible batch size (any number of samples)

Printing the Model Summary

model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)           Output Shape        Param #
=================================================================
 hidden1 (Dense)        (None, 128)         100,480
 hidden2 (Dense)        (None, 64)          8,256
 output (Dense)         (None, 10)          650
=================================================================
Total params: 109,386
Trainable params: 109,386
Non-trainable params: 0
_________________________________________________________________

The parameter count for the first Dense layer: 784 inputs × 128 neurons = 100,352 weight values, plus 128 bias values = 100,480 total. These are all the numbers TensorFlow adjusts during training.

Accessing Individual Layers

# Access by index
first_layer = model.layers[0]
print(first_layer.name)    # hidden1
print(first_layer.output_shape)  # (None, 128)

# Access by name
output_layer = model.get_layer('output')
print(output_layer.units)  # 10

# Retrieve weights from a specific layer
weights, biases = model.layers[0].get_weights()
print(weights.shape)  # (784, 128)
print(biases.shape)   # (128,)

Removing and Inserting Layers

# Remove the last layer
model.pop()
print(len(model.layers))   # 2 now

# Add a replacement
model.add(tf.keras.layers.Dense(5, activation='softmax', name='new_output'))

The input_shape Parameter in Detail

The input_shape parameter defines the shape of one single sample (not the batch). TensorFlow adds the batch dimension automatically as None.

# Tabular data: 20 features per sample
tf.keras.layers.Dense(64, input_shape=(20,))
# Full shape: (None, 20)

# Grayscale images 28×28 pixels
tf.keras.layers.Conv2D(32, 3, input_shape=(28, 28, 1))
# Full shape: (None, 28, 28, 1)

# Color images 224×224 pixels
tf.keras.layers.Conv2D(64, 3, input_shape=(224, 224, 3))
# Full shape: (None, 224, 224, 3)

# Sequences of 100 time steps with 10 features each
tf.keras.layers.LSTM(64, input_shape=(100, 10))
# Full shape: (None, 100, 10)

Running Data Through the Model

import numpy as np

# Create dummy data — 5 samples, 784 features each
x = np.random.random((5, 784)).astype('float32')

# Get predictions (forward pass)
predictions = model(x)
print(predictions.shape)   # (5, 10)
print(predictions[0])      # 10 probabilities for sample 0

# Or use predict() for large datasets (handles batching automatically)
predictions = model.predict(x, batch_size=32)

When Sequential Is Enough

Problem Type                      Sequential Sufficient?
──────────────────────────────────────────────────────────
Binary classification (yes/no)    Yes
Multi-class classification        Yes
Regression (predict a number)     Yes
Image classification (CNN)        Yes
Sentiment analysis (LSTM/GRU)     Yes
Multi-input models                No — use Functional API
Multi-output models               No — use Functional API
Residual connections (ResNet)     No — use Functional API
Siamese networks                  No — use Functional API
──────────────────────────────────────────────────────────

A Complete Example: Classifying Fashion Items

import tensorflow as tf

# Load Fashion-MNIST dataset (clothing images 28×28 grayscale)
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()

# Normalize pixel values to 0–1
x_train = x_train.astype('float32') / 255.0
x_test  = x_test.astype('float32') / 255.0

# Flatten 28×28 images to 784-length vectors
x_train = x_train.reshape(-1, 784)
x_test  = x_test.reshape(-1, 784)

# Build model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(256, activation='relu', input_shape=(784,)),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

model.fit(x_train, y_train, epochs=15, batch_size=128,
          validation_split=0.1)

test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test accuracy: {test_acc:.2%}")

The Sequential model keeps your code clear and organized. It handles all the plumbing between layers so you focus on architecture decisions: how many layers, how many neurons, which activations. The next topic examines Dense layers — the workhorse layer type that appears in almost every model — in full detail.

Leave a Comment

Your email address will not be published. Required fields are marked *