Part XI — Sequence Models: RNNs, LSTMs, Attention & The Transformer Block · Chapter 4

The Gated Recurrent Unit (GRU)

Hook

The LSTM — short for Long Short-Term Memory — fixed vanishing gradients with three gates and a separate cell state running alongside the hidden state. What if you could get the same fix with one gate fewer, and no second state to keep track of at all?

Intuition
update gate z ≈ 0.0180 every step — reset gate r ≈ 0.9820, h after 10 steps = 0.1284

The same fixed input every step, an update gate biased almost shut. Watch the hidden state creep upward, a little more each step, never resetting and never exploding — the signature of a gate that's mostly saying "keep what you already have."

Formalize

A GRU — short for Gated Recurrent Unit — cell has two gates instead of the LSTM's three, and only one state instead of two:

zt=σ(Wzxt+Uzht1+bz),rt=σ(Wrxt+Urht1+br)z_t = \sigma(W_z x_t + U_z h_{t-1} + b_z), \qquad r_t = \sigma(W_r x_t + U_r h_{t-1} + b_r)
  • ztz_t — the update gate: how much of the new candidate state to blend in versus keeping the old hidden state.
  • rtr_t — the reset gate: how much of the previous hidden state the candidate is allowed to see.
  • xtx_t — the input at time step tt.
  • ht1h_{t-1} — the hidden state carried over from the previous time step.
h~t=tanh(Wxt+U(rtht1)+b),ht=(1zt)ht1+zth~t\tilde{h}_t = \tanh\big(W x_t + U(r_t \odot h_{t-1}) + b\big), \qquad h_t = (1-z_t)\,h_{t-1} + z_t\,\tilde{h}_t
  • h~t\tilde{h}_t — the candidate hidden state: a fresh proposal for the new state, computed from the current input and the (possibly reset) previous state.
  • hth_t — the new hidden state: a blend of the old state and the candidate, weighted by the update gate.
  1. The update gate merges two LSTM jobs into one

    ztz_t plays the LSTM's forget gate and input gate at once: it's a single dial between "keep ht1h_{t-1} exactly" (zt0z_t{\approx}0) and "replace it with the new candidate" (zt1z_t{\approx}1).

  2. The reset gate controls what the candidate sees

    rtr_t controls how much of the old state the candidate itself is allowed to see before being blended in.

Play

The same 10-step gradient comparison from the LSTM chapter, with the GRU added: a plain RNN's gradient has collapsed to essentially zero, the LSTM's typical forget gate of 0.90.9 holds onto a modest fraction, and this GRU's update gate — biased hard toward "remember" — preserves more gradient than either, using one fewer gate to do it.

Worked example

Fixed input xt=1x_t=1 every step, weights biasing zz toward 00 and rr toward 11:

  1. Both gates settle to the same value every step

    With Wz=Uz=0W_z{=}U_z{=}0, bz=4b_z{=}-4, br=4b_r{=}4:

    • zt=σ(4)0.0180z_t = \sigma(-4) \approx 0.0180 at every timestep, regardless of the hidden state.
    • rt0.9820r_t \approx 0.9820 — the reset gate stays wide open, barely touching the candidate.
  2. The hidden state creeps forward

    With zt0.018z_t\approx0.018 and rt0.982r_t\approx0.982 fixed every step, the recurrence becomes ht0.982ht1+0.018tanh(1+0.491ht1)h_t \approx 0.982\,h_{t-1} + 0.018\tanh(1+0.491\,h_{t-1}) (since W=1W{=}1, U=0.5U{=}0.5, and xt=1x_t{=}1). Starting from h0=0h_0=0:

    • h1=0.982(0)+0.018tanh(1+0)0.0137h_1 = 0.982(0)+0.018\tanh(1+0) \approx 0.0137
    • h2=0.982(0.0137)+0.018tanh(1+0.491×0.0137)0.0272h_2 = 0.982(0.0137)+0.018\tanh(1+0.491\times0.0137) \approx 0.0272
    • h30.982(0.0272)+0.018tanh(1+0.491×0.0272)0.0406h_3 \approx 0.982(0.0272)+0.018\tanh(1+0.491\times0.0272) \approx 0.0406

    Each step keeps about 98.2%98.2\% of the old state and blends in only a shrinking sliver of the (slowly rising) candidate. Continuing the same recurrence for 7 more steps reaches h100.128h_{10} \approx 0.128 — small, steadily increasing, never overshooting.

  3. The same gate that shapes memory shapes the gradient

    The dominant path for h10/h1\partial h_{10}/\partial h_1 is a product of (1zt)(1-z_t) at each step: (0.982)90.849(0.982)^9 \approx 0.849. The LSTM's typical forget gate of 0.90.9 gives (0.9)90.387(0.9)^9 \approx 0.387 over the same span — this GRU's bias toward remembering preserves gradient even more aggressively, with no separate cell state required.

Checkpoint

Find a step count, among the three candidates, where the GRU's surviving gradient is still bigger than the LSTM's.

Pick a step count to try it
Summary
ht=(1zt)ht1+zth~t(one gate, doing the forget gate’s and the input gate’s job at once)h_t = (1-z_t)\,h_{t-1} + z_t\,\tilde h_t \qquad\text{(one gate, doing the forget gate's and the input gate's job at once)}

The GRU isn't a different fix from the LSTM's — it's a cheaper one. Fold the forget and input decisions into a single update gate, drop the separate cell state, and the same (1gate)(1-\text{gate}) memory highway falls out for free. Fewer parameters, fewer states to track, and — as this chapter's numbers show — no real cost in how well gradient survives across time. The next chapter builds on this same gated hidden state to solve a problem no single-sequence chapter has faced yet: turning one sequence into another of a different length entirely.