🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

tf.keras.Sequential()

AI & DATA SCIENCE // tf-keras-sequential

tf.keras.Sequential() builds a neural network model as a simple linear stack of layers, where each layer has exactly one input tensor and one output tensor.

Syntax

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

Deep Dive Course

A Sequential model is the simplest way to build a Keras model — you pass it a list of layers, and data flows through them in order, each layer's output becoming the next layer's input. It only supports this simple linear topology; models with multiple inputs, multiple outputs, shared layers, or non-linear connections between layers, like a residual/skip connection, require the more flexible Functional API instead, built with tf.keras.Model().

1Understanding tf.keras.Sequential()

A Sequential model is the simplest way to build a Keras model — you pass it a list of layers, and data flows through them in order, each layer's output becoming the next layer's input. It only supports this simple linear topology; models with multiple inputs, multiple outputs, shared layers, or non-linear connections between layers, like a residual/skip connection, require the more flexible Functional API instead, built with tf.keras.Model().

💡

Reach for Sequential() only when your architecture really is a single, unbranching stack of layers — the moment you need multiple inputs/outputs or a skip connection, switch to the Functional API with tf.keras.Model() instead of trying to force it into a Sequential model.

editor.html
import tensorflow as tf
from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=(32,)),
    layers.Dense(10, activation='softmax')
])
print(model.output_shape)
localhost:3000

2Practical Example

Here is a real-world application of tf.keras.Sequential() showing how it is used in production TensorFlow code.

editor.html
import tensorflow as tf
from tensorflow.keras import layers

model = tf.keras.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape=(32,)))
model.add(layers.Dense(10, activation='softmax'))
print(len(model.layers))
localhost:3000

3Best Practices

Follow these guidelines when working with tf.keras.Sequential():

1. Use Sequential() for simple, single-path architectures, and the Functional API for anything with multiple inputs, multiple outputs, or non-linear connections between layers

2. Specify an explicit input shape on the first layer, or via an Input layer, so the model can build its weights immediately, rather than waiting until the first call with real data

3. Add layers with model.add() incrementally when building a model conditionally or in a loop, instead of always passing the full list to the constructor at once

⚠️

Tip: Reach for Sequential() only when your architecture really is a single, unbranching stack of layers — the moment you need multiple inputs/outputs or a skip connection, switch to the Functional API with tf.keras.Model() instead of trying to force it into a Sequential model.

editor.html
import tensorflow as tf
from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=(32,)),
    layers.Dense(10, activation='softmax')
])
print(model.output_shape)
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf
from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=(32,)),
    layers.Dense(10, activation='softmax')
])
print(model.output_shape)
Example 02Advanced Example
import tensorflow as tf
from tensorflow.keras import layers

model = tf.keras.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape=(32,)))
model.add(layers.Dense(10, activation='softmax'))
print(len(model.layers))

Best Practices

  • Use Sequential() for simple, single-path architectures, and the Functional API for anything with multiple inputs, multiple outputs, or non-linear connections between layers
  • Specify an explicit input shape on the first layer, or via an Input layer, so the model can build its weights immediately, rather than waiting until the first call with real data
  • Add layers with model.add() incrementally when building a model conditionally or in a loop, instead of always passing the full list to the constructor at once

Interview Question

Why can't you build a model with a residual/skip connection using tf.keras.Sequential()?

Hint: Think about what topology Sequential() is actually able to represent.

Sequential() only supports a single, linear chain of layers, where each layer has exactly one input coming from the previous layer's single output — there's no mechanism to route a layer's output to more than one destination, or to combine two different layers' outputs back together, both of which are exactly what a residual/skip connection requires, since it needs an earlier layer's output to be added back in further downstream, alongside the main path's output. Representing that kind of branching, merging topology instead requires the Functional API, built with tf.keras.Model(), where each layer is called explicitly as a function on a tensor, letting you freely route outputs to multiple places and merge multiple tensors together with layers like Add or Concatenate.

Exercises

MediumPractice using tf.keras.Sequential() in a real scenario.
View Solution
import tensorflow as tf
from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=(32,)),
    layers.Dense(10, activation='softmax')
])
print(model.output_shape)

Frequently Asked Questions

Why can't you build a model with a residual/skip connection using tf.keras.Sequential()?

Sequential() only supports a single, linear chain of layers, where each layer has exactly one input coming from the previous layer's single output — there's no mechanism to route a layer's output to more than one destination, or to combine two different layers' outputs back together, both of which are exactly what a residual/skip connection requires, since it needs an earlier layer's output to be added back in further downstream, alongside the main path's output. Representing that kind of branching, merging topology instead requires the Functional API, built with tf.keras.Model(), where each layer is called explicitly as a function on a tensor, letting you freely route outputs to multiple places and merge multiple tensors together with layers like Add or Concatenate.

Related Functions

tf-keras-modelmodel-compiledense-layer