When a model has multiple inputs and outputs, each output can represent a different task. So, we need to measure performance separately for each output. For example, if one output predicts a category (classification), accuracy or F1-score matters. If another output predicts a number (regression), mean squared error (MSE) matters. This helps us understand how well the model does on each task.
Multi-input and multi-output models in TensorFlow - Model Metrics & Evaluation
Start learning this pattern below
Jump into concepts and practice - no test required
For classification outputs, a confusion matrix shows how many predictions were correct or wrong for each class. For example, if one output predicts "cat" or "dog":
| Predicted Cat | Predicted Dog |
|--------------|---------------|
| True Cat: 50 | False Dog: 3 |
| False Cat: 5 | True Dog: 42 |
This helps calculate precision, recall, and F1 for that output. For regression outputs, we look at error values like MSE instead.
Imagine a multi-output model where one output detects spam emails (classification) and another predicts email length (regression). For spam detection, high precision means few good emails are wrongly marked as spam. High recall means most spam emails are caught. Depending on what matters more, we adjust the model or threshold.
For the length prediction output, we focus on minimizing error, not precision or recall.
Good metrics mean each output performs well on its task. For classification outputs, precision and recall above 0.8 are usually good. For regression outputs, low MSE or MAE (mean absolute error) is good.
Bad metrics are low precision or recall (below 0.5) for classification, or very high error for regression. This means the model struggles on that output.
- Ignoring some outputs: Only checking metrics for one output hides problems in others.
- Mixing metric types: Using accuracy for regression outputs or MSE for classification is wrong.
- Data leakage: If inputs share information that leaks target info, metrics look too good.
- Overfitting: High training metrics but poor validation metrics on any output means overfitting.
Your multi-output model has 98% accuracy on one classification output but only 12% recall on detecting fraud in another output. Is it good for production? Why or why not?
Answer: No, it is not good. Even though accuracy is high on one output, the very low recall on fraud detection means the model misses most fraud cases. For fraud detection, recall is critical because missing fraud is costly. So, the model needs improvement on that output before production.
Practice
Solution
Step 1: Understand multi-input models
Multi-input models are designed to take multiple data sources as inputs simultaneously.Step 2: Differentiate from multi-output models
Multi-output models predict multiple outputs but usually from a single input source.Final Answer:
To accept more than one data source at the same time -> Option AQuick Check:
Multi-input = multiple data sources [OK]
- Confusing multi-input with multi-output
- Thinking multi-input reduces layers
- Assuming multi-input speeds training automatically
Solution
Step 1: Recall how to define multiple inputs
Multiple inputs should be stored as a list of Input layers in Keras.Step 2: Check each option
inputs = [tf.keras.Input(shape=(10,)), tf.keras.Input(shape=(5,))] correctly creates a list of two Input layers. inputs = tf.keras.Input(shape=(10,)), tf.keras.Input(shape=(5,)) creates a tuple but does not assign it properly. inputs = tf.keras.Input(shape=(10, 5)) defines a single input with combined shape. inputs = tf.keras.Input(shape=(10,)); inputs = tf.keras.Input(shape=(5,)) overwrites the first input with the second.Final Answer:
inputs = [tf.keras.Input(shape=(10,)), tf.keras.Input(shape=(5,))] -> Option CQuick Check:
Multiple inputs = list of Input layers [OK]
- Using a tuple instead of a list for inputs
- Overwriting inputs instead of storing both
- Combining shapes into one input incorrectly
input1 = tf.keras.Input(shape=(8,)) input2 = tf.keras.Input(shape=(4,)) x1 = tf.keras.layers.Dense(5)(input1) x2 = tf.keras.layers.Dense(3)(input2) output1 = tf.keras.layers.Dense(2)(x1) output2 = tf.keras.layers.Dense(1)(x2) model = tf.keras.Model(inputs=[input1, input2], outputs=[output1, output2]) print([o.shape for o in model.outputs])
Solution
Step 1: Trace output layers shapes
output1 is Dense(2) applied to x1, so shape is (None, 2). output2 is Dense(1) applied to x2, so shape is (None, 1).Step 2: Understand batch dimension
TensorFlow uses None for batch size, so output shapes include None as first dimension.Final Answer:
[TensorShape([None, 2]), TensorShape([None, 1])] -> Option AQuick Check:
Output shapes match Dense layer units [OK]
- Confusing input shape with output shape
- Ignoring batch dimension null
- Mixing intermediate layer shapes with output shapes
input = tf.keras.Input(shape=(10,)) x = tf.keras.layers.Dense(8)(input) output1 = tf.keras.layers.Dense(4)(x) output2 = tf.keras.layers.Dense(3)(x) model = tf.keras.Model(inputs=input, outputs=[output1, output2])
Solution
Step 1: Check inputs parameter
inputs can be a single Input layer if there is only one input source.Step 2: Check outputs parameter
outputs can be a list of tensors to define multiple outputs.Final Answer:
No error, the model is defined correctly -> Option DQuick Check:
Single input + multiple outputs = valid model [OK]
- Thinking inputs must always be a list
- Believing outputs cannot be a list
- Assuming multiple outputs require multiple inputs
Solution
Step 1: Define inputs separately for image and vector
Two inputs require two Input layers with correct shapes: (64,64,3) for image and (10,) for vector.Step 2: Define outputs separately for classification and regression
Outputs are two layers: one Dense with 5 units and softmax for classification, one Dense with 1 unit for continuous value.Final Answer:
inputs = [tf.keras.Input(shape=(64,64,3)), tf.keras.Input(shape=(10,))]; outputs = [tf.keras.layers.Dense(5, activation='softmax')(x), tf.keras.layers.Dense(1)(y)] -> Option BQuick Check:
Separate inputs and outputs for multi-input/output model [OK]
- Combining inputs into one vector incorrectly
- Using single input for different data types
- Outputting combined units instead of separate outputs
