What is the difference between tf.keras.model and tf.keras.sequential?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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.
Related reading
- What is the difference between the trainable_weights and trainable_variables in the tensorflow basic lstm_cell?
- What is the difference between these two ways of saving keras machine learning model weights?
- What is the difference between UpSampling2D and Conv2DTranspose functions in keras?
- What is the difference between virtual batch normalization and batch normalization?
- What is the difference between variable_scope and name_scope?
- What is the difference in installing tensorflow with pip command and conda or directing cloning?
- What is the difference between the train loss and train error?
- what is the difference between 'transform' and 'fit_transform' in sklearn
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.