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

TensorFlow · Class

Sequential

tf.keras.Sequential groups an ordered, linear stack of Keras layers into a Model. It is designed for straightforward one-input, one-output layer pipelines.

How it works

Sequential creates a Model from an ordered list of layers, passing each layer's output to the next layer's input. Define the input shape explicitly or let the model infer it on first use, then compile it before training or evaluation.

Syntax

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

Parameters

NameTypeDescription
layerslist of Layer objects or NoneOptional list of layers forming the linear stack.
trainableboolWhether the model is trainable.
namestr or NoneOptional model name.

Returns

A tf.keras.Sequential model, which is a Model composed of the supplied linear stack of layers.

Key points

  • It packages an ordered, linear layer pipeline as a Keras Model.
  • The constructor accepts optional layers, a trainable flag, and a model name.
  • Layers can be appended incrementally with add().
  • Compile the model with an optimizer and loss before fitting it.
  • Training uses fit(), which accepts input data, targets, batch size, and epoch count.
  • A list of layers can be supplied directly when constructing the model.

Example: Train a simple sequential model

import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.Input(shape=(4,)),
    tf.keras.layers.Dense(8, activation="relu"),
    tf.keras.layers.Dense(1),
])
model.compile(optimizer="sgd", loss="mse")
model.fit(tf.ones((8, 4)), tf.ones((8, 1)), epochs=1, verbose=0)
print(model(tf.ones((1, 4))).shape)

Builds, compiles, trains, and calls a two-layer sequential model with an explicit input shape.

When to use it

Linear layer stacks

Create a straightforward feed-forward neural network where each layer has one input and one output.

Incremental prototyping

Assemble a model incrementally while experimenting with architecture.

Standard model workflow

Train, evaluate, and run inference on a simple Keras model.

Interview questions

QWhat problem does tf.keras.Sequential solve?

Sequential represents a linear stack of layers, with one layer feeding the next. It is suited to models where every layer has one input tensor and one output tensor.

Follow-up: When should it be replaced? Use the Functional API for multiple inputs or outputs, shared layers, or non-linear topologies.

QHow can a Sequential model be constructed?

Pass layers in a list to the constructor or create an empty model and append layers with add(). The resulting object is a Keras Model that supports compilation, training, evaluation, and inference.

Follow-up: How can the input shape be made explicit? Add tf.keras.Input as the first element or layer.

QWhen is a Sequential model built?

The model can infer its input shape when first called or trained. An explicit Input layer builds the model earlier and makes its structure available for methods such as summary().

Follow-up: Why might early building matter? It allows inspection and shape validation before data-dependent execution.

  • tf.keras.Model
  • tf.keras.Input
  • tf.keras.layers.Dense
  • tf.keras.models.clone_model

Source: tf.keras.Sequential. Checked 2026-10-15.