Search

Search papers and lessons

Gated Recurrent Unit

Level 4 · 11 min read

The gated recurrent unit is a simpler LSTM: two gates decide how much of the old hidden state to keep and how much to replace, so useful signals survive over many more steps.

On this page

  • Colour = topic mix
  • Calculus
  • Neural network basics
  • Linear algebra
  • Probability
  • Architectures
  • Discrete math
  • Sign in to see your progress

TL;DR

The gated recurrent unit (GRU) is a recurrent cell with the same shape as a vanilla recurrent network, plus two gates. The update gate decides how much of the old hidden state to keep and how much to replace with new content. The reset gate decides how much of the old state the new content is allowed to look at. Because the old state can pass through almost unchanged, the learning signal survives across many more steps.

The GRU gets most of the benefit of the LSTM with fewer parts, which is why it is often the first gated cell people try.

Where you'll see it: in encoder and decoder networks for machine translation, in speech and handwriting recognition models, and in sequence models small enough that an LSTM would be too heavy.

Intuition

A notepad with two erasers

Recall the notepad picture from the recurrent network: read a word, combine it with what is on the page, and rewrite the page. The trouble is that a vanilla rewrite replaces the whole page every time. Anything written early has to survive a fresh rewrite at every word, and it usually gets smudged.

A GRU changes how the page is rewritten. Before writing the new draft, you decide how much of the old page to look at. That is the reset gate. Then, line by line, you decide how much of the old page to keep and how much to replace with the draft. That is the update gate. Keeping a line costs nothing: the old words stay exactly as they were. A line changes only when you choose to replace it.

That is the key idea. If the update gate is close to zero for some line, that line is copied forward untouched across many steps, and a learning signal can travel backwards along the same untouched path. The gates let the network choose when to remember.

Why not just use the LSTM?

The LSTM solved the same problem with a separate cell state and three gates. The GRU asks a simpler question: can the memory and the output share one vector, with two gates doing the work? In many experiments the answer is yes, closely enough that the simpler model wins on speed and parameter count. Choosing between the two is mostly a practical decision, not a question of which one is correct.

flowchart LR
  hp[previous state] --> RG((reset gate))
  x[input] --> RG
  hp --> UG((update gate))
  x --> UG
  RG --> CA[candidate state]
  x --> CA
  CA --> MIX[blend]
  UG --> MIX
  hp --> MIX
  MIX --> hn[new state]

The gates decide how the blend is made. The blend keeps the old state where the update gate is near 0 and takes the candidate where it is near 1.

The math

Setup

Let xt∈Rd\mathbf{x}_t \in \mathbb{R}^{d} be the input at step tt and ht−1∈Rm\mathbf{h}_{t-1} \in \mathbb{R}^{m} be the hidden state from the previous step. The symbol ⊙\odot means elementwise multiplication of two vectors of the same length. The GRU has three weight blocks. Each block has an input matrix WW, a recurrent matrix UU and a bias vector b\mathbf{b}: one block for the update gate, one for the reset gate, and one for the candidate state. The gates use the sigmoid σ\sigma, which returns values between 0 and 1. The candidate uses the hyperbolic tangent, which returns values between −1-1 and 11.

The equations

The update gate zt\mathbf{z}_t, the reset gate rt\mathbf{r}_t, the candidate h~t\tilde{\mathbf{h}}_t and the new state ht\mathbf{h}_t are

zt=σ(Wzxt+Uzht−1+bz)\mathbf{z}_t = \sigma\left(W_z \mathbf{x}_t + U_z \mathbf{h}_{t-1} + \mathbf{b}_z\right) rt=σ(Wrxt+Urht−1+br)\mathbf{r}_t = \sigma\left(W_r \mathbf{x}_t + U_r \mathbf{h}_{t-1} + \mathbf{b}_r\right) h~t=tanh⁡(Whxt+Uh(rt⊙ht−1)+bh)\tilde{\mathbf{h}}_t = \tanh\left(W_h \mathbf{x}_t + U_h \left(\mathbf{r}_t \odot \mathbf{h}_{t-1}\right) + \mathbf{b}_h\right) ht=(1−zt)⊙ht−1+zt⊙h~t\mathbf{h}_t = \left(1 - \mathbf{z}_t\right) \odot \mathbf{h}_{t-1} + \mathbf{z}_t \odot \tilde{\mathbf{h}}_t

Read the last line as a blend. Each entry of zt\mathbf{z}_t is a number between 0 and 1 that says how much of the candidate to take in that coordinate. The rest comes from the old state. Some papers swap the roles of zt\mathbf{z}_t and 1−zt1 - \mathbf{z}_t. The model is the same; only the label on the gate changes.

