Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 40 ยท Building Models with Keras

The Sequential API

Build Keras models with the Sequential API: Input, add(), summary(), naming layers, inspecting weights, when to use it, and a NumPy shape and parameter tracker.

keras.Sequential is the simplest way to build a model: a plain stack of layers where each layer has exactly one input tensor and one output tensor, and the output of one feeds the next. It covers a large share of real models (multilayer perceptrons, simple CNNs, many text classifiers) and it is the right starting point. This lesson shows how to build, inspect and modify Sequential models, and then re-creates the shape and parameter bookkeeping in NumPy so that summary() output holds no surprises.

Two ways to build one

Pass a list of layers:

Python
import keras
from keras import layers

model = keras.Sequential([
    keras.Input(shape=(784,)),
    layers.Dense(128, activation="relu"),
    layers.Dense(64, activation="relu"),
    layers.Dense(10, activation="softmax"),
], name="digit_classifier")

Or start empty and add layers one by one, which is handy inside loops and conditionals:

Python
model = keras.Sequential(name="digit_classifier")
model.add(keras.Input(shape=(784,)))
model.add(layers.Dense(128, activation="relu"))
model.add(layers.Dense(64, activation="relu"))
model.add(layers.Dense(10, activation="softmax"))

model.pop()          # remove the last layer if you want to swap the head

keras.Input(shape=...) declares the shape of one example, without the batch axis. With it, the model is built immediately: weights exist, summary() works and shapes are known. Without it, the model is built on the first call with real data. Putting an Input first is the recommended style; it fails early if you get a shape wrong.

Reading summary()

Python
model.summary()

prints a table of layers, output shapes and parameter counts. For the model above:

TEXT
Layer (type)        Output Shape    Param #
dense (Dense)       (None, 128)     100,480
dense_1 (Dense)     (None, 64)      8,256
dense_2 (Dense)     (None, 10)      650

Total params: 109,386

The numbers are fixed by the architecture: 784*128 + 128 = 100,480, 128*64 + 64 = 8,256, 64*10 + 10 = 650. The summary also splits trainable from non-trainable parameters, which matters once you freeze layers. The format of the printed box differs between Keras versions, but the information is the same.

None in (None, 128) is the batch axis. The model accepts any batch size.

A shape and parameter tracker (NumPy stand-in)

Since TensorFlow cannot run in the browser, here is a tracker that does what summary() does for Dense stacks: it follows the shape through each layer and counts parameters. Change the architecture and check your expectations before ever running Keras.

Python Stand-in: a summary() for Dense stacks (NumPy)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You should see 109,386 for the first model, matching the Keras summary above.

Selecting and inspecting layers

Python
model.layers                      # list of layer objects
model.layers[0].name              # 'dense'
model.get_layer("dense_1")        # by name
model.input_shape                 # (None, 784)
model.output_shape                # (None, 10)

for layer in model.layers:
    print(layer.name, [w.shape for w in layer.weights])
# dense   [(784, 128), (128,)]
# dense_1 [(128, 64), (64,)]
# dense_2 [(64, 10), (10,)]

weights, biases = model.layers[0].get_weights()   # NumPy arrays

Giving layers explicit name= values makes summary() and saved models easier to read, and lets you look layers up with get_layer. Names must be unique within a model.

Calling a model like a function

A built model is callable on a NumPy array or tensor. model(x) runs one forward pass directly and returns a tensor; model.predict(x) batches the input, runs in inference mode and returns NumPy arrays. Use predict for large datasets and model(x) for small, quick calls inside other code.

Python
import numpy as np

x = np.random.rand(5, 784).astype("float32")
probs = model(x)               # tensor (5, 10)
probs = model.predict(x)       # ndarray (5, 10), with a progress bar
print(probs.sum(axis=1))       # each row sums to about 1.0 (softmax)

A forward pass in NumPy

The forward pass of that exact architecture is a loop over weight matrices. Here it is with random weights; the point is to confirm shapes and the softmax property.

Python Stand-in: forward pass of a 784-128-64-10 stack (NumPy)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Input data shapes and Flatten

Dense expects (batch, features). Images arrive as (batch, 28, 28) or (batch, 28, 28, 1), so you start with a Flatten layer (or Reshape):

Python
model = keras.Sequential([
    keras.Input(shape=(28, 28)),
    layers.Flatten(),                       # (None, 784)
    layers.Dense(128, activation="relu"),
    layers.Dense(10),
])

Flatten has no parameters. It only reshapes.

Common Sequential mistakes

  • Wrong Input shape: giving shape=(None, 784) or shape=(32, 784). The shape excludes the batch axis.
  • Forgetting that Sequential needs one input and one output: no branches, merges or skips. Use the Functional API for those.
  • Reusing a layer object in two models or two places: it shares weights. Create new layer objects.
  • Last layer mismatch: 10 classes need Dense(10), not Dense(1).
  • Mixing up softmax and from_logits (Activation Functions).

When to leave Sequential

Move to the Functional API when a model needs multiple inputs (an image plus metadata), multiple outputs, skip connections (ResNet-style), shared layers or non-linear topologies. Sequential models can be nested as layers inside Functional ones, so nothing is wasted.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does keras.Input(shape=(784,)) describe?

    Input shapes exclude the batch axis; the batch size appears as None in summaries.
  2. 2. What is the total parameter count of Sequential with Input(20), Dense(32), Dense(1)?

    (20x32 + 32) + (32x1 + 1) = 672 + 33 = 705.
  3. 3. Which model needs the Functional API rather than Sequential?

    Sequential supports a single input and a single output in a straight line.
  4. 4. Does Flatten have trainable parameters?

    Flatten has no weights; it changes the shape from (batch, h, w) to (batch, h*w).

Summary

  • Sequential stacks layers with one input and one output; start it with keras.Input so it builds immediately.
  • summary() shows output shapes and parameters, which you can predict by hand: inputs x units + units.
  • Inspect with model.layers, get_layer, weights and get_weights().
  • model(x) is a direct call; model.predict(x) handles batching for large data.
  • Use the Functional API for branches, merges and multiple inputs or outputs.

Next: The Functional API