Search

Search papers and lessons

Bidirectional Recurrent Neural Network

Level 3 · 11 min read

A bidirectional RNN runs one recurrent network forward and another backward over a sequence, then joins the two states so every position sees both its past and its future.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Neural network basics
  • Probability
  • Architectures
  • Losses & regularization
  • Sign in to see your progress

TL;DR

A bidirectional recurrent neural network reads a sequence twice: once from left to right, and once from right to left. Each position then gets two hidden states, one that summarises everything before it and one that summarises everything after it. Joining them gives a representation that uses the whole sequence around that position.

This matters when the meaning of an element depends on what comes later as well as earlier. The price is that the model must see the whole sequence before it can answer, so it cannot be used to predict the next item in real time.

Where you'll see it: in part-of-speech and named-entity tagging, in speech recognition systems that transcribe a complete utterance, and in early pretrained language encoders before transformers took over.

Intuition

Consider the word "Washington" in two sentences. In "Washington signed the bill", you cannot tell whether it is a person or a place from the first word alone. The clue is "signed the bill", which comes after it. A plain left-to-right recurrent network has already committed to its reading of "Washington" by the time it reaches "signed". It never gets to revisit the decision.

A bidirectional network fixes this by reading the sentence twice. One reader goes left to right and keeps a running summary of the past. A second reader goes right to left and keeps a running summary of the future. For each word, you then hold both summaries side by side. When you ask what "Washington" means, you have the words before it and the words after it in front of you.

The plot below makes the trade-off concrete for a sequence of ten tokens. A forward reader at position xx has seen xx tokens so far, counting the current one. A backward reader at the same position has seen the current token and the 10−x10 - x that follow, so 11−x11 - x in all. Together they cover the whole sequence, with the current token counted twice. The two lines cross at the middle, between positions 5 and 6.

2468100246810position t in a 10-token sequencenumber of tokens seen
  • tokens seen by the forward reader
  • tokens seen by the backward reader
At position 5 the forward reader has seen 5 tokens and the backward reader 6. Near the start, the forward reader knows almost nothing, while the backward reader sees nearly everything.

Two readers that never talk to each other, merged at the end, is the whole idea. Nothing about the recurrence changes. The network still applies the same update rule at each step; it simply does so twice, in opposite directions.

The math

Setup

Let the input sequence be x1,x2,…,xTx_1, x_2, \dots, x_T, where each xt∈Rdx_t \in \mathbb{R}^d is a vector, for example an embedding of a word. The forward network produces hidden states h→1,…,h→T\overrightarrow{h}_1, \dots, \overrightarrow{h}_T in Rn\mathbb{R}^n, and the backward network produces h←1,…,h←T\overleftarrow{h}_1, \dots, \overleftarrow{h}_T in Rn\mathbb{R}^n. The arrows only indicate direction; they are not vectors. The two networks have separate weights.

The forward and backward recurrences

The forward recurrence is the usual one from the recurrent neural network, started at the left end:

h→t=tanh⁡ ⁣(Wf xt+Uf h→t−1+bf),h→0=0.\overrightarrow{h}_t = \tanh\!\left(W_f\, x_t + U_f\, \overrightarrow{h}_{t-1} + b_f\right), \qquad \overrightarrow{h}_0 = \mathbf{0}.

The backward recurrence runs the same kind of update from the right end, with its own weights:

h←t=tanh⁡ ⁣(Wb xt+Ub h←t+1+bb),h←T+1=0.\overleftarrow{h}_t = \tanh\!\left(W_b\, x_t + U_b\, \overleftarrow{h}_{t+1} + b_b\right), \qquad \overleftarrow{h}_{T+1} = \mathbf{0}.

Here Wf,Wb∈Rn×dW_f, W_b \in \mathbb{R}^{n \times d} map the input into the hidden space, Uf,Ub∈Rn×nU_f, U_b \in \mathbb{R}^{n \times n} carry the state from one step to the next, and tanh⁡\tanh is applied elementwise. The only difference between the two recurrences is the direction of the index.

Combining the two states

At each position the two states are joined. The most common choice is concatenation:

ht=[h→th←t]∈R2n.\mathbf{h}_t = \begin{bmatrix} \overrightarrow{h}_t \\ \overleftarrow{h}_t \end{bmatrix} \in \mathbb{R}^{2n}.

Other choices include summing the two states or averaging them. Concatenation keeps all the information, at the cost of doubling the size of the next layer. Summing keeps the size the same but mixes the two directions before anything downstream can separate them.

