Linear neural networks
Build a simple feed-forward classifier or regressor whose layers form one straight chain.
TensorFlow · Class
tf.keras.Sequential creates a Model from a linear stack of Keras layers. Layers can be supplied at construction or appended incrementally with add().
Sequential represents a model whose computation is a linear chain of layers: each layer receives the previous layer's output. Supplying an Input layer builds the model as layers are added; without one, weights are created when the model first receives input or is explicitly built.
tf.keras.Sequential(layers=None, trainable=True, name=None)| Name | Type | Description |
|---|---|---|
layers | list or tuple of layers, optional | A list or tuple of layers to compose into the model. |
trainable | bool, optional | Whether the model's variables should be trainable. |
name | str, optional | An optional name for the model. |
A tf.keras.Sequential model instance.
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.Input(shape=(16,)),
tf.keras.layers.Dense(8, activation="relu"),
tf.keras.layers.Dense(1),
])
model.compile(optimizer="sgd", loss="mse")
x = tf.zeros((4, 16))
y = tf.zeros((4, 1))
model.fit(x, y, epochs=1, verbose=0)
Creates a two-layer feed-forward model with an explicit input specification, compiles it, and trains it briefly on dummy data.
Build a simple feed-forward classifier or regressor whose layers form one straight chain.
Compose preprocessing, feature transformation, and prediction layers in a single callable model.
Prototype architectures quickly by appending layers and inspecting the resulting model.
It is a Model that composes layers in a linear stack. The output of one layer is passed directly as the input to the next.
Follow-up: When should it not be used? Use the Functional API for graphs with multiple inputs, multiple outputs, or non-linear connectivity.
Pass a list or tuple of layer instances to the constructor, or create an empty model and append layers with add(). An Input object can be placed first to establish the input shape.
Follow-up: How can a delayed-build model be initialized explicitly? Call build() with the batch input shape before training or inference.
An explicit Input causes the model to build as layers are added. Without it, the model is built later when it is called with data or used by a training/evaluation method.
Follow-up: Why prefer an Input object over putting input_shape on the first layer? It separates input specification from the layer stack and lets the model build continuously.
tf.keras.Modeltf.keras.layers.Layertf.keras.Inputtf.keras.Sequential.addtf.keras.Sequential.buildtf.keras.Sequential.compiletf.keras.Sequential.fittf.keras.Sequential.evaluate