multi-output neural network
regression and classification
neural network architecture
machine learning
deep learning

Multi-output neural network combining regression and classification

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

A multi-output neural network can solve more than one prediction task from the same input. A common pattern is to share an early feature extractor and then branch into separate heads, one for regression and one for classification.

This setup is useful when both tasks depend on the same underlying signal. Instead of training two separate models, you let one network learn shared representations and then optimize task-specific outputs with different losses.

Shared Trunk, Separate Heads

The standard architecture has:

  • a shared input and hidden stack
  • a regression head for continuous values
  • a classification head for class probabilities or logits

In Keras, the functional API is the natural fit.

python
1import keras
2from keras import layers
3
4inputs = keras.Input(shape=(10,), name="features")
5x = layers.Dense(64, activation="relu")(inputs)
6x = layers.Dense(32, activation="relu")(x)
7
8regression_output = layers.Dense(1, name="price")(x)
9classification_output = layers.Dense(3, activation="softmax", name="category")(x)
10
11model = keras.Model(
12    inputs=inputs,
13    outputs=[regression_output, classification_output],
14)
15
16model.summary()

The shared layers learn features useful to both tasks, while each head learns task-specific mappings from those features.

Compile with Separate Losses and Metrics

The model must be compiled with one loss per output.

python
1model.compile(
2    optimizer="adam",
3    loss={
4        "price": "mse",
5        "category": "sparse_categorical_crossentropy",
6    },
7    metrics={
8        "price": ["mae"],
9        "category": ["accuracy"],
10    },
11)

This is a key point: regression and classification have different objective functions. Trying to force them into one shared loss without separate heads usually makes the design harder and less correct.

Train with Named Targets

At training time, supply one target per output.

python
1import numpy as np
2
3x_train = np.random.randn(200, 10).astype("float32")
4y_price = np.random.randn(200, 1).astype("float32")
5y_category = np.random.randint(0, 3, size=(200,))
6
7model.fit(
8    x_train,
9    {
10        "price": y_price,
11        "category": y_category,
12    },
13    epochs=3,
14    batch_size=16,
15)

Using named outputs keeps the mapping explicit and prevents confusion once the model becomes more complex.

Loss Weighting Matters

One task can dominate the total loss if its numerical scale is much larger than the other. Keras lets you adjust this with loss_weights.

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

This is often necessary in mixed-task models because regression losses and classification losses do not naturally live on the same numeric scale.

Why Multi-Output Can Help

A shared model can help when the tasks reinforce each other. For example:

  • an image may have both a class label and a numeric severity score
  • a product may have a category and an estimated price
  • a customer record may need both churn probability and lifetime value prediction

If the tasks are related, shared representation learning can improve data efficiency and regularization.

When Separate Models Are Better

A single multi-output model is not always the best choice. If the tasks are unrelated or pull the shared layers in incompatible directions, one task can hurt the other.

This is especially likely when:

  • labels have very different quality
  • one task is much harder than the other
  • the tasks use different relevant features
  • the loss scales are poorly balanced

In those cases, separate models may be simpler and more effective.

Common Pitfalls

One common mistake is forgetting that the regression head and classification head need different loss functions. A single generic loss is rarely correct for both.

Another issue is mismatching target shapes. For example, a softmax classification head with sparse labels expects a different target shape from a one-hot classification head.

It is also easy to ignore loss weighting. If one head dominates the total loss numerically, the other task may barely train.

Finally, do not assume shared layers always help. Multi-task learning works best when the tasks are genuinely related.

Summary

  • A multi-output network can combine regression and classification by using a shared trunk with separate heads.
  • Each head should usually have its own loss and metrics.
  • Training data must provide one target per output.
  • Loss weighting is often important so one task does not dominate optimization.
  • Multi-output models work best when the tasks share useful underlying structure.

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.