Keras
machine learning
neural networks
model chaining
conditional processing

Keras conditional passing one model output to another model

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

In Keras, one model can feed another model directly because models are callable layers in the Functional API. The harder part is when the output of one stage decides which downstream computation should matter, because symbolic tensors do not behave like ordinary Python values.

Direct Model Chaining Is Simple

If by "passing one model output to another" you only mean ordinary chaining, Keras already supports that cleanly. You can build a feature extractor and then call a second model on the extracted features.

python
1import tensorflow as tf
2from tensorflow import keras
3from tensorflow.keras import layers
4
5inputs = keras.Input(shape=(20,))
6x = layers.Dense(32, activation="relu")(inputs)
7x = layers.Dense(16, activation="relu")(x)
8encoder = keras.Model(inputs, x, name="encoder")
9
10classifier_inputs = keras.Input(shape=(16,))
11classifier_outputs = layers.Dense(3, activation="softmax")(classifier_inputs)
12classifier = keras.Model(classifier_inputs, classifier_outputs, name="classifier")
13
14model_inputs = keras.Input(shape=(20,))
15features = encoder(model_inputs)
16predictions = classifier(features)
17
18model = keras.Model(model_inputs, predictions)
19model.summary()

This is model composition, not conditional routing. It is often all you need.

Conditional Routing Needs a Gate

If the first stage decides which downstream branch should influence the final answer, the usual pattern is to build a gating output and then combine branch results in the graph.

The key design choice is that both branches should produce compatible output shapes. Then a gate can weight or select between them.

python
1import tensorflow as tf
2from tensorflow import keras
3from tensorflow.keras import layers
4
5inputs = keras.Input(shape=(8,))
6shared = layers.Dense(16, activation="relu")(inputs)
7
8gate = layers.Dense(1, activation="sigmoid", name="gate")(shared)
9
10expert_a = keras.Sequential(
11    [
12        layers.Dense(8, activation="relu"),
13        layers.Dense(1),
14    ],
15    name="expert_a",
16)
17
18expert_b = keras.Sequential(
19    [
20        layers.Dense(8, activation="relu"),
21        layers.Dense(1),
22    ],
23    name="expert_b",
24)
25
26a_out = expert_a(shared)
27b_out = expert_b(shared)
28
29inverse_gate = layers.Lambda(lambda g: 1.0 - g)(gate)
30weighted_a = layers.Multiply()([gate, a_out])
31weighted_b = layers.Multiply()([inverse_gate, b_out])
32final_output = layers.Add()([weighted_a, weighted_b])
33
34model = keras.Model(inputs, final_output)
35model.compile(optimizer="adam", loss="mse")

This is a soft conditional path. When gate is close to 1.0, the model favors expert_a. When it is close to 0.0, it favors expert_b. Because the gate is continuous, training remains differentiable.

Why a Python if Usually Fails

A common first attempt looks like this:

python
1if gate > 0.5:
2    output = expert_a(shared)
3else:
4    output = expert_b(shared)

That is the wrong mental model inside the Functional API. gate is a symbolic tensor, not a normal Python boolean, so a plain if cannot decide graph structure at model-build time.

When people say they want "conditional passing" in Keras, they usually mean one of these two things:

  • Direct composition, where model A feeds model B
  • Routed composition, where a gating value controls how much each branch contributes

Most real projects are solved by the second pattern, not by Python control flow.

Hard Routing Is Possible, but It Changes the Tradeoff

Sometimes you want a hard choice instead of a soft mixture. One option is to use a TensorFlow op such as tf.where after both branches have produced outputs:

python
hard_mask = tf.cast(gate > 0.5, tf.float32)
output = tf.where(hard_mask > 0, a_out, b_out)

This makes the final prediction behave more like a switch, but it still computes both branches first. That is often acceptable, especially when the branches are small.

True "run only one branch" behavior is more advanced. It usually requires subclassed models, backend-specific control flow, and careful handling of batch shapes. If you are still prototyping the architecture, soft gating is usually easier to train and debug.

Keep the Interface Between Models Clean

Whether you chain models directly or route between them, the handoff between stages should be explicit:

  • Match tensor shapes
  • Match numeric scale and activation expectations
  • Give branch outputs the same dimensionality if they will be merged

For example, if one branch ends in a sigmoid scalar and the other ends in a vector, you cannot combine them with Add or tf.where without reshaping or redesigning the outputs. Most conditional-model bugs are shape bugs, not Keras bugs.

Common Pitfalls

  • Trying to use a Python if on a Keras tensor.
  • Building branches whose output shapes do not match, then trying to combine them.
  • Using a hard threshold too early, which can make optimization unstable.
  • Assuming the "unused" branch is free. In many graph-based designs, both branches still run.
  • Forgetting that plain model chaining is already supported and does not need special conditional logic.

Summary

  • In Keras, one model can feed another model directly because models are callable in the Functional API.
  • Conditional routing usually means adding a gate and combining compatible branch outputs.
  • Soft gating is easier to train than a hard threshold because gradients flow more smoothly.
  • A Python if does not work on symbolic tensors inside the model graph.
  • When debugging conditional models, check tensor shapes and branch compatibility before anything else.

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.