Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Deep Learning Notes
Topic #558

Keras Sequential API

The Sequential API is Keras's simplest model-building interface — a linear stack of layers, each feeding directly into the next, directly paralleling PyTorch's nn.Sequential from PyTorch Layers.

Building a Model

from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
    layers.Input(shape=(784,)),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.3),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

model.summary()   # prints the full architecture, layer shapes, and parameter counts

Alternative: Adding Layers Incrementally

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

A CNN Example

model = keras.Sequential([
    layers.Input(shape=(32, 32, 3)),
    layers.Conv2D(32, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

Notice Keras's default input shape convention is channel-last, (height, width, channels) — the opposite of PyTorch's default channel-first (channels, height, width) convention flagged in Tensors. This is a genuinely common source of shape-mismatch confusion when porting code or intuitions between the two frameworks.

Common Mistakes

  • Forgetting to specify the input shape (via an explicit Input layer or input_shape argument on the first layer) — without it, Keras can't build the model's weight shapes until it first sees actual data, which can cause confusing downstream errors.
  • Assuming the same channel ordering as PyTorch — Keras/TensorFlow defaults to channel-last (H, W, C), unlike PyTorch's channel-first (C, H, W) default.

Interview Relevance

Q: "What's a common shape-related mistake when porting a CNN architecture from PyTorch to Keras (or vice versa)?" Forgetting the difference in default channel ordering — PyTorch defaults to channel-first (C, H, W), while TensorFlow/Keras defaults to channel-last (H, W, C). Directly reusing shape assumptions from one framework in the other without adjusting for this produces shape-mismatch errors or silently incorrect results.

Practice Question

What would the input_shape argument be for a Keras Sequential model processing 28×28 grayscale images?

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →