3.5.5Sequence Models

Gated Recurrent Units (GRU)

2,617 words12 min readdifficulty · medium5 backlinks

What Problem Does GRU Solve?

Standard RNN issue: Gradient vanishes exponentially over many timesteps → can't learn long-range dependencies.

LSTM solution: Separate cell state ctc_t with gates to control information flow.

GRU insight: Do we really need a separate cell state? What if we combine the hidden state and cell state into one hth_t, and merge the forget and input gates into a single update gate? This cuts parameters by ~25% while keeping most of LSTM's power.


The GRU Architecture

Figure — Gated Recurrent Units (GRU)

GRU has 2 gates operating on hidden state hth_t:


Derivation from First Principles

Goal: Update hidden state hth_t from ht1h_{t-1} and xtx_t, controlling information flow.

Step 1: Compute Reset Gate

rt=σ(Wr[ht1,xt]+br)r_t = \sigma(W_r \cdot [h_{t-1}, x_t] + b_r)

Why this step? We need to decide: is the past ht1h_{t-1} relevant for the new candidate we're about to compute? If the sequence context changes (e.g., topic shift), we want rt0r_t \to 0 to "reset" and not be biased by irrelevant history.

Step 2: Compute Candidate Hidden State

h~t=tanh(Wh[rtht1,xt]+bh)\tilde{h}_t = \tanh(W_h \cdot [r_t \odot h_{t-1}, x_t] + b_h)

Why this step?

  • rtht1r_t \odot h_{t-1} element-wise multiplies the old state by the reset gate. If rtr_t is small, we ignore ht1h_{t-1}.
  • Concatenate with xtx_t, pass through tanh to get a candidate update h~t\tilde{h}_t in [1,1][-1, 1].
  • This is the "proposed new memory" if were to fully replace ht1h_{t-1}.

Step 3: Compute Update Gate

zt=σ(Wz[ht1,xt]+bz)z_t = \sigma(W_z \cdot [h_{t-1}, x_t] + b_z)

Why this step? We need to decide: how much of the old ht1h_{t-1} to keep vs. how much of the new candidate h~t\tilde{h}_t to accept. This is the interpolation weight.

Step 4: Final Hidden State (Linear Interpolation)

ht=(1zt)h~t+ztht1h_t = (1 - z_t) \odot \tilde{h}_t + z_t \odot h_{t-1}

