Classification with Softmax Outputs
Multi-input models use separate branches for separate data sources.
From Separate Data to One Answer
A single-input model receives one stream of data. A multi-input model receives several streams, sends each stream through its own path, and combines the resulting representations before producing an output. This is why a multi-input model should not be viewed as one straight sequence of layers. Its structure is a graph with branches that eventually meet.
A question-answering system can use one input for a question and another input for a text snippet containing information needed to answer it. The question and text travel through separate branches before their representations are combined.
The Two-Input Graph
The Keras functional API is designed for graph-shaped models. A two-input model begins with two input nodes. Each input is passed through its own branch encoder. The branch outputs then meet at a Keras merge operation, such as concatenate or add. The merged representation is passed to an output layer that produces the answer tensor.
The names text and question identify the two input nodes. They are part of the model structure, not decorative labels, because the names can later be used to map dictionary-based training data to the correct inputs.
This code is a structural example. The important path is explicit: the text input enters the text branch, the question input enters the question branch, both branch representations reach the merge operation, and the output layer receives the merged result. The Model construction receives both input tensors and produces the answer tensor.
Choosing the Merge Operation
| Merge strategy | What it does | What to inspect |
|---|---|---|
| Concatenate | Combines the branch representations into one merged representation | Whether both branch outputs are being brought into the merged result |
| Add | Combines the branch representations through an addition operation | Whether addition is the intended way to combine the branch outputs |
Concatenation and addition are not interchangeable descriptions of the graph. They are different merge operations, and the choice determines how the branch representations are brought together. When a multi-input model behaves unexpectedly, inspect the merge line instead of assuming that every merge combines information in the same way.
Supplying Training Data
Training data must preserve the model's input structure. The source describes two supported forms. A list supplies arrays in the same order as the model's inputs. A dictionary supplies arrays by input name. Dictionary-based training requires named inputs.
In the list form, position carries the meaning: the first array belongs to the first model input, and the second array belongs to the second model input. In the dictionary form, the names carry the meaning: the array under text is associated with the input named text, and the array under question is associated with the input named question.
Tracing a Failure
A multi-input model can fail at several distinct boundaries. Treat the model as a trace: data enters an input node, passes through its branch, reaches the merge operation, and contributes to the output. The most useful debugging question is where the actual data path first differs from the intended graph.
Checking only the final classifier when the real problem is earlier in the graph.
The final output depends on the paths and merge operation that came before it.
Fix:
Inspect the input branches first, then the merge stage, and only then the final classifier.Supplying list-based training arrays in an order different from the model's input order.
List-based training maps arrays by position.
Fix:
Compare the list order with the model's declared input order before training.Using dictionary-based training with names that do not identify the model inputs.
Dictionary-based training relies on input names.
Fix:
Use the model's input names as the dictionary keys.Assuming concatenate and add represent the same merge behavior.
The merge operation determines how the branch representations are brought together.
Fix:
Inspect the merge line and verify that it uses the intended Keras operation.
Practice the Trace
A model has two named inputs, text and question. Its input list is ordered as [text, question]. You have text_array and question_array. Explain how you would supply the data using the list form and the dictionary form. Then identify the first three places you would inspect if the model produced an unexpected output.
Hints
- For the list form, use the model's input order.
- For the dictionary form, use the input names.
- Trace the path from data mapping to input branches to the merge operation.
Checking a Two-Input Training Call
The model inputs are ordered as text followed by question. Decide whether each training-data form preserves the intended mapping.
List form: [text_array, question_array] preserves the declared input order, so the text array maps to text and the question array maps to question.
Dictionary form: {"text": text_array, "question": question_array} uses the named inputs, so each array is associated with its matching input name.
Debugging check: If the result is unexpected, inspect the data mapping first, then verify each branch receives its intended data, and then inspect whether the merge operation is concatenate or add as intended.
Both forms preserve the intended mapping when the list order is correct and the dictionary keys match the named model inputs.
Key Takeaways
- A multi-input Keras functional model gives each data source its own branch before combining the branch representations.
- A two-input graph contains input layers, branch encoders, a merge operation, and an output layer.
- Concatenate and add are different Keras merge operations, so the merge line deserves deliberate inspection.
- List-based training data maps by model input order, while dictionary-based training data maps by named inputs.
- When debugging, trace the actual data path from training-data mapping to input branches to the merge stage and then to the output.
Key Takeaways
- Separate inputs travel through independent branches in a Keras functional model.
- The branches meet at a merge operation such as concatenate or add before reaching the output layer.
- Input names and input order determine how training arrays are connected to the model.
- A reliable debugging strategy is to find where the actual data path first differs from the intended graph.