---
title: "Problem set 7: sequences, attention, graphical models and approximate inference"
heading: "Problem set 7: recurrent and gated cells, gradients through time, transformers' parameters, arithmetic and caches, attention weights, Bayesian networks and explaining away, messages on chains, Laplace and variational approximations, and an EM step"
eyebrow: S14 · Problem set 7
type: problems
---

::: hook
These problems cover lessons 24 to 37, with the weight on sequences, attention, graphical models and approximate inference: sizing recurrent and transformer models and what they cost to train and serve, following gradients and attention weights through them, reasoning with Bayesian networks, and approximating posteriors that have no closed form.
:::

::: pool
pick: 12

::: check
kind: parts
given: H = 128 | 512; D = 40 | 256; L = 100 | 1000

## An LSTM layer with {H} hidden units reads {D}-dimensional inputs, over sequences {L} steps long.

::: part
answer: 4*(H*(H + D) + H)
unit: parameters
tolerance: 0

### How many parameters does it have?

::: misses
- [H*(H + D) + H] An LSTM has four blocks of weights.
- [any] $4[H(H + D) + H]$.
:::
:::

::: part
answer: 4*H*(H + D)*L/1e6
unit: million
tolerance: 0.5%

### About how many million multiply-adds does one sequence's forward pass take?

::: misses
- [any] $4H(H + D)$ per step.
:::
:::
:::

::: check
kind: parts
given: p = 0.8 | 0.95 | 1.05 | 1.2; T = 20 | 100; n = 30 | 500; c = 1 | 5

## Backward signals through a recurrent network change by about a factor of {p} per step. Separately, a gradient of norm {n} is clipped to {c}.

::: part
answer: p^T
tolerance: 0.5%

### By what factor does a signal change over {T} steps?

::: misses
- [any] $\rho^T$.
:::
:::

::: part
answer: c/n
tolerance: 0.5%

### By what factor does clipping scale the gradient?

::: misses
- [any] θ/‖g‖.
:::
:::
:::

::: check
kind: parts
given: t = 5 | 50 | 500

## An LSTM cell should start training with a memory time constant of {t} steps.

::: part
answer: exp(-1/t)
tolerance: 0.5%

### What forget-gate value gives that?

::: misses
- [any] $f^\tau = e^{-1}$.
:::
:::

::: part
answer: ln(exp(-1/t)/(1 - exp(-1/t)))
tolerance: 0.5%

### What forget-gate bias produces it?

::: misses
- [any] Invert the sigmoid: $b = \ln[f/(1 - f)]$.
:::
:::
:::

::: check
kind: parts
given: d = 1024 | 2048 | 4096; L = 24 | 32; V = 32000 | 128000; N = 0.3 | 2; u = 0.4 | 0.5

## A transformer has width {d}, {L} layers and a {V}-token vocabulary, and trains on {N} trillion tokens on hardware sustaining {u} of a 1000 TFLOP/s peak.

::: part
answer: (12*d^2*L + V*d)/1e9
unit: billion
tolerance: 0.5%

### How many billion parameters does it have?

::: misses
- [12*d^2*L/1e9] Include the embedding.
- [any] $12d^2L + Vd$.
:::
:::

::: part
answer: 6*(12*d^2*L + V*d)*N*1e12/(u*1000e12)/86400
unit: days
tolerance: 0.5%

### How many days does training take?

::: misses
- [any] About 6 operations per parameter per token.
:::
:::
:::

::: check
kind: parts
given: L = 32 | 80; d = 4096 | 8192; n = 8192 | 32768; G = 80 | 192; W = 14 | 70

## A model with {L} layers of width {d} serves {n}-token contexts with a 16-bit key–value cache, on a device with {G} GB of memory, {W} GB of which hold the weights.

::: part
answer: 2*L*d*2*n/1e9
unit: GB
tolerance: 0.5%

### How much cache does one sequence need?

::: misses
- [L*d*2*n/1e9] Keys and values are both cached.
- [any] $2Ld \times 2$ bytes per token.
:::
:::

::: part
answer: floor((G - W)/(2*L*d*2*n/1e9))
unit: sequences
tolerance: 0

