๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #368

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?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Keras Sequential API โ€“ FAQs

Quick answers about learning Keras Sequential API in Deep Learning.

This free note from CodingNow 2.0 explains Keras Sequential API in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Keras Sequential API, is 100% free with no signup required.
With focused practice, most students grasp Keras Sequential API in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now