Sequence Models
Level 4 (Application: Novel Problems, No Hints)
Time limit: 60 minutes
Total marks: 60
Answer all questions. Show all working. Calculators permitted for arithmetic. Give numeric answers to 4 decimal places where appropriate.
Question 1 — RNN forward pass & BPTT gradient (14 marks)
A vanilla RNN uses (no bias). You are given scalars (single-unit RNN): , , . The input sequence is , . The scalar loss is with target .
(a) Compute and to 4 decimal places. (4)
(b) Using backpropagation through time, derive symbolically in terms of the computed activations, then evaluate it numerically. (7)
(c) State one reason why, for a 100-step version of this same RNN with , the gradient contribution from step 1 to the loss at step 100 would be negligible. (3)
Question 2 — LSTM cell computation (14 marks)
Consider a single-unit LSTM at one time step with the standard equations:
You are given the pre-activation values , , , , and previous cell state .
(a) Compute (4 dp). (4)
(b) Compute and (4 dp). (4)
(c) Suppose during training the forget gate saturates so and across many steps. Explain what happens to the cell state and why this mitigates the vanishing-gradient problem that afflicts vanilla RNNs. (6)
Question 3 — Attention weights (Luong dot-product) (12 marks)
An encoder produces 3 hidden states , , . The decoder state at the current step is . Luong (dot) attention scores each encoder state via .
(a) Compute the three raw scores. (3)
(b) Compute the softmax attention weights (4 dp). (5)
(c) Compute the context vector . (4)
Question 4 — Beam search decoding (10 marks)
A decoder has vocabulary . Using beam width , the log-probabilities of the next token given the prefix are:
Step 1 (from start): .
Step 2 log-probs given the step-1 token:
- given "A":
- given "B":
(a) After step 1, list the 2 beams kept and their cumulative scores. (3)
(b) Expand both beams and list all candidate sequences of length 2 with cumulative log-probs. (4)
(c) Which 2 length-2 sequences survive to the next beam? State the top sequence. (3)
Question 5 — Design / conceptual application (10 marks)
You are building a machine-translation system (English→French) with an encoder–decoder RNN.
(a) Your training uses teacher forcing but inference does not. Describe the resulting exposure bias problem and one concrete mitigation. (4)
(b) Input sentences vary from 3 to 40 tokens. Explain how you batch them efficiently and why masking is required in the loss. (3)
(c) Give one concrete reason to prefer a bidirectional RNN in the encoder but not in the decoder. (3)
Answer keyMark scheme & solutions
Question 1 (14 marks)
(a) — (2) — (2)
(b) Loss , . (1)
Chain through both time steps (BPTT). Let .
- (the term). (2)
- . (1)
- (since ). (1)
So the second path vanishes. Numerically: . . (2)
(c) The gradient propagated back steps scales like ; each factor and , so over 100 steps the product — exponentially small ⇒ vanishing gradient, negligible contribution. (3)
Question 2 (14 marks)
(a) — (1) — (1) — (1) — (1)
(b) — (2) — (2)
(c) With : , so the cell state is copied nearly unchanged forward (a "constant error carousel"). — (2) The gradient of w.r.t. is , so backpropagated gradient through the cell path is multiplied by ~1 each step instead of by a factor (as in the Jacobian of vanilla RNNs). — (2) Hence gradients do not shrink exponentially over long time lags, letting the LSTM learn long-range dependencies. — (2)
Question 3 (12 marks)
(a) ; ; . (3)
(b) ; sum . (2) ; . (3) (check: )
(c) ; . . (4)
Question 4 (10 marks)
(a) Top-2 of step 1: "A" (), "B" (). "
(b) Expand (cumulative = prefix + step2):
- A→A:
- A→B:
- A→
: - B→A:
- B→B:
- B→
: (4)
(c) Rank all: A→B , B→A , A→A , B→
Question 5 (10 marks)
(a) Teacher forcing always feeds the ground-truth previous token during training, but at inference the model feeds its own (possibly wrong) predictions. The model never learned to recover from its own errors ⇒ errors compound (exposure bias). Mitigation: scheduled sampling (gradually replace ground-truth with model predictions during training), or beam search / minimum-risk / sequence-level training. (4) (2 for problem, 2 for mitigation)
(b) Pad all sequences in a batch to the max length (or bucket by similar length to reduce padding), use a mask marking real vs pad tokens; the loss is computed only over non-pad positions (mask-weighted) so padding does not contribute spurious gradients/loss. (3)
(c) Encoder sees the whole input at once, so a bidirectional pass lets each encoded state incorporate both past and future context ⇒ richer representations. The decoder generates tokens left-to-right and cannot see future outputs at inference (they don't exist yet), so a backward pass is impossible/causally invalid. (3)
[
{"claim":"Q1a h1 and h2 values", "code":"import sympy as sp; h1=sp.tanh(1.0); h2=sp.tanh(0.5*h1-0.5); result = (round(float(h1),4)==0.7616 and round(float(h2),4)==-0.1186)"},
{"claim":"Q1b dL/dWh = -0.0891", "code":"import sympy as sp; h1=sp.tanh(1.0); h2=sp.tanh(0.5*h1-0.5); g=h2*(1-h2**2)*h1; result = round(float(g),4)==-0.0891"},
{"claim":"Q2 LSTM c_t and h_t", "code":"import sympy as sp; f=sp.sigmoid(2.0); i=sp.sigmoid(0.0); o=sp.sigmoid(1.0); ct=f*0.8+i*sp.tanh(-1.0); ht=o*sp.tanh(ct); result = (round(float(ct),4)==0.3238 and round(float(ht),4)==0.2288)"},
{"claim":"Q3 attention weights and context", "code":"import sympy as sp; import math; s=[math.exp(1),math.exp(1),math.exp(2)]; Z=sum(s); a=[x/Z for x in s]; cx=a[0]*1+a[1]*0+a[2]*1; cy=a[0]*0+a[1]*1+a[2]*1; result = (round(a[2],4)==0.5761 and round(a[0],4)==0.2119 and round(cx,4)==0.7881 and round(cy,4)==0.7881)"},
{"claim":"Q4 beam survivors A->B and B->A top scores", "code":"cands={'AA':-1.7,'AB':-1.2,'Ae':-2.5,'BA':-1.4,'BB':-2.5,'Be':-2.0}; top2=sorted(cands.items(), key=lambda kv:-kv[1])[:2]; result = ([k for k,v in top2]==['AB','BA'])"}
]