CAS logo CAS NLP LABInteractive Tutorial Series ← Series

⇄  Reading Both Ways

Bidirectional Recurrent Networks

Every lecture so far read the sentence the way you hear it — left to right, past only. This lecture adds the second lane: the same sentence read right to left, from the future. Two recurrent lanes over one sequence, concatenated at every position: each word understood in the light of its whole sentence.

Two Lanes, One Word Memory of the Future Follow-the-Dot Walkthrough Hand-Computed Concat Bi vs Uni
1997Schuster & Paliwalsame year as the LSTM
2Lanesforward + backward, never mixed
×2Parameterstwo weight sets, zero sharing
2hOutput widththe concat [h→ ; h←]
DEFINITION

A bidirectional recurrent network runs two independent recurrent layers over the same sequence in opposite directions and concatenates their states at every position: h→_t = Cell→(x_t, h→_{t−1}), h←_t = Cell←(x_t, h←_{t+1}), y_t = [h→_t ; h←_t]. The lanes share nothing — not weights, not state — only the input and the merge.

THE SAME PROBE SENTENCE, TWO LAMPS PER WORD

“The animal didn't cross the street because it was too tired.” — who is it? On the right, watch a bidirectional layer read it: for every word, two lamps fire — the teal lamp is the forward lane (everything the past knows), the sky lamp is the backward lane (everything the future knows).

At “it”, the sky lamp burns brightest — the answer arrived from the right.

WHY TWO DIRECTIONS? ↓

LIVE · TWO LANES PER WORD

y_t = [ h→_t ; h←_t ]

teal = past lane · sky = future lane — both lit at every position.

First, the honest motivation: a left-to-right reader is genuinely blind on real sentences — and you can watch the blindness.

02 THE MOTIVATION

One direction is blind. The future settles it.

Same left context, opposite futures — the classic Winograd pair. Pick a sentence, then limit what the reader may see: left only (every RNN so far), right only, or both. Watch the probability bars move.

The future exists at read timeTagging, NER, question answering, encoding for translation: the whole sentence is sitting in memory before the model emits anything. Reading it from both ends is free information.
Ambiguity resolves from the right“…because it was too tired / too wide” — identical past, opposite answer. No left-to-right reader, however deep, can distinguish the pair at the pronoun.
Reading, not predictingThe backward lane does not forecast the future — the sequence is already complete. It simply reads from the end. Nothing probabilistic, nothing mystical.
The priceThe full sequence must exist up front: no streaming, no live speech, no text generation. Bidirectionality is the encoder's luxury — §10 draws the exact line.

Convinced the second lane earns its keep? Then here is the machine — every wire of it, before we animate the two readings.

03 THE BLUEPRINT

Anatomy of two lanes

One input at the bottom, forked. Two cells fed from opposite directions of time. One concat at the top — the only place the lanes ever meet. Hover the diagram.

One input, forkedx_t is copied to both lanes unchanged. Whatever the network learns about this word from the left, it may learn something different from the right.
Two time-sourcesThe forward cell consumes h→_{t−1} (the past); the backward cell consumes h←_{t+1} (the future). Opposite arrows, same recurrence.
Nothing is sharedW→ and W← are separate weight sets with separate states — compressing the past and compressing the future are different jobs. Hence exactly ×2 parameters.
Concat, never addy_t = [h→_t ; h←_t] keeps the two readings in separate dimensions, so downstream layers can learn when to trust the past, the future, or both.

04 STEP BY STEP · BLOCK ARCHITECTURE

Follow the signal, both ways

Start with the full diagram, fully lit — fork, two lanes, two time-sources, concat, output — then tour one word's double reading in data-flow order: fork → from the past → from the future → both fire → the concat. A glowing dot rides the live data path at each step; the dialog underneath unpacks the math, symbol by symbol.

OVERVIEW · FULL DIAGRAM

Architecture references: Schuster & Paliwal, "Bidirectional Recurrent Neural Networks" (1997) · Bahdanau et al., "Neural Machine Translation by Jointly Learning to Align and Translate" (2015, BiGRU encoder) · Huang et al., "Bidirectional LSTM-CRF Models for Sequence Tagging" (2015).

05 CELL FLAVORS + THE PRICE OF TWO LANES

One trick, three cells

Bidirectionality is cell-agnostic — mirror any cell you like. Flip between BiRNN, BiGRU and BiLSTM; then price a whole bidirectional stack with the sliders. Watch the fan-in jump to 2h above layer 1: the next layer reads the concat.

LAYERS · L2
HIDDEN SIZE · h (per direction)128
INPUT SIZE · x100
BiRNN2 × 1 matrix / layer
BiGRU2 × 3 matrices / layer
BiLSTM2 × 4 matrices / layer

Statics understood. Now watch the thing move: a BiLSTM reading two probes, both lanes' memories exposed side by side.

06 LIVE BI-CELL · READING A SENTENCE

Watch the future decide

A BiLSTM reads the series' two probes. Left, the forward lane — what the past knows at each word. Right, the backward lane — what the future knows. Step word by word and watch the backward row solve the pronoun before the forward row can.

BI-CELL ACTIVE

