Multi-Input Models
A Keras functional model can share one representation and branch into several named prediction heads.
One Graph, Several Predictions
A multi-input or multi-output model is easier to understand when you stop imagining it as one straight stack of layers. A Keras functional model can receive several streams of data, process each stream through its own branch, and merge the resulting representations. It can also share one representation and branch into several named prediction heads. The graph therefore has places where information is separate and places where it is shared.
Tracing Two Input Branches
A single-input model receives one stream of data. A multi-input model receives several streams and gives each stream its own path before combining the resulting representations. For example, a question-answering system can receive a question through one input and a text snippet through another. The question and text travel independently until a merge operation brings their branch representations together.
Following the Question and Text
Trace the intended path for a question-answering model with named inputs text and question.
- 1Input assignment
The text data belongs to the text input node, and the question data belongs to the question input node.
- 2Branch processing
Each input travels through its own branch encoder, producing a representation for that source.
- 3Merge
The two branch representations meet through a Keras merge operation.
- 4Prediction
The merged representation travels to the answer output.
The intended path is text to text branch, question to question branch, both branches to merge, and merge to answer.
Building the Branch Structure
The main construction stages of a two-input functional model are input layers, independent branch encoders, a merge operation, and an output layer. Input names such as text and question identify the input nodes. Those names are useful later because dictionary-based training uses them to associate each array with the correct branch. The model construction is explicit: it receives both input tensors and produces the answer tensor.
The branches meet through a Keras merge operation such as concatenate or add. The merge line is not a minor implementation detail. It determines how the branch representations are brought together, so it is one of the first places to inspect when the model behaves unexpectedly.
| Merge strategy | What it does | Inspection question |
|---|---|---|
| Concatenate | Brings the branch representations together as a combined representation | Did the merge preserve the separate branch information in the intended arrangement? |
| Add | Combines the branch representations through an addition operation | Are the branch outputs suitable for the intended addition? |
Both are Keras merge operations, but they bring branch representations together in different ways.
Routing Training Data
Training data must preserve the model's input structure. With list-based training, input arrays are supplied in the same order as the model's inputs. This form is concise, but the order must remain correct. With dictionary-based training, arrays are supplied under the names of the input nodes. This makes the pairing explicit and requires named inputs.
Choosing a Training Form
A model has named inputs text and question. Organize the two input arrays for training.
- 1List form
Place the text array first and the question array second when that is the model's input order.
- 2Dictionary form
Associate the text array with the key text and the question array with the key question.
- 3Verification
Check that the selected form agrees with the model's input order or input names before training.
Both forms can preserve the intended mapping, but the list depends on order while the dictionary makes the names explicit.
Matching Heads to Losses
A multi-output model makes several predictions from the same input or shared internal representation. Each output can represent a different task, so each head needs a loss suited to what it predicts. The source example uses an age head for scalar regression, an income head for classification over income groups, and a gender head for binary classification. These tasks therefore use different kinds of losses rather than one loss reused blindly for every output.
| Prediction head | Task described in the source | Output behavior | Suitable loss examples |
|---|---|---|---|
| Age | Scalar regression | One unit | Mean squared error |
| Income | Classification over income groups | One unit per income group with softmax | Categorical crossentropy |
| Gender | Binary classification | One unit with sigmoid | Binary crossentropy |
The output task determines the kind of loss that should be associated with the head.
During training, one target array is paired with each output. Keras accepts those targets as a list ordered like the model outputs or as a dictionary keyed by output names. The dictionary form makes the pairing explicit; the list form is shorter but depends on keeping the output order correct.
Forming the Global Loss
The separate output losses do not remain isolated during optimization. Keras combines the individual losses into one global loss used to optimize the model. Because the output heads share a representation, the combined result determines how the shared part of the network is trained.
A plain combination does not guarantee that every task influences learning equally. If one individual loss is numerically much larger than the others, optimization can be driven mainly by that task. Loss weights change each loss's contribution before the global loss is formed, helping balance outputs whose losses have different numerical scales.
Balancing Three Tasks
A shared representation feeds age, income, and gender heads. The age loss is numerically much larger than the other two losses. What should you inspect?
- 1Identify the imbalance
Compare the numerical scales of the individual losses rather than assuming that their unweighted contributions are equally influential.
- 2Inspect the global combination
Keras combines the individual losses into one global loss, so a larger individual loss can have a larger effect on optimization.
- 3Consider loss weights
Loss weights change each loss's contribution before the global loss is formed and can help prevent the age task from dominating the shared representation.
The likely inspection point is the loss-weight configuration and the relative numerical scales of the individual losses.
Debugging the Data Path
A multi-input model can fail at several distinct boundaries. Treat it as a trace: data enters an input node, passes through its branch, reaches the merge operation, and contributes to the output. For a multi-output model, trace each prediction head to its loss, optional weight, and target data. The most useful debugging question is where the actual path first differs from the intended graph.
Common mistake Supplying list-based inputs in an order different from the model's input order.
What it looks likeThe text array is placed where the question array should be, or the question array is placed where the text array should be.
Why it failsList-based training pairs arrays by position, so a changed order sends data to the wrong branch.
Do this insteadCheck the model's input order before supplying a list, or use named inputs with a dictionary.
Common mistake Using dictionary-based inputs without matching the model's input names.
What it looks likeThe training dictionary uses a key that does not identify the named input node.
Why it failsDictionary-based training relies on input names to make the pairing explicit.
Do this insteadCompare every dictionary key with the model's input names.
Common mistake Treating every output as if it represented the same task.
What it looks likeReusing one loss blindly for age regression, income-group classification, and binary gender classification.
Why it failsThe output heads represent different prediction tasks and need losses suited to those tasks.
Do this insteadTrace each head to its task-appropriate loss.
Common mistake Assuming a plain combination of losses gives every task equal influence.
What it looks likeA numerically larger individual loss dominates the shared representation.
Why it failsDifferent numerical scales can make one loss contribute more strongly to the global optimization signal.
Do this insteadInspect the loss scales and consider loss weights.
Common mistake Inspecting only the final prediction when a multi-input model behaves unexpectedly.
What it looks likeThe final answer is wrong, but the actual mismatch began when data entered one branch or when branch outputs were merged.
Why it failsThe model is a graph with several boundaries, not one undifferentiated object.
Do this insteadTrace data mapping, each input branch, the merge operation, and the output path separately.
Practice the Trace
A functional model has two named inputs, text and question, and one answer output. Explain how you would verify the data path before training. Then describe how the training data would differ between list-based and dictionary-based input forms. Finally, imagine that the model has age, income, and gender outputs instead. State what you would inspect for each output besides the prediction itself.
Hints
- Begin with the mapping between arrays and input names or input order.
- Follow each input through its independent branch and inspect the merge operation.
- For several outputs, trace each head to its target, loss, and optional loss weight.
- 1A multi-input Keras model gives each data source an independent branch before merging the branch representations. Named input nodes support dictionary-based training, while list-based training depends on preserving input order. A merge operation such as concatenate or add determines how branch outputs meet. A multi-output model can reuse a shared representation for several named prediction heads. Each head needs a task-appropriate loss and target data, and Keras combines the individual losses into one global optimization loss. Loss weights help when one loss has a much larger numerical scale. When debugging, trace the actual data path from mapping to branch, merge, output, target, loss, and optional weight.
Key Takeaways
- Separate inputs travel through independent branches and meet at a Keras merge operation.
- Concatenate and add are different merge strategies, so the merge stage deserves explicit inspection.
- One shared representation can feed several named prediction heads for different tasks.
- Each output needs matching target data and a loss suited to its task; loss weights can balance different loss scales.
- List-based training depends on order, dictionary-based training depends on names, and debugging should trace the complete data path.