From “a network needs a fixed-size input” to a character-level language model that writes “hello” one letter at a time. Every formula on this page is real LaTeX, rendered in your browser — not a screenshot of an equation.
| Question | Answer | Where |
|---|---|---|
| What problem does an RNN solve? | Data where order matters and length varies: sentences, audio, video frames. | Part 2 |
| What is the actual mechanism? | One hidden vector $\mathbf{h}_t$ updated from the previous $\mathbf{h}_{t-1}$ and the current input $\mathbf{x}_t$. | Part 3 |
| Why does it not blow up in parameters? | The same three matrices are reused at every timestep (weight sharing). | Part 4 |
| How is it trained? | Forward over the whole sequence, then backward over the whole sequence: BPTT. | Part 7 |
| What goes wrong? | Long sequences make the backward pass slow, memory-hungry, and prone to vanishing/exploding gradients. | Part 7 |
| What comes next? | GRU and LSTM — gates that decide what to forget (W13L1). | Part 8 |
The first ten slides are revision from W12L2 — convolutional networks applied to images. Two ideas from that half come back later, so they are worth pinning down here: a metric that is not accuracy, and normalisation.
Segmentation takes the whole spatial region $R$ of an image and cuts it into $n$ pieces. The definition on the slide is the mathematical one, and it is worth reading slowly because it says exactly what a segmentation must satisfy:
Read it in three pieces: the pieces cover everything (the union is the whole image — no pixel left behind), the pieces do not overlap (every pixel gets exactly one label), and every piece passes a predicate $Q$ — the rule that says “all pixels in this region belong to the same thing”. A segmentation that leaves a pixel unassigned, or gives it two labels, is not a segmentation; it is a bug with a colour map.
U-Net is the standard answer to “classify every pixel”. It has two halves: a contracting path (encoder) that repeatedly shrinks the image and extracts coarser features, and an expanding path (decoder) that grows it back to full resolution. The trick that makes the output sharp is the skip connection: at each level, the high-resolution features from the encoder are concatenated straight into the decoder, so the fine detail never has to survive the bottleneck.
If 98% of an image is background, a model that predicts “background” everywhere scores 98% accuracy and is useless. Dice ignores the true negatives entirely and only asks about the overlap between prediction $P$ and ground truth $G$:
All three forms are the same number. The first says “intersection over the average size” — which is where the factor 2 comes from. The third is the one you compute from a confusion matrix. It runs from 0 (no overlap) to 1 (perfect), and it equals the F1 score of the positive class — worth remembering, because the slide calls it “intersection over average” and never says F1.
Object detection outputs a bounding box $(x, y, w, h)$ and a class distribution for every object. On the slide, one branch of the network predicts class scores and another predicts four box coordinates.
IoU is the overlap divided by the union — 1.0 means the boxes coincide exactly, and a prediction is usually counted correct above a threshold such as 0.5. AP is the area under the precision–recall curve; mAP is that area averaged over classes. Note the asymmetry: IoU is used to decide whether a detection counts, while AP is used to summarise the whole ranking.
All four methods do the same arithmetic — subtract a mean, divide by a standard deviation — and differ only in which set of numbers the mean is taken over:
| Method | Statistics computed over | Best when |
|---|---|---|
| Batch norm | the mini-batch, per channel | large batches, CNNs |
| Layer norm | all features of one sample | sequences, small batches |
| Instance norm | one sample, one channel | style transfer |
| Group norm | one sample, a group of channels | small batches, detection |
$\gamma$ and $\beta$ are learned, so the network can undo the normalisation if it wants to — that is the part people forget. Normalisation does not remove information; it re-scales it and then learns the scale back.
A fully connected network takes a vector of fixed size. A CNN takes an image of fixed height and width. Both are fine until the data is a sentence — because the third word of a sentence has no meaning without the two before it, and because not every sentence has the same number of words.
The deck names three properties that make sequence data different:
Formally, a sequence is a list of vectors $\mathbf{x}^{(1)}, \mathbf{x}^{(2)}, \ldots, \mathbf{x}^{(T)}$ where $T$ changes from example to example. A batch is therefore a set of sequences of different lengths — which is already awkward for any framework that expects a rectangular tensor.
And the failure is not hypothetical. For video captioning the input is $T$ frames — a number that changes with the clip — and the output is a caption whose length also changes. A CNN can encode a frame; it cannot emit a variable-length sentence.
Here is the entire idea of the lecture, in one equation:
Read it as a sentence: the new state is some function of the previous state and the current input, and the function has parameters $W$. Nothing says what $f_W$ is yet — that is the next section. What matters here is the shape of the dependency: to compute step $t$ you need step $t-1$, and to compute $t-1$ you needed $t-2$. Unroll it and the same cell appears over and over:
Three consequences fall out immediately, and all three are exam material:
The general $f_W$ is made concrete with a linear layer and a $\tanh$:
Three matrices, three different jobs. The one that surprises people is $W_{hh}$: it multiplies the previous hidden state, so it is the part of the network that decides how much of the past to carry forward. Set it near zero and the network forgets instantly; make it large and the state saturates.
| Matrix | Shape | Multiplies | Job |
|---|---|---|---|
| $W_{xh}$ | $H \times D$ | input $\mathbf{x}_t$ | lift a $D$-dim input into the $H$-dim state space |
| $W_{hh}$ | $H \times H$ | previous state $\mathbf{h}_{t-1}$ | decide how the past carries forward |
| $W_{hy}$ | $K \times H$ | current state $\mathbf{h}_t$ | read a $K$-dim prediction out of the state |
Count the parameters and the reason for weight sharing becomes obvious:
Drag the three dimensions. The right-hand column is what a per-timestep (unshared) version would cost.
The slide lists four requirements for the activation: non-linear, easy to compute, differentiable, bounded output. $\tanh$ hits all four, and the bound is the interesting one — it keeps the state from growing without limit as the sequence gets longer.
That derivative is the whole story of Part 7. It is largest at $z=0$ (value 1) and vanishes as $|z|$ grows: once a unit is saturated, it stops passing gradient back.
Slide the input value. Watch the derivative collapse to zero outside roughly ±3.
Because $\mathbf{x}$, $\mathbf{h}$ and $\mathbf{y}$ are independent — you can feed one input or many, read one output or many — the same cell covers every standard task shape. The deck shows four:
This is the smallest complete RNN in the deck, and it is worth tracing end to end because every part of the machinery appears exactly once.
The data. Train on the string "hello", with vocabulary
$V \in \{\texttt{h}, \texttt{e}, \texttt{l}, \texttt{o}\}$ — four symbols, so $D = K = 4$.
The task is always the same: given the characters so far, predict the next one.
| t | input xt | target yt | what the model must learn |
|---|---|---|---|
| 1 | h | e | after "h", the only sensible continuation is e |
| 2 | e | l | after "he", l |
| 3 | l | l | after "hel", l again |
| 4 | l | o | after "hell", o — the same input as step 3, a different target |
That last row is the reason this is an RNN problem and not a lookup table. The input l
appears at steps 3 and 4 and must produce l in one case and o in the other.
A feed-forward network seeing only the current character cannot tell them apart. The hidden state
can, because at step 4 it also knows that a l has already happened.
The softmax turns four raw scores into a probability distribution; the loss is the negative log of the probability assigned to the correct next character. This is exactly the softmax classifier from the earlier lectures, applied once per timestep — and the total loss is those per-step losses added up.
Type a word (≤ 8 letters) and step through it. This shows the data flow and tensor shapes — the weights are not trained, so it deliberately does not pretend to predict anything.
These two loops are constantly confused, and the deck puts them side by side for that reason:
| Training | Sampling (test time) | |
|---|---|---|
| input at step t | the real character from the training string | the character the model just predicted |
| what you compare | prediction vs the real next character | nothing — there is no label |
| when it stops | after $T$ steps | when you decide (a length, or an end token) |
| error behaviour | one bad step does not poison the next, because the truth is fed in | one bad step is the next input — errors compound |
Because the same weights are used at every step, the gradient of the loss with respect to those weights is a sum over all timesteps:
And each of those terms has to travel back through every state between the step where the weight was used and the step where the loss was measured. Applying the chain rule along the chain of states:
where $\mathcal{J}_{k \to t}$ is the product of Jacobians that carries a signal from step $k$ all the way to step $t$. Look at that product. Every extra timestep multiplies the gradient by one more Jacobian. If each factor is slightly below 1, the product decays exponentially — the vanishing gradient. If each is slightly above 1, it explodes. Neither is a bug in the code; both are arithmetic.
Schematic, not a trained model: assumes each timestep multiplies the gradient by a fixed factor $r$. The point is the exponent, not the exact curve.
That is the learning problem. There is also a mechanical problem: to compute the backward pass you must keep every intermediate state from the forward pass alive, so memory grows linearly with sequence length, and the whole sequence must be processed before a single weight update. The slide's own annotation says it plainly — “slow and memory intensive, hardly converge.”
The fix is not to forget the past — it is to stop differentiating through all of it. Run forward and backward over chunks of length $\tau$ (typically 10–100 steps). Between chunks, the hidden state keeps its value but the computational graph is severed, so no gradient flows across the boundary.
| Full BPTT | Truncated BPTT | |
|---|---|---|
| memory | $O(T)$ — every state kept | $O(\tau)$ — only the current chunk |
| gradient | exact (for the whole sequence) | biased: dependencies longer than $\tau$ are invisible to the gradient |
| state | carried | still carried — the forward memory survives |
| cost per step | grows with $T$ | constant in $\tau$ |
The sentence to remember: truncation limits what the model can learn from, not what it can remember. The hidden state still contains information from the beginning of the sequence; the gradient simply stops being able to assign credit that far back.
An RNN layer outputs a sequence of states, so you can feed that sequence straight into another RNN layer. Depth helps empirically, exactly as it does in CNNs — the lower layer learns short-range patterns, the upper layer learns longer-range ones.
Next lecture (W13L1) picks up exactly where this one stops: GRU and LSTM. Both are the same recurrence with a gate added, and both exist because of the product of Jacobians in Part 7 — a gate is a learned way of deciding which factors in that product should be near 1 and which should be near 0.
| Wrong | Right |
|---|---|
| “The RNN stores the whole sequence.” | It stores a fixed-size summary $\mathbf{h}_t$; the past is compressed, not kept. |
| “Each timestep has its own weights.” | One set of weights, reused $T$ times. That is why the parameter count has no $T$ in it. |
| “Dice and IoU are the same.” | Dice $=2\mathrm{IoU}/(1+\mathrm{IoU})$. They rank identically but are not numerically equal. |
| “Vanishing gradients are a coding bug.” | They follow from multiplying many Jacobians $\lt 1$. Truncation and gates are the fixes. |
| “Truncated BPTT forgets the past.” | It keeps the state; it only cuts the gradient at the chunk boundary. |
| “Many-to-many is one architecture.” | The deck draws two: aligned (output every step) and encoder–decoder (output after reading). |
Tap any thumbnail to open it full size. The ones that carry the argument are also placed inline above, next to the explanation they belong to.






















