TOKEN STREAM · click any word to jump

FORWARD h→ · knows the past

mean |h|
0.00

BACKWARD h← · knows the future

mean |h|
0.00
READY

You've seen the behavior in both lanes. Here is the exact machinery — one sign flip, t−1 becomes t+1, and everything else you already know.

07 THE GOVERNING EQUATIONS

One sign flip. Every symbol touchable.

Hover or tap any highlighted symbol — the inspector below explains its job, direction and personality.

h→_t=Cell→(x_t,h→_{t−1}) · the ordinary recurrence — reads the past
h←_t=Cell←(x_t,h←_{t+1}) · the mirrored recurrence — reads the future
y_t=[ h→_t ; h←_t ] · W→ ≠ W← · Cell∈ { RNN, GRU, LSTM }

READING ORDER

① the two equations are the SAME recurrence mirrored — the only change is the state index: t−1 (past) becomes t+1 (future).

② both lanes read the same x_t; neither ever sees the other's state.

③ the lanes never share weights: W→ compresses the past, W← the future — hence exactly ×2 parameters.

④ the output concatenates — width 2h, never an addition. The next layer reads 2h-wide vectors.

08 ONE WORD, TWO DIRECTIONS, BY THE NUMBERS

Hand-computed. Both lanes.

One-dimensional states, actual weights, one tanh per direction. Step through both lanes and watch y_t = [0.8977 ; 0.4930] emerge — the NumPy tab in §11 reproduces it to the fourth decimal.

Forward pass understood — in both directions. The backward pass mirrors too: error flows left in one lane and right in the other.

09 BPTT, MIRRORED

Gradients also travel both ways

Backpropagation through time runs opposite to each lane's reading direction: the forward lane's error flows toward the past, the backward lane's toward the future. Drag the gated keep-factor and compare the three chains.

Gated keep-factor per step0.95

Honest footnote: bidirectionality is not a gradient cure — each lane vanishes on its own schedule, which is why the lanes are gated (GRU/LSTM) in practice. What two lanes DO buy: every position receives error from both sides, so the shorter of the two paths to any word is often much shorter than the only path a unidirectional net has.

10 THE VERDICT

Bi vs uni — the honest scoreboard

Direction is decided by one question: does the future exist at read time? Flip through the options, then check the head-to-head table.

Go bidirectional when…the whole input exists up front and every position needs full-sentence context: tagging, NER, span extraction, encoding for QA and translation. The encoder's default.
Stay unidirectional when…the future genuinely doesn't exist: language modeling, text generation, live streaming, on-device prediction. The decoder's only legal architecture.
Empirically…BiLSTM-CRF was the NER standard (Huang 2015); Bahdanau's attention encoder was a BiGRU (2015); ELMo was bidirectional LSTMs (2018). The rule survived the transformer: BERT encodes both ways, GPT decodes one way.
AspectUnidirectional →Bidirectional ⇄
Context per positionthe past onlypast + future — the whole sentence
Parameters — two weight sets, zero sharing
Compute2× — but the two lanes parallelize
Output widthh2h — the concat [h→ ; h←]
Streaming / real-time — emits as words arrive✗ — needs the full sequence first
Text generation — the decoder's only option✗ — the future doesn't exist yet
Gradient pathstime, one directiontime, mirrored — error from both sides
Classic uselanguage modeling, decodersNER (BiLSTM-CRF), tagging, QA encoders, ELMo

One-line summary: bidirectionality turns one reader into two readers meeting at every word — double the parameters, double the compute, the whole sentence in every vector — legal only when the future already exists.

11 CODE LAB

Bidirectional: one flag, one wrapper, or from scratch

PyTorch hides the second lane behind bidirectional=True; Keras wraps any recurrent layer in Bidirectional; the NumPy tab shows what both are actually doing — and reproduces §08 to the fourth decimal.

BIDIRECTIONAL · THREE WAYS

12 SUMMARY

What to carry out of this room

01 · The idea

Run two independent recurrent layers over the same sequence in opposite directions; concatenate their states at every position. Each word is understood in the light of its whole sentence.

02 · The wiring

x_t forks to both lanes unchanged. h→ flows left→right from the past; h← flows right→left from the future. The lanes meet only at the concat — never in the weights, never in the state.

03 · What the future buys

Ambiguity resolution: pronouns (Winograd), word senses, entity boundaries. “It was too tired” is unsolvable left-to-right and trivial right-to-left.

04 · The bill

Exactly ×2 parameters and ×2 compute per layer, a 2h-wide output that compounds when stacked — and a hard requirement: the full sequence up front. No streaming, no generation.

05 · The flavors

BiRNN (Schuster & Paliwal, 1997), BiGRU (Bahdanau's attention encoder, 2015), BiLSTM (BiLSTM-CRF for NER; ELMo, 2018). One trick, any cell.

06 · The verdict

Encoders go bidirectional; decoders stay unidirectional. The rule outlived the RNN: BERT reads both ways, GPT reads one. Direction is about whether the future exists — not about the architecture.

13 MASTERY CHECK

Eight questions. Two lanes, no hiding.

YOUR SCORE

0 / 8