Lecture 16 · Animations
Recurrent networks
Two looks inside a recurrent cell. The backward pass: what happens to a gradient carried back through 56 copies of the same matrix. And an LSTM cell holding a value across twenty steps, gate by gate. Press Step to advance one unit and read what happened.
Every input on this page is synthetic, generated in your browser from a fixed seed, and every weight is set by hand, not trained. The point is to watch the mechanism, so the weights are chosen to make it visible.
1The gradient through time
A two-unit cell is run forward for $T = 56$ steps, the deck's window. Then each Step carries the gradient one step further back, multiplying in one more Jacobian. The left plot is the size of $\partial\mathbf h_T/\partial\mathbf h_t$ against the distance $n = T - t$, on a log scale, next to $\rho^n$. The right plot shows the $\tanh'$ factor the backward pass meets at each step.
By the chain rule, with $\mathbf a_k$ the pre-activation at step $k$,
$$\frac{\partial\mathbf h_T}{\partial\mathbf h_t} = \prod_{k=t+1}^{T}\operatorname{diag}\bigl(\phi'(\mathbf a_k)\bigr)\,\mathbf W_h^{\intercal},$$the deck's one-step Jacobian, multiplied $n = T - t$ times, with the latest step on the left. Here $\mathbf W_h = \rho\,\mathbf Q$ with $\mathbf Q$ a rotation by 0.6 rad. Its eigenvalues are $\rho e^{\pm 0.6i}$, so its spectral radius is $\rho$, and since $\mathbf Q$ is orthogonal, $\lVert(\mathbf W_h^{\intercal})^n\rVert = \rho^n$ exactly.
$\lVert\cdot\rVert$ is the spectral norm, the largest singular value. For a $2\times 2$ matrix with entries $a, b, c, d$ it is $\sqrt{\bigl(s + \sqrt{s^2 - 4\det^2}\bigr)/2}$ with $s = a^2 + b^2 + c^2 + d^2$. It is submultiplicative, $\lVert\mathbf A\mathbf B\rVert \le \lVert\mathbf A\rVert\,\lVert\mathbf B\rVert$. Since $\tanh' = 1 - \tanh^2 \in (0, 1]$, every $\operatorname{diag}$ factor has norm at most 1, so with $\tanh$ the product is at most $\rho^n$.
The inputs are $x_t \sim \mathcal N(0, 0.2^2)$, with $\mathbf W_x^{\intercal}x_t = x_t\,(0.8, -0.5)$ and $\mathbf b = \mathbf 0$.
- With $\phi(a) = a$ the norm sits exactly on $\rho^n$: a straight line on the log scale, falling for $\rho < 1$ and rising for $\rho > 1$. At $\rho = 0.9$ it is about $0.003$ after 55 steps.
- With $\tanh$ it never rises above the dashed line, as the bound says, and saturated steps ($\tanh'$ well below 1) pull it further down.
- Linear, $\rho = 1.1$: after 55 steps the gradient is 190 times larger than at the start. Then $\rho = 0.9$: about 300 times smaller. A 10% change in one number moves the result by nearly five orders of magnitude.
- Switch to $\tanh$ with $\rho = 1.3$. The state saturates, $\tanh'$ drops, and the norm ends near 6 rather than the $\rho^{55} \approx 2\times 10^{6}$ of the linear cell. That is the bounded activation keeping the product from exploding.
2An LSTM cell, gate by gate
A one-unit LSTM given a memory task. At the write step it must store the value then on its input, then ignore twenty steps of distracting inputs. At the read step it must output what it stored. Each Step runs one time step. The plots show the inputs, the three gates, and the cell state $c_t$ next to the output $h_t$. The weights are set by hand so that every gate reads only the two flags.
An LSTM step, with each gate a sigmoid of $(\mathbf x_t, \mathbf h_{t-1})$:
$$\mathbf f_t = \sigma(\mathbf W_{xf}^{\intercal}\mathbf x_t + \mathbf W_{hf}^{\intercal}\mathbf h_{t-1} + \mathbf b_f),$$and likewise $\mathbf i_t$ and $\mathbf o_t$ with their own weights, and $\tilde{\mathbf c}_t$ with $\tanh$ in place of $\sigma$. Then
$$\mathbf c_t = \mathbf f_t\odot\mathbf c_{t-1} + \mathbf i_t\odot\tilde{\mathbf c}_t,$$ $$\mathbf h_t = \mathbf o_t\odot\tanh(\mathbf c_t).$$The input is $\mathbf x_t = (v_t, w_t, r_t)$: a value, a write flag and a read flag. Hand-set, with every $\mathbf W_{h\cdot} = \mathbf 0$:
$$f_t = \sigma(b_f - 14w_t),\quad i_t = \sigma(14w_t - 7),$$ $$o_t = \sigma(14r_t - 7),\quad \tilde c_t = \tanh(v_t).$$So the gates depend on $\mathbf x_t$ alone, and $\partial c_t/\partial c_{t-1} = f_t$ exactly. Across the whole wait, $\partial c_{t}/\partial c_{t_w} = \prod_{k=t_w+1}^{t} f_k$, which the side panel reports.
$\sigma(z) = 1/(1 + e^{-z})$, so $\sigma(7) \approx 0.99909$ and $\sigma(-7) \approx 0.00091$.
- At the write step, $f \approx 0$ clears the old state and $i \approx 1$ writes $\tilde c = \tanh(v)$. On every other step, $f \approx 0.999$ and $i \approx 0.001$, so $c$ barely moves while the input jumps about.
- $h_t$ stays near 0 until the read step, because $o \approx 0$. The memory is in $c$, which is not exposed until the output gate opens.
- The cell-state update is a sum, not a squashing. Twenty steps of $f \approx 0.999$ multiply the gradient along $c$ by about 0.98: it arrives almost intact.
- Lower the forget bias $b_f$ to 2, so $f \approx 0.88$ while waiting. After 20 steps only $0.88^{20} \approx 0.08$ of the stored value is left, and the read returns almost nothing. Remembering is the forget gate staying open.
- Move the read step earlier or later with $b_f = 7$. The output hardly depends on how long the cell waited.