Blame
|
1 | # 14. LSTM and GRU |
||||||
| 2 | ||||||||
| 3 | A plain recurrent network struggles to carry information across many time steps because repeated multiplication by the same weight matrix makes gradients vanish or explode. Gated recurrent cells fix this by adding a state that flows through time with mostly additive updates, controlled by learned gates. This module builds the long short-term memory (LSTM) cell and the lighter gated recurrent unit (GRU), and contrasts when to reach for each. |
|||||||
| 4 | ||||||||
| 5 | **Objectives** |
|||||||
| 6 | - Explain why a gated cell state preserves long-range gradient flow (the constant error carousel). |
|||||||
| 7 | - Write the three LSTM gates as sigmoids of an affine map of the concatenated input. |
|||||||
| 8 | - Derive the LSTM candidate, cell update, and hidden state. |
|||||||
| 9 | - Write the GRU reset and update gates and its interpolated hidden state. |
|||||||
| 10 | - Compare LSTM and GRU on gate count, cell state, parameter count, and typical use. |
|||||||
| 11 | ||||||||
| 12 | ## 14.1 The gating idea |
|||||||
| 13 | ||||||||
| 14 | A vanilla recurrent layer updates its hidden state by $h_t = g(W_h h_{t-1} + W_x x_t + b)$. Backpropagating the loss through $T$ steps multiplies many Jacobians of this map together, so the gradient magnitude scales roughly like the $T$-th power of the recurrent weight's spectral radius. Below one it vanishes, above one it explodes, and in both cases the network cannot learn dependencies that span many steps. |
|||||||
| 15 | ||||||||
| 16 | The gating idea introduces a separate **cell state** $c_t$ that is updated mainly by addition rather than by a full matrix multiply. When the update leaves the previous cell state untouched, the gradient of $c_t$ with respect to $c_{t-1}$ is close to the identity, so error signals flow backwards over long spans without shrinking. This near-identity path is the **constant error carousel**. |
|||||||
| 17 | ||||||||
| 18 | *Remark:* the key word is additive. Multiplicative recurrence compounds a factor at every step, while an additive path lets the state persist by default and change only when a gate opens. |
|||||||
| 19 | ||||||||
| 20 | ## 14.2 The LSTM cell |
|||||||
| 21 | ||||||||
| 22 | Throughout, $[h_{t-1}, x_t]$ denotes the concatenation of the previous hidden state and the current input into one vector. Each gate is a vector in $(0, 1)$ produced by a sigmoid $\sigma$ applied to an affine map of that concatenation, so a gate value near $1$ lets information through and a value near $0$ blocks it. |
|||||||
| 23 | ||||||||
| 24 | ### 14.2.1 The three gates |
|||||||
| 25 | ||||||||
| 26 | The **forget** gate $f_t$ decides how much of the old cell state to keep, the **input** gate $i_t$ decides how much of the new candidate to write, and the **output** gate $o_t$ decides how much of the cell state to expose as the hidden state: |
|||||||
| 27 | ||||||||
| 28 | $$\boxed{ f_t = \sigma\!\left(W_f\,[h_{t-1}, x_t] + b_f\right), \quad i_t = \sigma\!\left(W_i\,[h_{t-1}, x_t] + b_i\right), \quad o_t = \sigma\!\left(W_o\,[h_{t-1}, x_t] + b_o\right) }$$ |
|||||||
| 29 | ||||||||
| 30 | *Remark:* the gates share the same functional form and differ only in their learned parameters. The bias is explicit here, exactly as with the feedforward layers of earlier modules, and is never folded into the weight matrix. |
|||||||
| 31 | ||||||||
| 32 | ### 14.2.2 Candidate and cell update |
|||||||
| 33 | ||||||||
| 34 | A $\tanh$ layer proposes a **candidate** update $\tilde{c}_t$, the new content the cell could store: |
|||||||
| 35 | ||||||||
| 36 | $$\boxed{ \tilde{c}_t = \tanh\!\left(W_c\,[h_{t-1}, x_t] + b_c\right) }$$ |
|||||||
| 37 | ||||||||
| 38 | The cell state is then updated by keeping a gated fraction of the past and adding a gated fraction of the candidate, with $\odot$ the elementwise (Hadamard) product: |
|||||||
| 39 | ||||||||
| 40 | $$\boxed{ c_t = f_t \odot c_{t-1} + i_t \odot \tilde{c}_t }$$ |
|||||||
| 41 | ||||||||
| 42 | When $f_t \approx 1$ and $i_t \approx 0$ the cell simply copies $c_{t-1}$, which is the constant error carousel: $\partial c_t / \partial c_{t-1} \approx \mathrm{diag}(f_t)$, so gradients pass through nearly unattenuated. |
|||||||
| 43 | ||||||||
| 44 | ### 14.2.3 Hidden state |
|||||||
| 45 | ||||||||
| 46 | The hidden state is the squashed cell state, gated by the output gate: |
|||||||
| 47 | ||||||||
| 48 | $$\boxed{ h_t = o_t \odot \tanh(c_t) }$$ |
|||||||
| 49 | ||||||||
| 50 | *Remark:* the cell state $c_t$ is the long-term memory that flows along the carousel, while the hidden state $h_t$ is the filtered view exposed to the next layer and to the output at this step. Keeping them separate is what distinguishes the LSTM from the GRU below. |
|||||||
| 51 | ||||||||
| 52 | ## 14.3 The GRU |
|||||||
| 53 | ||||||||
| 54 | The GRU merges the cell and hidden state into a single $h_t$ and uses only two gates, so it has fewer parameters while keeping the additive-update benefit. |
|||||||
| 55 | ||||||||
| 56 |  |
|||||||
| 57 | ||||||||
| 58 | *The GRU merges the cell and hidden state and uses just a reset and an update gate.* |
|||||||
| 59 | ||||||||
| 60 | ### 14.3.1 Reset and update gates |
|||||||
| 61 | ||||||||
| 62 | The **reset** gate $r_t$ controls how much past state feeds the candidate, and the **update** gate $z_t$ controls how much of the state to refresh: |
|||||||
| 63 | ||||||||
| 64 | $$\boxed{ r_t = \sigma\!\left(W_r\,[h_{t-1}, x_t] + b_r\right), \quad z_t = \sigma\!\left(W_z\,[h_{t-1}, x_t] + b_z\right) }$$ |
|||||||
| 65 | ||||||||
| 66 | ### 14.3.2 Candidate and interpolated state |
|||||||
| 67 | ||||||||
| 68 | The candidate uses a reset-gated version of the previous hidden state, and the new state is a gated interpolation between the old state and the candidate: |
|||||||
| 69 | ||||||||
| 70 | $$\boxed{ \tilde{h}_t = \tanh\!\left(W\,[\,r_t \odot h_{t-1}, \; x_t\,]\right), \quad h_t = (1 - z_t) \odot h_{t-1} + z_t \odot \tilde{h}_t }$$ |
|||||||
| 71 | ||||||||
| 72 | *Remark:* the interpolation form ties the keep and write fractions together with a single gate: whatever weight $z_t$ gives the candidate, $1 - z_t$ is left for the past. The LSTM sets its keep fraction $f_t$ and write fraction $i_t$ independently, which is one more gate and one more matrix. |
|||||||
| 73 | ||||||||
| 74 | ## 14.4 LSTM versus GRU |
|||||||
| 75 | ||||||||
| 76 | Both cells solve the vanishing-gradient problem with an additive state path. They differ in how many gates carry that path and whether the long-term memory is kept separate from the exposed state. |
|||||||
| 77 | ||||||||
| 78 | | Aspect | LSTM | GRU | |
|||||||
| 79 | | --- | --- | --- | |
|||||||
| 80 | | Gates | 3 (forget, input, output) | 2 (reset, update) | |
|||||||
| 81 | | Separate cell state | yes ($c_t$ and $h_t$) | no (single $h_t$) | |
|||||||
| 82 | | Parameters per unit | more (four affine maps) | fewer (three affine maps) | |
|||||||
| 83 | | Keep and write | independent ($f_t$, $i_t$) | tied ($z_t$ and $1 - z_t$) | |
|||||||
| 84 | | Prefer when | long dependencies, ample data and compute | smaller data, faster training, similar accuracy | |
|||||||
| 85 | ||||||||
| 86 | *Remark:* in practice the two often reach comparable accuracy. The GRU trains faster and generalizes well on smaller datasets, while the extra capacity of the LSTM can help on very long sequences. Treat the choice as a tunable hyperparameter rather than a settled rule. |
|||||||
| 87 | ||||||||
| 88 | ## 14.5 Anatomy of a gated cell |
|||||||
| 89 | ||||||||
| 90 | The diagram traces one LSTM step: the previous cell state enters on the additive path, the gates modulate what is forgotten, written, and exposed, and the outputs feed the next step. |
|||||||
| 91 | ||||||||
| 92 |  |
|||||||
| 93 | ||||||||
| 94 | *The LSTM cell carries a cell state along the top, edited by a forget multiply and an input add, with sigmoid gates controlling the flow.* |
|||||||
| 95 | ||||||||
| 96 | *Remark:* the horizontal path from previous cell state to new cell state is the carousel, and it carries no full matrix multiply, only the elementwise gate products. |
|||||||
| 97 | ||||||||
| 98 | *Gates let a recurrent state persist over long spans, but they still read one step at a time. The next part lets every position attend directly to every other, removing the sequential bottleneck.* |
|||||||
| 99 | ||||||||
| 100 | --- |
|||||||
| 101 | Next: [Attention](/en/Deep%20Learning/15%20Attention) · [Course overview](/en/Deep%20Learning) |
|||||||
