tensorflow
keras
deep learning
machine learning
model comparison

What is the difference between tf.keras.model and tf.keras.sequential?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The important relationship is that tf.keras.Sequential is a special case of tf.keras.Model. Use Sequential when the model is just a straight stack of layers. Use Model when you need a graph with branching, multiple inputs or outputs, shared layers, or fully custom behavior.

Sequential Means Linear Stack

Sequential is the simplest Keras API. Each layer feeds directly into the next one, with one input path and one output path.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(8, activation="relu"),
6    tf.keras.layers.Dense(3, activation="softmax")
7])
8
9model.compile(optimizer="adam", loss="sparse_categorical_crossentropy")
10print(model.summary())

This is ideal for ordinary feedforward models where the topology is just input -> layer -> layer -> output.

Model Covers More Than One Pattern

tf.keras.Model is the base class. You normally use it in one of two ways:

  • through the Functional API
  • by subclassing Model

The Functional API creates a graph of layers. TensorFlow’s guide describes it as more flexible than Sequential and able to handle non-linear topology, shared layers, and multiple inputs or outputs.

Example with two inputs:

python
1import tensorflow as tf
2
3left = tf.keras.Input(shape=(8,), name="left")
4right = tf.keras.Input(shape=(8,), name="right")
5combined = tf.keras.layers.Concatenate()([left, right])
6x = tf.keras.layers.Dense(16, activation="relu")(combined)
7output = tf.keras.layers.Dense(1, name="score")(x)
8
9model = tf.keras.Model(inputs=[left, right], outputs=output)
10model.compile(optimizer="adam", loss="mse")

That cannot be expressed naturally with Sequential because there are two inputs and a merge step.

Subclassing Model

If you need custom forward logic, subclass tf.keras.Model directly.

python
1import tensorflow as tf
2
3class MyModel(tf.keras.Model):
4    def __init__(self):
5        super().__init__()
6        self.dense1 = tf.keras.layers.Dense(8, activation="relu")
7        self.dense2 = tf.keras.layers.Dense(1)
8
9    def call(self, inputs):
10        x = self.dense1(inputs)
11        return self.dense2(x)
12
13model = MyModel()

Subclassing is useful for research models or custom control flow, but it trades away some of the simplicity and graph visibility of the Functional API.

How to Choose

Use Sequential when:

  • the model is a plain stack of layers
  • there is one input and one output path
  • you want the clearest minimal code

Use Functional Model when:

  • layers branch or merge
  • layers are shared
  • the model has multiple inputs or outputs
  • you want a graph representation of the architecture

Use subclassed Model when:

  • the architecture has custom Python logic that does not fit the graph-building APIs cleanly

A Practical Rule

If you can honestly describe the network as “a stack of layers,” use Sequential. If that sentence stops being true, move to Model.

The good news is that once built, Sequential models still support the normal Keras workflow: compile, fit, evaluate, predict, and saving.

Debugging and Inspectability

The Functional API often gives a clearer architecture view for non-trivial models because inputs and outputs are explicit graph objects. That usually makes diagrams, summaries, and intermediate wiring easier to reason about than a heavily customized subclass. That visibility is often the deciding factor in production teams.

Common Pitfalls

The most common mistake is comparing Sequential with Model as if they were unrelated APIs. Sequential is a specialized model class.

Another issue is forcing a multi-input or branching architecture into Sequential, which makes the code awkward or impossible.

A third pitfall is subclassing Model too early when the Functional API would be easier to inspect and maintain.

Summary

  • 'tf.keras.Sequential is a special case of tf.keras.Model.'
  • Use Sequential for simple linear stacks of layers.
  • Use Functional Model for graphs with multiple inputs, outputs, branches, or shared layers.
  • Use subclassed Model for custom forward logic.
  • Pick the simplest API that accurately matches the architecture.

Course illustration
Course illustration

All Rights Reserved.