Concepts / Sequence Processing with Recurrent Layers

Sequence Processing with Recurrent Layers

Multi-input models use separate branches for separate data sources.

  • Programming

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.

travels throughtravels throughbranch resultbranch resultcombined resultquestioninput tensorquestion encoderbranch representationmergeconcatenate or addansweroutput tensortextinput tensortext encoderbranch representation
How do the question and text inputs travel through independent encoder branches before reaching the merge stage?

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.

separate pathsbranch representationscombined representationinput layersquestion and textbranch encodersone path per inputmerge operationconcatenate or addoutput layeranswer tensor
What contains what, and what happens next, from the two input layers through branch encoders, merging, and the output layer?

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.

python

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.

branch outputbranch outputbranch outputbranch outputcombinedcombinedquestionrepresentationconcatenateone merge choiceconcatenatedrepresentationtext representationaddanother merge choiceadded representation
What changes in the merged representation when the branch outputs are concatenated instead of added?
Merge choiceRole in the graphInspection question
concatenateBrings the two branch representations together through concatenationIs this the intended way to combine the branch outputs?
addBrings the two branch representations together through additionIs 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?

  • A merge operation
  • A second model output
  • A replacement for one input node
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.

first arraysecond arraykey lookupkey lookupmapped inputmapped inputmapped inputmapped inputlist of arraysinput order mattersfirst model inputquestionquestionnamed keyquestion branchdictionary ofarraysinput names mattersecond model inputtexttextnamed keytext branch
How do a list of arrays and a dictionary of arrays map training data to the model's separate input branches?
python

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.

data reaches nodesbranch resultscombined resulttraining-datamappinglist order or namesinput branchesintended data per branchmerge stageconcatenate or addoutputfinal result
Where does data or control flow break when checking each input branch, the merge operation, and the training-data mapping in sequence?
  • 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.

  1. Check which array or named value is reaching each input node.
  2. Follow each branch independently and verify that it is receiving the intended source.
  3. Inspect the merge operation and confirm whether concatenate or add was intended.
  4. Confirm that the model output is built from the merged representation.
  5. If using a list, verify input order. If using a dictionary, verify input names.

Practice the Trace

MEDIUM

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

  1. A multi-input Keras model gives each data source its own branch before combining branch representations.
  2. The main graph stages are input nodes, branch encoders, a merge operation, and an output.
  3. keras.layers.concatenate and keras.layers.add are distinct choices for the merge stage.
  4. List-based training data follows model input order, while dictionary-based training data uses named inputs.
  5. 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.