What the gates do

  • Update gate. When an entry of zt\mathbf{z}_t is near 0, that coordinate of the state is copied forward. When it is near 1, the coordinate is replaced by the candidate.
  • Reset gate. The candidate reads rt⊙ht−1\mathbf{r}_t \odot \mathbf{h}_{t-1}, not the full old state. When rt\mathbf{r}_t is near 0, the candidate is computed as if the past did not exist, which lets the unit start a fresh thought, such as a new sentence. When rt\mathbf{r}_t is near 1, the candidate sees the full past, as in the vanilla rule.

Parameter count

Each weight block has mdmd entries in WW, m2m^2 in UU and mm in b\mathbf{b}. Three blocks give

3(md+m2+m)3\left(md + m^2 + m\right)

parameters. A vanilla recurrent layer has one block, so the GRU is three times as large. An LSTM has four blocks, so the GRU is about three quarters of its size at the same hidden width.

The gradient path

The reason the GRU trains better shows up in the derivative of the new state with respect to the old one. Differentiating the blend gives a direct term plus other terms that go through the gates and the candidate:

∂ht∂ht−1=diag(1−zt)+(terms through zt and h~t).\frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_{t-1}} = \mathrm{diag}\left(\mathbf{1} - \mathbf{z}_t\right) + (\text{terms through } \mathbf{z}_t \text{ and } \tilde{\mathbf{h}}_t).

The diagonal term is the part of the Jacobian that does not pass through any weight matrix. It is a diagonal matrix whose entries are 1−z1 - z. When the update gate is near 0, these entries are near 1, so the gradient passes through the step almost unchanged. The other terms, which go through the gates and the candidate, are still present, but this direct path lets the signal skip over the nonlinear part of the step. The vanilla recurrence has no such path: its Jacobian is always a product of the weight matrix and an activation derivative.

Worked examples

Example
One scalar step, by hand

Take a single hidden unit with these scalar weights: wz=1w_{z} = 1, uz=0u_{z} = 0, bz=0b_z = 0; wr=0w_r = 0, ur=0u_r = 0, br=0b_r = 0; wh=1w_h = 1, uh=1u_h = 1, bh=0b_h = 0. Start from h0=0h_0 = 0 and feed x1=1x_1 = 1, then x2=0x_2 = 0.

Step 1, with x1=1x_1 = 1 and h0=0h_0 = 0:

  1. Update gate: z1=σ(1)=1/(1+e−1)≈0.7311z_1 = \sigma(1) = 1/(1 + e^{-1}) \approx 0.7311.
  2. Reset gate: r1=σ(0)=0.5r_1 = \sigma(0) = 0.5.
  3. Candidate: h~1=tanh⁡(1⋅1+1⋅(0.5⋅0))=tanh⁡(1)≈0.7616\tilde{h}_1 = \tanh(1 \cdot 1 + 1 \cdot (0.5 \cdot 0)) = \tanh(1) \approx 0.7616.
  4. New state: h1=(1−0.7311)⋅0+0.7311⋅0.7616≈0.5568h_1 = (1 - 0.7311) \cdot 0 + 0.7311 \cdot 0.7616 \approx 0.5568.

Step 2, with x2=0x_2 = 0 and h1≈0.5568h_1 \approx 0.5568:

  1. Update gate: z2=σ(0)=0.5z_2 = \sigma(0) = 0.5. Reset gate: r2=0.5r_2 = 0.5.
  2. Candidate: h~2=tanh⁡(0+1⋅(0.5⋅0.5568))=tanh⁡(0.2784)≈0.2714\tilde{h}_2 = \tanh(0 + 1 \cdot (0.5 \cdot 0.5568)) = \tanh(0.2784) \approx 0.2714.
  3. New state: h2=(1−0.5)⋅0.5568+0.5⋅0.2714=0.2784+0.1357≈0.4141h_2 = (1 - 0.5) \cdot 0.5568 + 0.5 \cdot 0.2714 = 0.2784 + 0.1357 \approx 0.4141.

The input was present only at step 1, yet h2h_2 is still 0.41. Half of the old state was carried over exactly, and half was mixed with a fresh candidate that was itself built from the old state. The value of h2h_2 is the blend, and each gate decided its share.

Example
Why a gate of 0.05 beats a factor of 0.5

Suppose a learning signal must travel backwards through 20 steps. In a vanilla recurrent network, a typical per-step factor is 0.5, so the signal shrinks to

0.520=11 048 576≈9.5×10−7.0.5^{20} = \frac{1}{1\,048\,576} \approx 9.5 \times 10^{-7}.

