Deep Learning
Keras
Functional API
Multi-input Multi-output
Neural Networks

Multi-input Multi-output Model with Keras Functional API

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

Keras Functional API is designed for models with multiple inputs and outputs, such as systems that combine text and tabular features while predicting both category and score. This architecture is common in recommendation, ranking, and multitask learning.

This article shows how to build, train, and validate a multi-input multi-output model safely.

Core Sections

1) Define multiple inputs

python
1from tensorflow.keras import Input, Model
2from tensorflow.keras.layers import Dense, Concatenate
3
4text_in = Input(shape=(128,), name="text_features")
5meta_in = Input(shape=(16,), name="meta_features")
6
7x1 = Dense(64, activation="relu")(text_in)
8x2 = Dense(32, activation="relu")(meta_in)
9merged = Concatenate()([x1, x2])

Separate branches let each modality learn suitable representation.

2) Define multiple outputs

python
1class_out = Dense(5, activation="softmax", name="class_output")(merged)
2score_out = Dense(1, activation="linear", name="score_output")(merged)
3
4model = Model(inputs=[text_in, meta_in], outputs=[class_out, score_out])

Each output can use distinct loss and metric definitions.

3) Compile with per-output losses

python
1model.compile(
2    optimizer="adam",
3    loss={"class_output": "sparse_categorical_crossentropy", "score_output": "mse"},
4    loss_weights={"class_output": 1.0, "score_output": 0.3},
5    metrics={"class_output": ["accuracy"], "score_output": ["mae"]},
6)

Loss weighting is important so one task does not dominate training.

4) Fit with named dictionaries

python
1model.fit(
2    x={"text_features": X_text, "meta_features": X_meta},
3    y={"class_output": y_class, "score_output": y_score},
4    epochs=10,
5    batch_size=64,
6)

Named tensors reduce input-output ordering mistakes.

5) Inference and monitoring

python
pred_class, pred_score = model.predict({"text_features": X_text_val, "meta_features": X_meta_val})

Track each output metric separately; aggregate loss alone hides task-specific regressions.

6) Production checklist for multi-head Keras models

Code examples are necessary, but production readiness depends on how this pattern behaves under failure, load, and operational drift. Before rollout, define success criteria that are measurable. A useful baseline is three metrics: correctness (for example, expected output match rate), reliability (error rate and retry behavior), and latency (p95 or p99 execution time). Capture these metrics in a repeatable test environment rather than relying on ad hoc local runs. If external systems are involved, include at least one synthetic fault scenario such as timeout, malformed payload, or temporary dependency outage. This confirms the implementation fails predictably and recovers in a controlled way.

Document environment assumptions close to the code. Include runtime version constraints, required environment variables, and exact dependency versions used during validation. Many regressions come from mismatched environments rather than algorithmic changes. A short README snippet or inline comment that names these assumptions can prevent repeated troubleshooting later. Also define ownership for operational issues: who receives alerts, what threshold triggers action, and what rollback path is acceptable. Without explicit ownership and rollback criteria, otherwise small incidents can take longer to resolve.

A practical rollout sequence is:

  1. Run automated checks (lint, unit tests, static validation) in CI.
  2. Execute a smoke test against representative input sizes.
  3. Validate one failure mode and verify error visibility in logs.
  4. Deploy behind a feature flag or phased rollout if possible.
  5. Monitor key metrics for a defined stabilization window.
bash
1# Example operator workflow
2make lint
3make test
4./scripts/smoke_check.sh

Finally, keep a short limitations section. State what the current approach intentionally does not optimize or support. This prevents accidental misuse by future contributors and keeps design discussions grounded in explicit tradeoffs. For long-lived systems, schedule periodic review of this implementation, especially after runtime upgrades or library changes. A lightweight maintenance cadence often catches compatibility issues before they become production incidents.

Common Pitfalls

  • Feeding inputs in wrong order without named mappings.
  • Using incompatible label shapes per output head.
  • Forgetting loss weights and over-optimizing one objective.
  • Monitoring only total loss and missing per-head degradation.
  • Sharing too much trunk capacity for unrelated tasks.

Summary

Keras Functional API makes multi-input multi-output models straightforward when branches, outputs, and losses are explicitly defined. Use named tensors, per-output metrics, and balanced loss weights to keep multitask training stable and interpretable.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the 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