liveBook · Manning

Bidirectional rnn

As explained in Inside Deep Learning: Math, Algorithms, Models

A recurrent-network configuration that processes a sequence in both forward and backward directions.

A bidirectional RNN processes a sequence in both directions: one recurrent layer reads left to right, while another reads right to left. Their outputs are combined at each time step, giving later layers access to information from both the past and the future. This can make learning long-range dependencies easier because information accumulates more evenly across the sequence. section 4.4.2

In PyTorch, enable the configuration with bidirectional=True. The representation from each direction is concatenated, so a hidden size of hidden_nodes produces 2 * hidden_nodes values. A classifier consuming the final state therefore needs nn.Linear(hidden_nodes * 2, classes). section 4.4.2 section 4.2.3

Implementing a bidirectional recurrent classifier typically requires these changes:

  • Set bidirectional=True on nn.RNN, nn.GRU, or another recurrent layer. section 4.4.2
  • Extract the final layer’s states for both directions, reshape them by layer and direction, then permute and concatenate them by batch. section 4.2.3 section 11.4.1
  • Resize downstream layers to consume the doubled representation; a three-layer example uses LastTimeStep(rnn_layers=3, bidirectional=True) followed by nn.Linear(hidden_nodes*2, classes). section 4.4.2

Sequence-to-sequence encoder

A sequence-to-sequence model can use a bidirectional GRU encoder because the complete input sequence is available. With hidden_size//2 units per direction, concatenation restores the intended hidden_size for the encoder representation. The decoder remains one-directional: it generates output incrementally and cannot know future output tokens. section 11.4.1

Figure 4.16 Bidirectional RNN. The output of each step in time goes to two RNNs: one processing input from left to right (green) and the other from right to left (red). The output of the two RNNs is combined (via concatenation) to create one new item at each step. This item then goes to two outputs.

Shows the two recurrent passes, their per-step concatenation, and the combined outputs passed onward. section 4.4.2

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Explains directional concatenation, hidden-state reshaping, and downstream sizing, but defers detailed recurrent internals.
Examples
Demonstrates three-layer PyTorch classification and a bidirectional GRU encoder paired with a stepwise decoder.
What it assumes
Assumes familiarity with recurrent layers, hidden states, sequence dimensions, and PyTorch modules.
What you can do after
Build it from scratch with PyTorch by enabling bidirectionality, extracting both final states, and resizing downstream layers.
How it connects
Connects bidirectional RNNs with recurrent neural networks, GRUs, sequence-to-sequence models, attention, embeddings, and fully connected layers.

Also explained in

Compare how 4 books explain bidirectional rnn →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.