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

Adding Layers

~9 min · sequential

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

The four layers you'll reach for first

A Sequential model is only as expressive as the layers you stack into it, and for the bulk of everyday work four core layers carry the load:

  • Dense(units, activation) — fully connected layer, the workhorse. Every input connects to every output; units is how many outputs it produces.
  • Dropout(rate) — randomly zeroes a fraction of its inputs during training only to fight overfitting. At inference it's a no-op, so you never remove it for prediction.
  • Flatten() — collapses a multi-dimensional input into 1D (a 28×28 image becomes a 784-vector) so a Dense layer, which only speaks in vectors, can consume it.
  • Activation(fn) — applies a nonlinearity. You'll rarely use it standalone; the idiom is to fold it into the layer with Dense(64, activation="relu").

The first layer is special: it needs a shape

Every layer after the first infers its input shape from the layer before it — but the first layer has nothing to infer from, so you have to tell it. The clean way is to make keras.Input(shape=(...)) the first element of the stack. Until Keras knows that shape it can't allocate weights, which is why a model with no declared input stays "unbuilt" and summary() complains. Declaring it up front means your model is fully built the instant it's constructed, before a single batch of data shows up (the Code section shows the full stack).

Code

A 4-layer stack with shapes annotated·python
model = keras.Sequential([
    keras.Input(shape=(28, 28, 1)),  # 28x28 grayscale images
    layers.Flatten(),                       # → (784,)
    layers.Dense(128, activation="relu"),   # → (128,)
    layers.Dropout(0.2),                   # Regularization
    layers.Dense(10, activation="softmax"), # → (10,) probabilities
])

External links

Exercise

Build the same MNIST classifier two ways: once with constructor list, once with model.add(). Verify both produce identical model.summary().

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.