Plain layer stacks
A straightforward feed-forward network with one input and one output.
Keras · Class
Keras Sequential builds a Model from a linear stack of layers. It suits straightforward single-input, single-output architectures and supports training and inference.
Sequential constructs a model by arranging layers in one linear chain. Provide layers at creation time or append them with add(); an explicit Input builds the model as layers are added, while omitting it delays weight creation until the model is first called, trained, evaluated, or predicted.
keras.Sequential(layers=None, trainable=True, name=None)| Name | Type | Description |
|---|---|---|
layers | list of layers or None | Optional ordered list of layer instances forming the stack. |
trainable | bool | Whether the model is trainable. |
name | str or None | Optional name assigned to the model. |
A Keras Model representing the sequential layer stack.
import keras
from keras import layers
model = keras.Sequential([
layers.Input(shape=(16,)),
layers.Dense(8, activation="relu"),
layers.Dense(1)
])
model.compile(optimizer="sgd", loss="mse")
Creates a single-input, single-output stack and explicitly defines its input shape before compiling it.
A straightforward feed-forward network with one input and one output.
A model whose layers are known before construction.
Incrementally assembling and editing a stack during experimentation.
Sequential is designed for a linear stack in which each layer has one input tensor and one output tensor. It provides the model's training and inference features through the resulting Model.
Follow-up: When should the Functional API be preferred? Prefer it when the architecture is not a plain stack, such as when it requires multiple inputs, multiple outputs, or branches.
The constructor accepts an optional list of layers, and layers can also be appended with add(). An explicit Input lets the model build continuously as layers are added; otherwise building is delayed.
Follow-up: How can delayed construction be completed manually? Call build(batch_input_shape) after adding layers without an input shape.
add() places a layer on top of the current stack. It rejects objects that are not layers, layers whose input shape is unknown, layers with multiple outputs, and layers already connected elsewhere.
Follow-up: What operation reverses add()? Use pop() to remove the last layer; the current API can rebuild the model after removal.
ModelFunctional APIadd methodpop methodInput layerfit methodevaluate methodpredict method