Simple prediction models
A feed-forward classifier or regressor with one input tensor and one output tensor.
Keras · Class
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().
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.
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 | string or None | Optional name for the model. |
A Keras Sequential Model instance.
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.
A feed-forward classifier or regressor with one input tensor and one output tensor.
A convolutional or dense pipeline where each layer feeds directly into the next.
A model assembled progressively while experimenting with layer order.
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.
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.
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.Modelkeras.InputSequential.addSequential.popModel.compileModel.fitModel.summary