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.
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:
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.
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.Sequentialis a special case oftf.keras.Model.' - Use
Sequentialfor simple linear stacks of layers. - Use Functional
Modelfor graphs with multiple inputs, outputs, branches, or shared layers. - Use subclassed
Modelfor custom forward logic. - Pick the simplest API that accurately matches the architecture.