For an output such as a tag or a class, a further layer reads ht\mathbf{h}_t:

y^t=softmax⁡ ⁣(V ht+c),\hat{\mathbf{y}}_t = \operatorname{softmax}\!\left(V\,\mathbf{h}_t + \mathbf{c}\right),

where VV is an output matrix of size (number of classes) by 2n2n, and c\mathbf{c} is a bias vector.

Training

The network is trained by backpropagation through time, using a loss summed over positions. Gradients flow back through each direction along its own chain of time steps, and the two directions meet only in the loss, through the concatenated states. Because the forward and backward passes do not depend on each other, they can run in parallel. Each pass on its own is still sequential across time, so parallelism helps with throughput but does not remove the step-by-step nature of the recurrence.

Worked examples

For hand arithmetic, the examples below use ReLU, max⁡(0,z)\max(0, z), in place of tanh⁡\tanh. The structure is identical. All weights are scalars, with input weight 1 and recurrent weight 0.5 in both directions, and no biases.

Example
A three-step sequence by hand

The input sequence is x1=1x_1 = 1, x2=2x_2 = 2, x3=3x_3 = 3.

Forward pass (left to right, starting from h0=0h_0 = 0):

  1. h→1=max⁡(0, 1⋅1+0.5⋅0)=1\overrightarrow{h}_1 = \max(0,\ 1 \cdot 1 + 0.5 \cdot 0) = 1.
  2. h→2=max⁡(0, 1⋅2+0.5⋅1)=2.5\overrightarrow{h}_2 = \max(0,\ 1 \cdot 2 + 0.5 \cdot 1) = 2.5.
  3. h→3=max⁡(0, 1⋅3+0.5⋅2.5)=4.25\overrightarrow{h}_3 = \max(0,\ 1 \cdot 3 + 0.5 \cdot 2.5) = 4.25.

Backward pass (right to left, starting from the end):

  1. h←3=max⁡(0, 1⋅3+0.5⋅0)=3\overleftarrow{h}_3 = \max(0,\ 1 \cdot 3 + 0.5 \cdot 0) = 3.
  2. h←2=max⁡(0, 1⋅2+0.5⋅3)=3.5\overleftarrow{h}_2 = \max(0,\ 1 \cdot 2 + 0.5 \cdot 3) = 3.5.
  3. h←1=max⁡(0, 1⋅1+0.5⋅3.5)=2.75\overleftarrow{h}_1 = \max(0,\ 1 \cdot 1 + 0.5 \cdot 3.5) = 2.75.

Combined states (concatenation):

  • At t=1t = 1: (1, 2.75)(1,\ 2.75). The forward half has seen only x1x_1, but the backward half already contains information from x2x_2 and x3x_3.
  • At t=2t = 2: (2.5, 3.5)(2.5,\ 3.5).
  • At t=3t = 3: (4.25, 3)(4.25,\ 3). The backward half has seen only x3x_3, and the forward half has seen all three inputs.

Notice that the position-1 state, which a forward-only network would have as just 11, now carries 2.75 from the other direction.

Example
Coverage of a tagging decision

A tagger labels each word of a 12-word sentence. Consider the decision at position t=4t = 4.

  1. The forward state h→4\overrightarrow{h}_4 has read positions 1 through 4, so it knows the four words up to and including the current one.
  2. The backward state h←4\overleftarrow{h}_4 has read positions 12 down to 4, so it knows the nine words from the current one to the end.
  3. Together the two states cover all 12 positions. The current word is seen by both halves, so it is represented twice, and each half contributes a different view of it.
  4. A forward-only tagger would see only 4 positions. For a decision that depends on a later cue, such as a verb at position 9 that determines whether the word at position 4 is a subject or an object, the bidirectional version has the cue available and the forward version does not.
Example
Why the output cannot run online

A chat system must produce a word before the user has typed the next one. Suppose it uses a bidirectional encoder.

  1. To compute h←t\overleftarrow{h}_t for the current word, the backward reader must start at the end of the sequence.
  2. The end of the sequence is not known until the user finishes.
  3. So the backward state cannot be computed at the moment it is needed, and the bidirectional model is unusable for real-time next-word prediction.

Tradeoffs and failure modes

Bidirectionality buys context at the price of latency and a different kind of model. Use it when the whole input is available up front and the answer depends on context from both sides. Avoid it when you must produce outputs as the input arrives.