### How many sequences fit at once?

::: misses
- [any] Free memory over the cache per sequence, rounded down.
:::
:::
:::

::: check
kind: parts
given: d = 64 | 256; a = 24 | 40; b = 8 | 16

## A query's raw dot products with two keys are {a} and {b}, in {d} dimensions.

::: part
answer: 1/(1 + exp((b - a)/sqrt(d)))
tolerance: 0.5%

### What attention weight does the first key get?

::: misses
- [1/(1 + exp(b - a))] Divide the scores by $\sqrt d$ first.
- [a/(a + b)] Attention uses a softmax, not a ratio of scores.
- [any] $\text{softmax}(s/\sqrt d)$.
:::
:::
:::

::: check
kind: parts
given: d = 768 | 2048; n = 1024 | 16384 | 131072

## A transformer layer of width {d} attends over {n} positions.

::: part
answer: 4*n*d/(24*d^2 + 4*n*d)
tolerance: 0.5%

### What fraction of the layer's per-token arithmetic is attention itself?

::: misses
- [any] $4nd/(24d^2 + 4nd)$.
:::
:::
:::

::: check
kind: parts
given: K = 2 | 3 | 4; A = 2 | 5; B = 3 | 4

## In a Bayesian network, a node with {K} states has two parents with {A} and {B} states.

::: part
answer: (K - 1)*A*B
unit: parameters
tolerance: 0

### How many free parameters does its conditional table need?

::: misses
- [K*A*B] Each column sums to 1.
- [any] $(K - 1)AB$.
:::
:::

::: part
answer: K*A*B - 1
unit: parameters
tolerance: 0

### How many would a full joint table over the three variables need?

::: misses
- [any] $KAB - 1$.
:::
:::
:::

::: check
kind: parts
given: a = 0.02 | 0.1; b = 0.05 | 0.2; q = 0.9 | 0.99; r = 0.8 | 0.95; l = 0.001 | 0.01

## Two independent faults (priors {a} and {b}) trip an alarm with probabilities {q} and {r} (noisy-OR), which also trips on its own with probability {l}.

::: part
answer: a*(1 - (1 - l)*(1 - q)*(1 - b*r))/(a*(1 - (1 - l)*(1 - q)*(1 - b*r)) + (1 - a)*(1 - (1 - l)*(1 - b*r)))
tolerance: 0.5%

### What is the first fault's probability given the alarm?

::: misses
- [any] $P(E|A)$ and $P(E|\bar A)$ average over the second fault.
:::
:::

::: part
answer: a*(1 - (1 - l)*(1 - q)*(1 - r))/(a*(1 - (1 - l)*(1 - q)*(1 - r)) + (1 - a)*(1 - (1 - l)*(1 - r)))
tolerance: 0.5%

### And given the alarm and the second fault?

::: misses
- [any] Condition on the second fault being present.
:::
:::
:::

::: check
kind: parts
given: s = 0.8 | 0.95; u = 0.1 | 0.3

## A two-state chain stays in state 1 with probability {s} and moves from state 2 to 1 with probability {u}. It's observed in state 1 at time 1.

::: part
answer: s^2 + (1 - s)*u
tolerance: 0.5%

### What is the probability it's in state 1 at time 3?

::: misses
- [s^2] It can also return to state 1 through state 2.
- [any] Sum over the state at time 2.
:::
:::
:::

::: check
kind: parts
given: a = 1 | 2; b = 10 | 100; k = 3 | 15; T = 50 | 500

## Counts are Poisson with rate λ; λ has a gamma({a}, {b}) prior, and {k} events occur in time {T}.

::: part
answer: (a + k - 1)/(b + T)
tolerance: 0.5%

### Where is the Laplace approximation centered?

::: misses
- [(a + k)/(b + T)] That's the posterior mean; Laplace uses the mode.
- [any] The mode of gamma(α + k, β + T).
:::
:::

::: part
answer: sqrt(a + k - 1)/(b + T)
tolerance: 0.5%

### What standard deviation does it give?

