C.W.K.
Stream
Lesson 03 of 07 · published

Multiple Inputs and Outputs

~9 min · functional

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

When one input and one output isn't the shape of the problem

One of the Functional API's most powerful features: combining different data types and producing multiple predictions in a single model. Plenty of real problems are natively multi-input or multi-output — a support ticket has a title, a body, and tags (three different feature spaces), and you may want to predict both its priority and its routing department at once. Forcing that into a single-input, single-output model means either training three separate models or flattening everything into one vector and losing the structure.

The Code block shows the canonical shape: one branch per input, each processed by its own layers, merged with layers.concatenate, then split into one head per output. Every input and output gets a name=, which is what lets you pass data and losses as dictionaries instead of brittle positional lists.

Multi-task isn't free

Multiple heads share the trunk, so the gradients from each output flow back through the same merged features — that shared representation is the whole point (it's why multi-task models often generalize better than separate ones). But it also means the heads compete: if one loss is numerically larger, it dominates training. That's what loss_weights is for at compile time — scale each loss so no single head drowns out the rest. Start with equal weights, watch the per-output losses, and rebalance only if one stalls.

Code

Three inputs, two output heads (support-ticket classifier)·python
# Multi-input, multi-output ticket classification
title_input = keras.Input(shape=(100,), name="title")
body_input = keras.Input(shape=(500,), name="body")
tags_input = keras.Input(shape=(12,), name="tags")

# Process each branch
title_features = layers.Dense(64, activation="relu")(title_input)
body_features = layers.Dense(128, activation="relu")(body_input)
tags_features = layers.Dense(32, activation="relu")(tags_input)

# Merge branches
x = layers.concatenate([title_features, body_features, tags_features])
x = layers.Dense(128, activation="relu")(x)

# Multiple outputs
priority = layers.Dense(3, activation="softmax", name="priority")(x)
department = layers.Dense(5, activation="softmax", name="department")(x)

model = keras.Model(
    inputs=[title_input, body_input, tags_input],
    outputs=[priority, department],
)

External links

Exercise

Build a model that takes (image, age) and outputs (class_logits, regression_score). Use named inputs/outputs. Compile with two losses. Train one epoch on synthetic data.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.