Sequence Models
Time limit: 90 minutes Total marks: 60 Instructions: Answer all THREE questions. Show all derivations. Notation: is the logistic sigmoid, is elementwise (Hadamard) product, is the spectral norm unless stated.
Question 1 — Vanishing gradients: proof + numerics (20 marks)
Consider a vanilla RNN with hidden update
(a) Derive the Jacobian and show that the gradient of a loss at time w.r.t. an early state factorises as a product of Jacobians. Write the product explicitly. (4)
(b) Let . Prove the bound Hence give a sufficient condition on that guarantees the gradient contracts geometrically to zero, and a necessary condition (via largest singular value) for it to not vanish. (6)
(c) Take the scalar case with , and suppose at every step . Compute the multiplicative gradient factor for a span of steps. Give the value and state whether this vanishes. (4)
(d) LSTMs mitigate this via the cell-state path . Show (ignoring the indirect dependence through gates), and explain in two sentences why a forget gate near preserves gradient flow where the tanh path does not. (6)
Question 2 — Attention mechanism: build and analyse (22 marks)
An encoder produces annotations . A decoder state is .
(a) Bahdanau (additive) attention scores: . Luong (dot) attention: . State one computational and one representational difference between them, and explain why Bahdanau uses the previous decoder state while Luong uses the current . (6)
(b) Attention weights are and context . Derive (the softmax Jacobian) and give it in closed form. (4)
(c) Numerical: with scores , compute the attention weights (to 3 decimals) and the context as a convex combination coefficient vector. Verify . (6)
(d) Show that if all scores are equal, attention reduces to a uniform average , and argue why this is the desired "no information" prior. Then show that as the gap , attention converges to a hard (hard attention). (6)
Question 3 — Seq2seq decoding: beam search build + complexity (18 marks)
A seq2seq model outputs, at each step, a distribution over a vocabulary . We decode with beam search, beam width , using length-normalised log-probability score.
(a) Give the beam-search update rule: from live hypotheses, how many candidate extensions are generated, and how are the top- selected? Why do we sum log-probabilities rather than multiply probabilities? (4)
(b) Worked example. Vocabulary , beam width , start empty. Step-1 token probs: . Step-2 conditional probs:
| prefix | ||
|---|---|---|
| A | 0.7 | 0.3 |
| B | 0.5 | 0.5 |
Enumerate all length-2 sequences with their probabilities, and identify the two beams kept after step 2 (highest joint probability). State the single most probable sequence. (8)
(c) State the time complexity of beam search over steps as a function of , , . Contrast with exhaustive search complexity and with greedy (). Give one reason length-normalisation is applied before final ranking. (6)
Answer keyMark scheme & solutions
Question 1
(a) [4] Since with , by chain rule (2) Loss gradient telescopes: (2)
(b) [6] Submultiplicativity of the spectral norm: . Applying to the product: (3) Since , . Hence if the bound is : sufficient condition gives geometric contraction (vanishing). (2) Necessary (non-vanishing) condition: the largest singular value (and the not too small), otherwise the product norm is bounded above by decaying to . (1)
(c) [4] Scalar factor per step . (2) Over 10 steps: . (1) This is , so the gradient vanishes. (1)
(d) [6] . Treating as constants w.r.t. (ignoring gate dependence), (3) The product over time is ; if the product stays near , so gradients neither vanish nor explode — a constant error carousel. The tanh path instead multiplies by with and typically , giving geometric decay. (3)
Question 2
(a) [6]
- Computational: Bahdanau requires a small MLP (, a tanh) per (query,key) pair — more parameters/FLOPs; Luong dot-product has no extra parameters and is cheaper/parallelisable. (2)
- Representational: additive scoring can align keys/queries of different subspaces and is more expressive for small dims; dot-product assumes query and key live in the same space and scale. (2)
- Bahdanau computes attention using because the context is fed into the RNN cell to produce (attention precedes the recurrence). Luong computes attention with because it applies attention after the RNN update, then combines context with . (2)
(b) [4] Softmax . i.e. . Derivation: for , quotient rule gives ; for , . (4)
(c) [6] Exponentials: . Sum . (2) , , . (3) Sum . Context . (1)
(d) [6] If for all : , so , uniform average. (3) This is the maximum-entropy / least-committal distribution — with no evidence favouring any position, equal weight avoids bias. (1) Let be the max with gap . Then , all others : attention selects a single position — hard attention / . (2)
Question 3
(a) [4] From live hypotheses each is extended by every token in , giving candidates. Score each by (length-normalised) cumulative log-prob; keep the top overall as the new beam. (2) We sum log-probabilities because the joint probability is a product ; multiplying many probabilities underflows numerically and log turns it into a numerically stable sum. (2)
(b) [8] Joint probabilities:
- (4)
Sorted: . Beams kept after step 2 (top 2): (ties broken to ; equally valid). (3) Most probable sequence: with . (1)
(c) [6] Per step we score candidates and re-select top- (cost or ); over steps total (plus decoder forward passes). (2) Exhaustive search is — exponential; greedy is the special case, cheapest but no back-up hypotheses. (2) Length-normalisation (dividing log-prob by length, or length) is applied before final ranking because raw joint log-prob is a sum of negative terms that grows in magnitude with length, biasing beam search toward shorter sequences; normalising makes hypotheses of different lengths comparable. (2)
[
{"claim":"Q1c gradient factor 0.4^10 approx 1.048576e-4",
"code":"val=(Rational(8,10)*Rational(5,10))**10; result=abs(float(val)-1.048576e-4)<1e-9"},
{"claim":"Q2c softmax weights of (2,1,0) sum to 1 and alpha1 approx 0.665",
"code":"import math; e=[math.exp(2),math.exp(1),math.exp(0)]; s=sum(e); a=[x/s for x in e]; result=(abs(sum(a)-1)<1e-9) and (abs(a[0]-0.665)<1e-3)"},
{"claim":"Q3b joint probabilities AA=0.42 max, AB=0.18",
"code":"AA=Rational(6,10)*Rational(7,10); AB=Rational(6,10)*Rational(3,10); BA=Rational(4,10)*Rational(5,10); result=(AA==Rational(42,100)) and (AB==Rational(18,100)) and (AA>BA) and (AA>AB)"},
{"claim":"Q2b softmax Jacobian diagonal entry equals alpha_i(1-alpha_i)",
"code":"a1=symbols('a1'); result=simplify(a1*(1-a1)-(a1-a1**2))==0"}
]