keras
sequential models
multiple outputs
deep learning
machine learning

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.

Practice ML system design

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:

text
shared layers -> output A
             -> output B

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.

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(20,), name="features")
4x = tf.keras.layers.Dense(64, activation="relu")(inputs)
5x = tf.keras.layers.Dense(32, activation="relu")(x)
6
7class_output = tf.keras.layers.Dense(3, activation="softmax", name="class_output")(x)
8score_output = tf.keras.layers.Dense(1, name="score_output")(x)
9
10model = tf.keras.Model(
11    inputs=inputs,
12    outputs=[class_output, score_output]
13)
14
15model.compile(
16    optimizer="adam",
17    loss={
18        "class_output": "sparse_categorical_crossentropy",
19        "score_output": "mse",
20    },
21    metrics={
22        "class_output": ["accuracy"],
23        "score_output": ["mae"],
24    },
25)
26
27features = tf.random.normal((64, 20))
28class_labels = tf.random.uniform((64,), minval=0, maxval=3, dtype=tf.int32)
29scores = tf.random.normal((64, 1))
30
31model.fit(
32    features,
33    {"class_output": class_labels, "score_output": scores},
34    epochs=2,
35    verbose=0,
36)

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.

python
1import tensorflow as tf
2
3backbone = tf.keras.Sequential([
4    tf.keras.layers.Dense(64, activation="relu"),
5    tf.keras.layers.Dense(32, activation="relu"),
6], name="backbone")
7
8inputs = tf.keras.Input(shape=(20,))
9features = backbone(inputs)
10
11class_output = tf.keras.layers.Dense(3, activation="softmax", name="class_output")(features)
12score_output = tf.keras.layers.Dense(1, name="score_output")(features)
13
14model = tf.keras.Model(inputs=inputs, outputs=[class_output, score_output])

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.

python
1model.compile(
2    optimizer="adam",
3    loss={
4        "class_output": "sparse_categorical_crossentropy",
5        "score_output": "mse",
6    },
7    loss_weights={
8        "class_output": 1.0,
9        "score_output": 0.2,
10    },
11)

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 Sequential models are not the right API for true multi-output architectures
  • Use the Functional API to branch shared features into multiple output heads
  • A Sequential block 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 Sequential to Model(inputs, outputs)

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.