Multiple outputs in keras Sequential models
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
If you need a model with multiple outputs in Keras, the important answer is that a plain Sequential model is usually the wrong tool. The Sequential API is designed for a single linear stack, while multi-output models require branching, which is exactly what the Functional API is built for.
Why Sequential Does Not Fit
Keras Sequential is appropriate when every layer has one input tensor and one output tensor in a simple chain. A true multi-output model breaks that assumption because one shared representation has to branch into two or more heads.
That branching is not what Sequential is meant to describe.
So if your mental model is:
you should switch to the Functional API.
What a Real Multi-Output Model Looks Like
A common example is multitask learning, where one network predicts both a class label and a regression value from the same shared features.
This is the standard Keras solution. The model has one input, shared hidden layers, and two output heads.
Where Sequential Still Helps
A Sequential model can still be useful as a reusable subnetwork inside a larger Functional model.
In that design, Sequential handles the linear shared stack, while the Functional API handles the branching.
Multiple Losses and Loss Weights
Multi-output models usually need multiple losses, and sometimes those losses should not contribute equally.
This matters because one output can dominate training if its loss scale is much larger than the others.
Returning Multiple Values from One Final Layer Is Not the Same Thing
Sometimes people try to fake multiple outputs by returning one tensor that contains several values. That can work if the outputs are truly just one combined vector, but it is not the same as a Keras multi-output model with named heads, distinct losses, and separate metrics.
If the outputs have different meanings or training objectives, separate heads are clearer and more maintainable.
Common Pitfalls
The biggest pitfall is trying to force branching behavior into Sequential. That usually leads to awkward hacks or a model that is not actually multi-output in the Keras sense.
Another pitfall is forgetting to name the output layers. Named outputs make compile and fit calls much easier to read.
A third pitfall is ignoring loss scaling between tasks. Multi-output models can train badly if one objective overwhelms the others.
Finally, do not assume the Functional API is "advanced for no reason." In Keras, it is the normal solution for architectures that are not just one straight stack.
Summary
- Plain
Sequentialmodels are not the right API for true multi-output architectures - Use the Functional API to branch shared features into multiple output heads
- A
Sequentialblock can still be reused as a linear subnetwork inside a Functional model - Multi-output models usually need separate losses, metrics, and sometimes loss weights
- If the network branches, switch mental models from
SequentialtoModel(inputs, outputs)
Related reading
- Multiple sessions and graphs in Tensorflow in the same process
- Multitask deep learning with Tensorflow
- Multivariate LSTM with missing values
- My LSTM learns, loss decreases, but Numerical Gradients don''t match Analytical Gradients
- Multiple parameter servers are not sharing the load when running TensorFlow distributed
- Multiple sessions and graphs in Tensorflow in the same process
- Multiple pipelines that merge within a sklearn Pipeline?
- Multiple traces on Polynomial Regression Graph
.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.