::: misses
- [sqrt(a + k)/(b + T)] That's the exact standard deviation.
- [any] $\sqrt{\alpha' - 1}/\beta'$.
:::
:::
:::

::: check
kind: parts
given: s = 1 | 4; u = 0.5 | 2; r = 0.6 | 0.9

## A Gaussian posterior has standard deviations {s} and {u} and correlation {r}; a mean-field approximation minimizes KL(q‖p).

::: part
answer: s*sqrt(1 - r^2)
tolerance: 0.5%

### What standard deviation does the first factor get?

::: misses
- [s] Mean field underestimates the marginal.
- [any] $1/\sqrt{\Lambda_{11}} = \sigma_1\sqrt{1 - \rho^2}$.
:::
:::

::: part
answer: u*sqrt(1 - r^2)
tolerance: 0.5%

### And the second?

::: misses
- [u] Mean field underestimates this marginal too.
- [any] $\sigma_2\sqrt{1 - \rho^2}$.
:::
:::

::: part
answer: 0.5*ln(1/(1 - r^2))
unit: nats
tolerance: 0.5%

### What is KL(q‖p)?

::: misses
- [any] $\frac12\ln[1/(1 - \rho^2)]$, whatever the scales.
:::
:::
:::

::: check
kind: parts
given: D = 8 | 32; m = 0.2 | 1; s = 0.3 | 0.8; R = -95 | -180

## A VAE encoder outputs μ = {m} and σ = {s} in each of {D} latent dimensions, and the decoder's reconstruction log-likelihood is {R} nats.

::: part
answer: D*0.5*(m^2 + s^2 - 1 - ln(s^2))
unit: nats
tolerance: 0.5%

### What is the KL term?

::: misses
- [any] $\frac D2(\mu^2 + \sigma^2 - 1 - \ln\sigma^2)$.
:::
:::

::: part
answer: R - D*0.5*(m^2 + s^2 - 1 - ln(s^2))
unit: nats
tolerance: 0.5%

### What is the ELBO?

::: misses
- [R + D*0.5*(m^2 + s^2 - 1 - ln(s^2))] Subtract the KL term.
- [any] Reconstruction minus KL.
:::
:::
:::

::: check
kind: parts
given: f = 1 | 2; n = 0.1 | 0.5; t = 1 | 3

## A Gaussian process with prior variance {f} and noise variance {n} observes t = {t} at one input.

::: part
answer: f*t/(f + n)
tolerance: 0.5%

### What does it predict for the function at that same input?

::: misses
- [t] The noise means the observation is shrunk toward the prior mean.
- [any] $\sigma_f^2t/(\sigma_f^2 + \sigma^2)$.
:::
:::

::: part
answer: f*n/(f + n)
tolerance: 0.5%

### What is the function's posterior variance there?

::: misses
- [any] $\sigma_f^2 - \sigma_f^4/(\sigma_f^2 + \sigma^2)$.
:::
:::
:::

::: check
kind: parts
given: x = 0 | 0.5; y = 1.5 | 2; z = 4 | 5; a = 0 | 1; b = 3 | 4

## A mixture of two unit-variance Gaussians with equal weights has means {a} and {b}. EM sees the points {x}, {y} and {z}.

::: part
answer: 1/(1 + exp(((y - a)^2 - (y - b)^2)/2))
tolerance: 0.5%

### What responsibility does the first component take for {y}?

::: misses
- [any] $1/(1 + e^{[(y - a)^2 - (y - b)^2]/2})$.
:::
:::

::: part
answer: (x/(1 + exp(((x - a)^2 - (x - b)^2)/2)) + y/(1 + exp(((y - a)^2 - (y - b)^2)/2)) + z/(1 + exp(((z - a)^2 - (z - b)^2)/2)))/(1/(1 + exp(((x - a)^2 - (x - b)^2)/2)) + 1/(1 + exp(((y - a)^2 - (y - b)^2)/2)) + 1/(1 + exp(((z - a)^2 - (z - b)^2)/2)))
tolerance: 0.5%

### What is the first component's new mean?

::: misses
- [any] The responsibility-weighted average of the points.
:::
:::
:::
:::
