Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

keras.Sequential

keras.Sequential creates a Keras Model from an ordered linear stack of layers. It supports construction from a layer list or incremental assembly with add().

How it works

Sequential represents a model whose computation is a linear chain of layers. You can provide the layers at construction time or add them incrementally; declaring an Input builds the model as layers are added, while omitting it delays building until input data or a build-related operation supplies the shape.

Syntax

keras.Sequential(layers=None, trainable=True, name=None)

Parameters

NameTypeDescription
layerslist of layers or NoneOptional ordered list of layer instances forming the stack.
trainableboolWhether the model is trainable.
namestring or NoneOptional name for the model.

Returns

A Keras Sequential Model instance.

Key points

  • The class packages a linear layer stack as a Keras Model.
  • Choose it when the architecture is a plain sequential stack rather than a general computation graph.
  • Every layer in the supported topology has one input tensor and one output tensor.
  • The constructor accepts an initial ordered list of layer instances.
  • The model exposes its contained layers through the layers attribute.
  • Use add(layer) to append another layer to the end of the stack.
  • Use pop() to remove the final layer.
  • Calling build(batch_input_shape) lets you explicitly initialize a model using delayed construction.

Common mistakes

  • Without an input shape, the model has no weights immediately after construction and is built only when called or trained.
  • Delayed construction can be made explicit with build(), or it will occur on the first training, evaluation, prediction, or data call.

Example: Build a Sequential model

import keras

model = keras.Sequential([
    keras.Input(shape=(16,)),
    keras.layers.Dense(8, activation="relu"),
    keras.layers.Dense(1),
])
model.compile(optimizer="adam", loss="mse")
model.summary()

Creates a two-layer single-input, single-output model, explicitly defines its input shape, then configures it for training.

When to use it

Simple prediction models

A feed-forward classifier or regressor with one input tensor and one output tensor.

Linear feature pipelines

A convolutional or dense pipeline where each layer feeds directly into the next.

Incremental prototyping

A model assembled progressively while experimenting with layer order.

Interview questions

QWhat problem does keras.Sequential solve?

Sequential is a Model specialized for a linear stack of layers, where each layer receives one tensor and returns one tensor. It is useful when the network has a straightforward feed-forward topology.

Follow-up: What should replace it for branching or multiple-input architectures? Use the Functional API, which can express graphs with shared, merged, branched, or multiple input and output tensors.

QHow can layers be supplied to a Sequential model?

Pass a list of layers to the constructor, or instantiate an empty model and call add() for each layer. Supplying keras.Input first makes the input shape explicit and builds the model while layers are appended.

Follow-up: How can a delayed-build model be initialized before training? Call build(batch_input_shape) after adding layers, then inspect or summarize the resulting model.

QWhen does a Sequential model become built?

If no input shape is specified, weights are created when the model first receives compatible input or when a build, training, evaluation, or prediction operation determines the shape. An explicit Input avoids this delayed-build state.

Follow-up: Why might explicit Input be preferable? It makes the model's expected input shape visible immediately and allows the model structure to be inspected before data is passed through it.

  • keras.Model
  • keras.Input
  • Sequential.add
  • Sequential.pop
  • Model.compile
  • Model.fit
  • Model.summary

Source: The Sequential class | Keras. Checked 2026-10-15.