In a GRU whose update gate sits near 0.05 at each step, the direct path contributes a factor of 1−0.05=0.951 - 0.05 = 0.95 per step:

  1. Start with 0.95200.95^{20}.
  2. Take the natural logarithm: 20ln⁡0.95=20×(−0.05129)≈−1.02620 \ln 0.95 = 20 \times (-0.05129) \approx -1.026.
  3. Exponentiate: e−1.026≈0.358e^{-1.026} \approx 0.358.

After 20 steps, the GRU still passes about 36 percent of the signal, against about one part in a million for the vanilla unit. The factor of 0.95 is not a magic number; it is what a gate near zero produces, and the network learns the gate value from data.

Note: the comparison uses the direct path only. The other terms in the Jacobian also contribute, so this is an illustration of the mechanism, not a full gradient calculation.

Tradeoffs and failure modes

A GRU is a good default for a recurrent model that must learn dependencies across tens of steps and has modest data or compute. It is a poor choice when the whole sequence is available at once and the task needs direct links between distant positions; there, a Transformer with attention usually does better and trains in parallel.

SituationWhat goes wrongBetter choice
Dependencies over a few stepsNothing, a vanilla cell may be enoughA [[recurrent-neural-network
Dependencies over hundreds of steps and very large modelsThe GRU's fixed capacity can run out of room to keep everythingAn [[long-short-term-memory
Sequence available at once, long range neededProcessing is still one step at a timeA Transformer
Gradients explode despite gatingGates do not stop large updatesGradient Norm Clipping
Reset gate stuck near 0 everywhereThe candidate ignores the past and the unit behaves like a feedforward layerCheck initialisation and learning rate; the gate should learn to open
Tiny training setGates add parameters that can overfitA smaller hidden size, dropout, or early stopping

The GRU does not remove the vanishing gradient problem. It reduces it by giving the signal a path that does not pass through a weight matrix at every step. If the update gates are pushed towards 1 for the whole sequence, the direct path disappears and the unit behaves like a vanilla one again.

History

Cho and colleagues introduced the GRU in 2014 in a paper on RNN encoder-decoder models for statistical machine translation. Their aim was a gated unit simpler than the LSTM that could still learn long dependencies in translation. In the same year, Chung and colleagues ran a systematic comparison of the LSTM and the GRU on music and speech modelling tasks. They found that both gated units clearly outperformed the plain tanh recurrent unit, and that neither was consistently better than the other across the tasks they tested.

The result made the GRU an attractive default. It became common in encoder-decoder translation systems of the late 2010s, in speech recognition, and in small sequence models on constrained hardware. Like the LSTM, it was overtaken for most large-scale language tasks by the Transformer, which replaces recurrence with attention and processes all positions in parallel.

Check yourself

QuestionRecall: what does each of the two gates control?Show answer

The update gate controls, coordinate by coordinate, how much of the old hidden state is kept and how much is replaced by the candidate. The reset gate controls how much of the old state the candidate is allowed to read when it is computed.

QuestionComputation: using the scalar unit from the first worked example, with h1≈0.5568h_1 \approx 0.5568 and x2=0x_2 = 0, what is h2h_2 if the update gate is 0.8 instead of 0.5? Keep the reset gate at 0.5 and the candidate as before.Show answer

The candidate is unchanged at tanh⁡(0.2784)≈0.2714\tanh(0.2784) \approx 0.2714. The new state is (1−0.8)⋅0.5568+0.8⋅0.2714=0.1114+0.2171≈0.3285(1 - 0.8) \cdot 0.5568 + 0.8 \cdot 0.2714 = 0.1114 + 0.2171 \approx 0.3285. A larger update gate moves the state further toward the candidate and away from the old value.

QuestionWhy: why does a vanilla recurrent unit lose the learning signal over long sequences, while a GRU with a small update gate keeps more of it?Show answer

In the vanilla unit, every step multiplies the signal by the recurrent weight matrix and an activation derivative, so the signal shrinks geometrically when those factors are typically below 1. In the GRU, the update gate provides a direct term diag(1−zt)\mathrm{diag}(\mathbf{1} - \mathbf{z}_t) in the Jacobian. When the gate is near 0, this term is near the identity, so the signal passes through the step with little loss.

QuestionApplied: you must choose between a GRU and an LSTM for an on-device model that predicts the next keystroke, with a hidden size of 128 and a strict memory budget. Which do you try first, and what would change your mind?Show answer

Try the GRU first. At the same hidden width it has three weight blocks instead of four, which saves about a quarter of the recurrent parameters and speeds up each step. If validation accuracy on long-range patterns, such as dependencies across whole sentences, stays clearly below what the LSTM reaches on a comparable model, then the separate cell state of the LSTM is worth its extra cost, and the budget should be revisited.

Sources