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

TensorFlow · Class

keras.Sequential

tf.keras.Sequential creates a Model from a linear stack of Keras layers. Layers can be supplied at construction or appended incrementally with add().

How it works

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.

Syntax

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

Parameters

NameTypeDescription
layerslist or tuple of layers, optionalA list or tuple of layers to compose into the model.
trainablebool, optionalWhether the model's variables should be trainable.
namestr, optionalAn optional name for the model.

Returns

A tf.keras.Sequential model instance.

Key points

  • The class packages a straight, ordered layer pipeline as a Keras Model.
  • The first Input declaration is optional when the model can infer its input shape later.
  • With an Input specification, adding layers progressively establishes the model's structure and weights.
  • A delayed-build model can be initialized explicitly with a batch input shape before it is called.
  • After compilation, evaluation uses the model's configured loss and metrics to report test-mode results.
  • The model can produce predictions for supplied samples through its prediction operation.

Common mistakes

  • If no input shape or Input object is provided, the model remains unbuilt and has no weights until it first receives input through a call or training/evaluation operation.
  • Use the Functional API instead when the architecture branches, merges tensors, or otherwise requires more than one input or output.

Example: Build and train a Sequential model

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.

When to use it

Linear neural networks

Build a simple feed-forward classifier or regressor whose layers form one straight chain.

End-to-end pipelines

Compose preprocessing, feature transformation, and prediction layers in a single callable model.

Incremental experimentation

Prototype architectures quickly by appending layers and inspecting the resulting model.

Interview questions

QWhat does tf.keras.Sequential represent?

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.

QHow can layers be added to a Sequential model?

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.

QWhen is a Sequential model built?

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.Model
  • tf.keras.layers.Layer
  • tf.keras.Input
  • tf.keras.Sequential.add
  • tf.keras.Sequential.build
  • tf.keras.Sequential.compile
  • tf.keras.Sequential.fit
  • tf.keras.Sequential.evaluate

Source: tf.keras.Sequential | TensorFlow v2.16.1. Checked 2026-10-15.