Sequence Processing with Recurrent Layers
Multi-input models use separate branches for separate data sources.
From One Stream to Several
A single-input model receives one stream of data. A multi-input model receives several streams and gives each stream its own path through the network before combining the resulting representations. The Keras functional API is designed for this graph-shaped model structure.
A question-answering system can use two inputs: a question and a text snippet containing information needed to answer it. The question and the text do not enter through one shared input node. Each has its own branch, and the branch results are combined before the answer is produced.
Tracing the Model Graph
The important shift is to stop treating the model as one straight sequence of layers. Instead, inspect it as several branches that eventually meet. For a two-input model, trace the data in this order: each input node, its branch encoder, the merge operation, and the final output.
Tracing a Question-Answering Graph
Trace the intended path when a model receives a question and a text snippet.
Start at the inputs: The model receives one question input and one text input. These are separate streams rather than one combined stream.
Follow each branch: The question travels through the question branch, while the text travels through the text branch. Each branch produces a representation.
Inspect the meeting point: The two branch representations meet at a Keras merge operation such as concatenate or add.
Continue to the output: The merged result is passed toward the answer output.
The complete path is question and text inputs, independent branch encoders, a merge operation, and an answer output.
Constructing the Two-Branch Model
The construction has four main stages. First, create an input node for each data source. Second, send each input through its own encoder branch. Third, combine the branch outputs with a Keras merge operation. Finally, define the model with both input tensors and the answer tensor as its output.
This example is schematic: question_encoder, text_encoder, and output_layer stand for the branch and output components you choose. The important graph structure is explicit. The two named input tensors are passed to Model, and the answer tensor is declared as the output.
Choosing the Merge Operation
The branches must meet through a merge operation. Keras provides operations such as keras.layers.concatenate and keras.layers.add. The choice determines how the branch representations are brought together, so the merge line deserves separate inspection whenever the model behaves unexpectedly.
| Merge choice | Role in the graph | Inspection question |
|---|---|---|
| concatenate | Brings the two branch representations together through concatenation | Is this the intended way to combine the branch outputs? |
| add | Brings the two branch representations together through addition | Is addition the intended merge operation for these branches? |
What do you think happens?
A two-input model has correctly built question and text branches. What graph component must still be present before the output can use both branch results?
Reveal answer
Answer: A merge operation
The branches meet through an operation such as concatenate or add before the resulting representation is sent toward the output.
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 uses input names, associating the text array with text and the question array with question. Dictionary-based training therefore requires named inputs.
These two training calls express the same structural requirement in different ways. The list relies on the model's input order. The dictionary relies on the names question and text. If the order or names do not match the model inputs, the data path no longer matches the intended graph.
Debugging by Boundary
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. The most useful debugging question is where the actual path first differs from the intended path.
Treating a multi-input graph as one straight sequence of layers.
The model has several paths that remain separate until the merge operation.
Fix:
Trace each input node through its own branch before inspecting the merge and output.Supplying list-based training data in the wrong input order.
List-based training maps arrays according to the model's input order.
Fix:
Compare the list order with the order of the model's inputs before training.Using dictionary keys that do not identify the named inputs.
Dictionary-based training depends on input names.
Fix:
Check that the model inputs are named and that the dictionary keys match those names.Skipping inspection of the merge line.
Concatenate and add are distinct merge choices.
Fix:
Inspect whether the graph uses the intended Keras merge operation.
- Check which array or named value is reaching each input node.
- Follow each branch independently and verify that it is receiving the intended source.
- Inspect the merge operation and confirm whether concatenate or add was intended.
- Confirm that the model output is built from the merged representation.
- If using a list, verify input order. If using a dictionary, verify input names.
Practice the Trace
A model is defined with two named inputs, question and text. Its graph sends question through one encoder and text through another, then uses add before the output. Training data is supplied as a list whose first array is text and whose second array is question. Identify the first boundary you would inspect and explain why.
Hints
- Start with the mapping from training data to model inputs.
- A list uses the model's input order.
- Only after the input mapping is checked should you inspect the branch and merge stages.
Applying the Boundary Rule
The model inputs are ordered as question, then text, but the training list supplies text first and question second.
Compare the structures: The model expects the first list array at question and the second list array at text.
Locate the first mismatch: The supplied arrays are reversed relative to the model input order, so the mismatch occurs at the training-data mapping boundary.
Delay later inspection: The branch encoders and merge operation should be inspected after confirming that each input node receives the intended data.
Inspect and correct the list-to-input mapping first, then continue tracing the branches and merge stage.
Key Takeaways
- A multi-input Keras model gives each data source its own branch before combining branch representations.
- The main graph stages are input nodes, branch encoders, a merge operation, and an output.
- keras.layers.concatenate and keras.layers.add are distinct choices for the merge stage.
- List-based training data follows model input order, while dictionary-based training data uses named inputs.
- The most practical debugging route is to find where the actual data path first differs from the intended graph.
Key Takeaways
- Separate inputs travel through independent branches in a multi-input functional model.
- The branches meet at a merge operation such as concatenate or add.
- Named inputs support dictionary-based training data, while list-based data depends on input order.
- Debugging is clearer when input mapping, branch flow, merging, and output are checked as separate boundaries.