SituationProblemBetter choice
Tagging or classifying a complete sentence or utteranceNone; the full input is availableBidirectional RNN, or a bidirectional transformer for longer inputs
Generating text one token at a timeThe backward pass needs the future, which does not exist yetA unidirectional (causal) model
Streaming speech recognition with a latency budgetThe backward pass waits for the end of the utteranceUnidirectional model, or a bidirectional model over short chunks
Very long sequencesBoth passes are still sequential over time, and the vanishing-gradient problem affects both directionsAn attention-based model, or a gated cell such as an [[long-short-term-memory
Using the model as a language model for scoringEach token's score uses the token itself through the backward path, so the probability is not a proper next-token distributionTrain a unidirectional model for the probability, and use the bidirectional model only as an encoder

Some practical failure modes:

  • Leakage in training. If the training target leaks into the input, the model learns to copy it and the validation score looks suspiciously good. Check that no label information reaches the input through either direction.
  • Doubled cost. Each direction has its own weights, so the parameter count roughly doubles compared with a single RNN of the same width. The memory for activations also doubles during training.
  • Asymmetric information. The two directions do not contribute equally at every position. Near the start, the backward half is strong and the forward half is weak. A model that treats them symmetrically may underuse one of them.

History

Mike Schuster and Kuldip Paliwal introduced bidirectional recurrent neural networks in 1997 for speech, where a full utterance is available before recognition begins, and their paper set out the two-direction structure used today.

Alex Graves and Jürgen Schmidhuber combined bidirectional networks with LSTM cells in the mid-2000s and showed strong results on phoneme classification, which made the approach standard for speech tasks. It spread to natural language once researchers started building sequence taggers on top of pretrained word vectors. Huang, Xu and Yu's 2015 bidirectional LSTM-CRF tagger was a widely used example, pairing the two-direction encoder with a conditional random field on top to model dependencies between output tags.

In the late 2010s, pretrained language encoders such as ELMo used bidirectional LSTMs, and then BERT used a bidirectional transformer instead of a recurrent network. Transformers process all positions at once and attend to both sides directly, so the recurrent form of bidirectionality became less common for pretraining. The core idea that context from both sides helps the representation of each position remained.

Check yourself

QuestionA bidirectional RNN runs over a sequence of length 8. How many tokens has the forward reader seen at position 3, and how many has the backward reader seen? Which positions does the combined state cover?Show answer

The forward reader has seen positions 1, 2 and 3, which is 3 tokens, counting the current one. The backward reader has seen positions 8 down to 3, which is 6 tokens. Together they cover all 8 positions, and the current position is seen by both.

QuestionCompute the combined state at t=1t = 1 for the input x1=2x_1 = 2, x2=1x_2 = 1, with scalar weights input 1 and recurrent 0.5 and ReLU activation, and no biases.Show answer

Forward: h→1=max⁡(0,1⋅2+0)=2\overrightarrow{h}_1 = \max(0, 1 \cdot 2 + 0) = 2. Backward: h←2=max⁡(0,1⋅1+0)=1\overleftarrow{h}_2 = \max(0, 1 \cdot 1 + 0) = 1, then h←1=max⁡(0,1⋅2+0.5⋅1)=2.5\overleftarrow{h}_1 = \max(0, 1 \cdot 2 + 0.5 \cdot 1) = 2.5. The concatenated state at t=1t = 1 is (2, 2.5)(2,\ 2.5). The backward half contributes information from x2x_2 even though the position is at the start.

QuestionWhy is a bidirectional RNN unsuitable for generating text one word at a time, even though it can be trained on the same data?Show answer

Generation must produce the next word before the rest of the text exists. The backward state at a position depends on every later position, so it cannot be computed at generation time. Training works because the whole target sequence is known in advance, which hides the problem. A unidirectional model that uses only earlier positions is the right choice for generation.

QuestionYou are building a tagger for a system that must label each word of a finished document in batch overnight, and the labels depend on words that come later in the sentence. Would you use a bidirectional RNN, and what would you check before trusting the results?Show answer

Yes, a bidirectional RNN fits: the whole document is available, and the labels depend on context from both sides. Before trusting the results, check for leakage: confirm that no label information reaches the inputs through either direction. Compare against a forward-only baseline on sentences where later words matter, to confirm the backward half is actually helping rather than being ignored. Also check the memory budget, since the two directions roughly double the parameter count and activation memory.

Sources