Why this step?

  • If zt1z_t \approx 1: htht1h_t \approx h_{t-1}preserve old memory (like LSTM's high forget gate).
  • If zt0z_t \approx 0: hth~th_t \approx \tilde{h}_taccept new candidate (like LSTM's high input gate).
  • This is a convex combination: ztz_t acts as a learned interpolation weight per dimension.

Key insight: The update gate ztz_t merges LSTM's forget and input gates. In LSTM, you have separate ftf_t (forget) and iti_t (input). In GRU, when ztz_t is high (keep old), it's low for new (reject new), and vice versa—they're coupled as (1zt)(1-z_t) and ztz_t.


GRU vs LSTM: Formula Comparison

LSTM GRU
3 gates: forget ftf_t, input iti_t, output oto_t 2 gates: update ztz_t, reset rtr_t
Separate cell state ctc_t and hidden state hth_t Single hidden state hth_t (no separate cell)
ct=ftct1+itc~tc_t = f_t \odot c_{t-1} + i_t \odot \tilde{c}_t ht=(1zt)h~t+ztht1h_t = (1-z_t) \odot \tilde{h}_t + z_t \odot h_{t-1}
ht=ottanh(ct)h_t = o_t \odot \tanh(c_t) (no output gate, hth_t is directly exposed)

Parameter count: GRU has ~25% fewer parameters than LSTM (2 gates vs 3, no separate cell state weights).



Example 1: Sentiment Analysis Sentence

Sequence: "The movie started well but the ending was terrible."

Let's trace one GRU cell at word "but" (timestep tt):

  • Input: xtx_t = embedding of "but", ht1h_{t-1} = hidden state after "well" (likely positive sentiment accumulated).

Step 1: Compute rtr_t: rt=σ(Wr[ht1,xt]+br)r_t = \sigma(W_r [h_{t-1}, x_t] + b_r) "But" signals a contrast. If the network has learned that "but" often reverses sentiment, rtr_t might be low (say 0.2), meaning "don't rely heavily on the positive ht1h_{t-1}—we're about to change direction."

Why this step? The reset gate detects the contrast word and decides to partially ignore the positive history.

Step 2: Compute h~t\tilde{h}_t: h~t=tanh(Wh[rtht1,xt]+bh)\tilde{h}_t = \tanh(W_h [r_t \odot h_{t-1}, x_t] + b_h) With rt=0.2r_t = 0.2, we multiply ht1h_{t-1} by 0.2, heavily damping the old positive sentiment. The candidate h~t\tilde{h}_t is now mostly driven by xtx_t ("but") and will start shifting toward neutral or preparing for negativity.

Why this step? We compute a fresh candidate that's not overly biased by the past positive context.

Step 3: Compute ztz_t: zt=σ(Wz[ht1,xt]+bz)z_t = \sigma(W_z [h_{t-1}, x_t] + b_z) "But" is a pivot word. The network might learn ztz_t should be low (say 0.3), meaning "don't keep much of the old positive ht1h_{t-1}—accept more of the new candidate."

Why this step? The update gate decides to shift away from the old sentiment toward the new candidate.

Step 4: Final hth_t: ht=(10.3)h~t+0.3ht1=0.7h~t+0.3ht1h_t = (1 - 0.3) \odot \tilde{h}_t + 0.3 \odot h_{t-1} = 0.7 \tilde{h}_t + 0.3 h_{t-1} The new hidden state is 70% the candidate (which has low influence from past due to reset gate) and 30% the old state. Net effect: sentiment representation shifts from positive toward neutral/negative.

Result: As the sequence continues ("ending was terrible"), the reset gate will again be low, update gate will favor new negative information, and final hTh_T will represent negative sentiment.


Example 2: Long-Term Dependency (Name Recall)

Sequence: "Alice went to the park. She played with her dog. Later, she went home. __ was tired."

We want to fill the blank with "Alice" (or "She"), requiring memory of "Alice" from the start.

At the blank (timestep tt):

  • Many timesteps have passed since "Alice" was mentioned.
  • Intermediate words ("park", "dog", "home") are less relevant to who is tired.

GRU behavior:

For intermediate words (park, dog, home):

  • ztz_t is high (say 0.8–0.9): The update gate learns these words don't change the subject identity, so htht1h_t \approx h_{t-1}. The identity "Alice" is preserved through the (1zt)(1-z_t) term being small—we mostly keep old ht1h_{t-1}.

Why this works: High ztz_t acts like LSTM's high forget gate + low input gate: "keep old memory, ignore new irrelevant content." Gradient flows backward through the (1zt)(1-z_t) and ztz_t terms, which are gated—gradient highway.

At the blank:

  • ht1h_{t-1} still encodes "Alice" because intermediate ztz_t were high.
  • The decoder can extract "Alice" from hth_t to generate "She" or "Alice was tired."

Key: GRU's update gate ztz_t controls the gradient flow. When zt1z_t \approx 1, the gradient htht11\frac{\partial h_t}{\partial h_{t-1}} \approx 1 (chain rule), avoiding vanishing gradient.



Common Mistakes


When to Use GRU vs LSTM?

Use GRU Use LSTM
Default choice for new projects (simpler) Very long sequences (100+ steps)
Small datasets (fewer params → less overfit) Complex hierarchical dependencies
Faster training/inference needed When you have compute budget and want max expressiveness
Performance is close to LSTM empirically Speech, complex NLP (slight edge in some benchmarks)

Rule of thumb: Start with GRU. If you hit a performance ceiling and have resources, try LSTM. The difference is often marginal (1-2% accuracy) in practice.



Recall Explain to a 12-Year-Old

Imagine you're writing a story in a notebook, one sentence at a time. You have two sticky notes to help you decide what to write next: Sticky Note 1 (Reset Gate): "Should I look back at what I wrote before to get ideas, or should I start fresh?" If the story changes topics (like going from pirates to princesses), you might ignore the old sentences. Low reset = "forget the old stuff for this new idea."

Sticky Note 2 (Update Gate): "Should I keep my current page as-is, or should I erase some and write the new sentence?" If the new sentence is really important, you erase more of the old page (low update gate). If the old stuff is still important, you keep most of it (high update gate).

The GRU does this automatically for every word in a sentence, deciding "how much old memory to keep" and "how much past context to use for new ideas." It's like a smart notebook that knows when to remember and when to move on—perfect for understanding long stories or conversations!


Connections


#flashcards/ai-ml

What are the two gates in a GRU and what does each control? :: Reset gate rtr_t controls how much of past hidden state influences the new candidate. Update gate ztz_t controls interpolation between old hidden state and new candidate.

Write the GRU final hidden state update equation :: ht=(1zt)h~t+ztht1h_t = (1 - z_t) \odot \tilde{h}_t + z_t \odot h_{t-1}, where ztz_t is update gate, h~t\tilde{h}_t is candidate hidden state.

How does GRU's update gate ztz_t relate to LSTM's forget and input gates?
GRU merges them: high ztz_t means keep old (like LSTM forget=1, input=0), low ztz_t means accept new (like LSTM forget=0, input=1). They're coupled as ztz_t and (1zt)(1-z_t).
What is the role of the reset gate rtr_t in the candidate computation?
It gates the previous hidden state: h~t=tanh(Wh[rtht1,xt]+bh)\tilde{h}_t = \tanh(W_h [r_t \odot h_{t-1}, x_t] + b_h). Low rtr_t means ignore past context when forming the new candidate.
Why does GRU avoid vanishing gradients?
The update gate creates a gradient highway: when zt1z_t \approx 1, htht11\frac{\partial h_t}{\partial h_{t-1}} \approx 1, so gradients don't decay exponentially over long sequences.
GRU vs LSTM: which has fewer parameters and by roughly how much?
GRU has ~25% fewer parameters (2 gates vs 3, no separate cell state).
If you see the word "but" in sentiment analysis, should the GRU's reset gate be high or low, and why?
Low. "But" signals a contrast/topic shift, so the reset gate should reduce influence of past (positive) sentiment when computing the new candidate.
In a long sequence where intermediate words are irrelevant, should the update gate ztz_t be high or low?
High. High ztz_t means htht1h_t \approx h_{t-1}, preserving long-term memory and ignoring irrelevant new information.

Concept Map

suffers from

solves VG but

simplifies

merges gates into

adds

scales past state for

feeds

feeds

mixed via

weights

kept when z near 1

produces

Standard RNN

Vanishing Gradients

LSTM

3 gates 2 states

GRU

Update Gate z_t

Reset Gate r_t

Candidate h_tilde

Input x_t

Previous h_t-1

Linear Interpolation

New Hidden State h_t

Hinglish (regional understanding)

Intuition Hinglish mein samjho

GRU ek simplified version hai LSTM ka. Socho RNN ko long-term memory problem hai—bahut zyada timesteps ke bad gradient vanish ho jata hai, toh network bhool jaata hai purane words. LSTM neisko solve kiya3 gates se aur alag cell state se, lekin thoda complex ho gaya. GRU bolta hai: kya zarurat hai 3 gates ki? Chaliye 2 gates se kaam chalate hain—ek update gate jo decide karta hai kitna purana hidden state rakhna hai aur kitna naya candidate lena hai, aur ek reset gate jo decide karta hai ki naya candidate banate waqt kitna purana context use karna hai.

Result kya hai? GRU me 25% kam parameters hain LSTM se

Go deeper — visual, from zero

Test yourself — Sequence Models

Connections