C.W.K.
Stream
Lesson 02 of 06 · published

Sequential API — The Linear Stack

~11 min · sequential, dense, dropout

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

The simplest model that does real work

The Sequential API is a list of layers, each feeding into the next. It's the right choice for most standard feedforward and convolutional networks — and it's enough for 60% of real-world classification tasks.

Always declare the Input shape explicitly. With keras.Input(shape=(...)) as the first entry, the model is fully built immediately — you can call model.summary() and see parameter counts right away. Without it, the model is lazily built on first forward pass, which delays error detection.

When NOT to use Sequential: any time you need branches (skip connections, residual blocks), shared layers (siamese networks), multiple inputs (image + metadata), or multiple outputs (multi-task heads). For those, jump to the Functional API.

Code

Sequential 두 가지 패턴·python
import tensorflow as tf
from tensorflow import keras
from keras import layers

# Pattern 1: list to constructor (recommended)
model = keras.Sequential([
    keras.Input(shape=(784,)),
    layers.Dense(256, activation='relu'),
    layers.Dropout(0.3),
    layers.Dense(128, activation='relu'),
    layers.BatchNormalization(),
    layers.Dense(10),    # logits, no softmax (use from_logits=True in loss)
], name="mnist_classifier")

# Pattern 2: incremental .add()
model2 = keras.Sequential(name="mnist_v2")
model2.add(keras.Input(shape=(784,)))
model2.add(layers.Dense(256, activation='relu'))
model2.add(layers.Dense(10))

model.summary()
print(model.layers)               # list of Layer objects
print(model.get_layer('dense'))    # by name
model2.pop()                       # remove last layer

Exercise

Build a Sequential MLP for MNIST: Input(784) → Dense(256, relu) → Dropout(0.3) → Dense(128, relu) → Dense(10). Print model.summary() and confirm the parameter count is around 235K.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.