3.5.6 · D4Sequence Models

Exercises — Bidirectional RNNs

2,054 words9 min readBack to topic

Throughout, we use the parent note's notation. As a quick recall before you start:

Recall Notation refresher (open if any symbol is unfamiliar)
  • ::: the input vector at timestep (a word embedding, a vector of numbers).
  • ::: the forward hidden state, built from (the past).
  • ::: the backward hidden state, built from (the future).
  • ::: concatenation — stack the two vectors end to end.
  • ::: a squashing function that maps any real number into .
  • ::: turns a vector of scores into probabilities that sum to .
Figure — Bidirectional RNNs

Level 1 — Recognition

L1.1

A vanilla RNN computes from inputs only. Which of the four tasks below cannot be solved reliably by a vanilla (forward-only) RNN, and needs a BiRNN?

(a) Predicting the next word in a sentence you are typing live. (b) Labelling "Paris" as city vs person given the whole sentence. (c) Streaming speech-to-text where audio arrives one frame at a time and must be transcribed immediately. (d) Tagging every word of a complete sentence with its part of speech.

Recall Solution

Need a BiRNN: (b) and (d). Both label a word using context that can appear after it, and the full sequence is available.

  • (a) is causal — you only have the past by definition, so a forward RNN is correct.
  • (c) is online / real-time: the future frames have not arrived yet, so a BiRNN cannot be used (it would need to wait for the whole utterance).
  • (b), (d): full sequence present, answer depends on later words → BiRNN wins.

L1.2

In the combination , suppose and . What is the dimension of ? What would it be if we used summation instead?

Recall Solution
  • Concatenation stacks them: dimension .
  • Summation adds element-wise: both must already match ( and ), and the result stays . Concatenation keeps past and future separate; summation mixes them into one 5-vector.

L1.3

Match each initialization to its RNN direction: and .

Recall Solution
  • seeds the forward RNN before the first word, so depends only on .
  • seeds the backward RNN after the last word, so depends only on . Look at figure s01: the two zero-boxes sit at the two ends of the strip.

Level 2 — Application

For L2 problems use the scalar (1-D) toy so you can compute by hand. Weights: Input sequence (scalars): . All hidden states are scalars, and the cell is .

L2.1

Compute the forward states (round to 4 decimals).

Recall Solution

Start .

  • What we did: fed each input plus the decayed memory of the previous state. What it looks like: each state is a point on the tanh S-curve, pulled toward the input's sign.

L2.2

Compute the backward states .

Recall Solution

Start , march right-to-left.

L2.3

Form the concatenated states and . Which coordinate of encodes the future of position 1?

Recall Solution
  • The second coordinate of is ; it carries information about — the future of position 1. The first coordinate () knows only .

Level 3 — Analysis

L3.1

A colleague replaces concatenation with summation, , to "save memory." Using the L2 numbers, show a case where two different pairs give the same summed state, illustrating information loss.

Recall Solution

Summation collapses a 2-vector to a scalar, so it is many-to-one. From L2, concatenated is , summing to . But the pair also sums to — a different balance of past/future that the downstream layer can no longer distinguish. Concatenation would keep them apart. Conclusion: summation is lossy; concatenation lets the output layer learn how to weight past vs future.

L3.2

Explain why the forward and backward RNNs use separate weight matrices. What representation would be forced if they shared weights?

Recall Solution

The forward RNN learns how information flows from the past; the backward RNN learns how it flows from the future — these are genuinely different statistical patterns (e.g. "a determiner precedes a noun" vs "an object follows a verb"). Sharing would force one compromise matrix to model both, degrading both. Separate parameters let each direction specialize. (Cost: roughly double the recurrent parameters.)

L3.3

Given hidden dim per direction and output classes, count parameters in the output layer for (a) a unidirectional RNN and (b) a BiRNN. Take .

Recall Solution

The classifier sees : dimension (uni) or (bi).

  • Uni: is , plus of size params.
  • Bi: is , plus of size params. The BiRNN's classifier grows because it reads a doubled feature vector.

Level 4 — Synthesis

L4.1

You want to translate a sentence into another language. Should the encoder be bidirectional? Should the decoder? Justify each with the causality argument.

Recall Solution
  • Encoder — yes, bidirectional. The whole source sentence is available, and encoding word 3 may need word 7 (word order differs across languages). See Encoder-Decoder Architecture and Sequence-to-Sequence Models.
  • Decoder — no. The decoder generates the target one token at a time; the future target tokens do not exist yet, so it is inherently causal. A bidirectional decoder is impossible at generation time. This asymmetry (bi-encoder, causal-decoder) is exactly what modern attention-based Transformers preserve.

L4.2

BiRNN cells suffer the same vanishing-gradient problem as vanilla RNNs over long sequences. Propose a fix that keeps the architecture bidirectional, and name the cell type.

Recall Solution

Replace each RNN cell with a gated cell — an LSTM or GRU — in both directions, giving a Bi-LSTM / Bi-GRU. The gates provide a near-linear "carry" path so gradients survive long sequences. See LSTM and GRU Cells. The bidirectional wiring (two passes, concatenate) is unchanged — only the cell internals change.

L4.3

For a Named Entity Recognition tagger (see Named Entity Recognition) over a sentence of length with per-direction hidden dim , how many hidden-state vectors are produced in total (before concatenation), and how many concatenated feed the classifier?

Recall Solution
  • Forward produces states, backward produces states → hidden vectors total.
  • They combine pairwise into concatenated states , each of dim , one per word, each getting a softmax tag.

Level 5 — Mastery

L5.1

Consider position in the L2 toy. Show explicitly that the concatenated state depends on every input , and identify which coordinate carries which inputs. Then compute .

Recall Solution

.

  • was built from (which saw ) plus → carries (the past).
  • was built from (which saw ) plus → carries (the future). Union: all three inputs influence . Numerically . This is precisely why a BiRNN can tag word 2 using full-sentence context.

L5.2

A full BiRNN classifier on the L2 toy uses output weights acting on (2 classes). Compute the logits, then the softmax probabilities (4 decimals). Which class wins?

Recall Solution

Logits :

  • Softmax: .
  • sum
  • Class 1 wins (). The softmax turned the raw logit gap into a valid probability distribution summing to .

L5.3

Design question. You have 1 000 000 unlabelled streaming sentences arriving in real time for a live autocomplete feature, and 50 000 fully written labelled sentences for offline NER. For each system, state uni- vs bidirectional and one sentence of justification.

Recall Solution
  • Live autocomplete: unidirectional (causal) — future words don't exist yet at typing time; a BiRNN cannot run.
  • Offline NER: bidirectional (ideally a Bi-LSTM) — the whole sentence is present and a token's tag depends on later words. This is the canonical BiRNN use case from the parent note.

Recall Quick self-check

A BiRNN needs the whole sequence available — true or false? ::: True; it cannot compute any backward state until the last input is seen. Concatenation vs summation — which preserves more info? ::: Concatenation (keeps past and future separate). Which fix keeps bidirectionality but stops vanishing gradients? ::: Swap the cell for an LSTM/GRU → Bi-LSTM.