16. Gated Recurrent Units (GRU)
Streamlined gated sequence processing: Reset and update gate mechanisms, hidden state interpolation, computational efficiency, and GRU vs LSTM comparison.
GRU: Complete Notes (Beginner to Advanced)
Introduction#
Gated Recurrent Unit (GRU) is a type of recurrent neural network designed to process sequential data while controlling how information is retained and updated over time.
GRU was introduced as a simpler gated alternative to the Long Short-Term Memory (LSTM) architecture.
A basic RNN uses:
textCurrent Input + Previous Hidden State ↓ RNN ↓ Current Hidden State
A GRU adds gates that control the hidden state:
Architecture & Data FlowCurrent Input + Previous Hidden State ↓ +-------------+ | GRU | +-------------+ | | v v Update Gate Reset Gate | v Candidate Hidden State | v New Hidden State
The main GRU components are:
textUpdate Gate Reset Gate Candidate Hidden State
Unlike LSTM, a standard GRU does not maintain a separate cell state.
1. Gated Recurrent Unit (GRU)
A Gated Recurrent Unit (GRU) is a gated recurrent architecture that maintains a hidden state and uses learned gates to regulate information flow.
At time step t, a GRU receives:
›xₜ hₜ₋₁
and produces:
›hₜ
The standard GRU equations are:
Mathematical Formulationzₜ = σ(W_z xₜ + U_z hₜ₋₁ + b_z) rₜ = σ(W_r xₜ + U_r hₜ₋₁ + b_r) h̃ₜ = tanh(W_h xₜ + U_h (rₜ ⊙ hₜ₋₁) + b_h) hₜ = (1 - zₜ) ⊙ hₜ₋₁ + zₜ ⊙ h̃ₜ
Where:
Mathematical Formulationxₜ = current input hₜ₋₁ = previous hidden state h̃ₜ = candidate hidden state hₜ = new hidden state zₜ = update gate rₜ = reset gate σ = sigmoid tanh = hyperbolic tangent ⊙ = element-wise multiplication
High-Level Flow#
Architecture & Data Flowxₜ | v +--------------+ | | hₜ₋₁| GRU | | | +--------------+ | | v v Update Gate Reset Gate | | | v | Candidate State | | +--------+ | v Hidden State
Main Idea#
The GRU asks two important questions:
textUpdate Gate: How much should the new candidate replace the old hidden state? Reset Gate: How much of the previous hidden state should be considered when creating the candidate?
This allows the GRU to control memory without using a separate cell state.
2. Update Gate
The Update Gate determines how much of the previous hidden state should be retained versus how much should be replaced by the candidate hidden state.
It is commonly represented as:
›zₜ
The equation is:
Mathematical Formulationzₜ = σ(W_z xₜ + U_z hₜ₋₁ + b_z)
Because sigmoid produces values between:
›0 and 1
each component of the update gate acts like a soft control value.
Interpretation#
Using the common GRU formulation:
Mathematical Formulationzₜ ≈ 0 → retain more of the previous hidden state zₜ ≈ 1 → use more of the candidate hidden state
The final hidden-state update is:
Mathematical Formulationhₜ = (1 - zₜ) ⊙ hₜ₋₁ + zₜ ⊙ h̃ₜ
Therefore:
Mathematical FormulationNew Hidden State = Old Hidden State × (1 - Update Gate) + Candidate × Update Gate
Example#
Suppose:
Mathematical Formulationhₜ₋₁ = 0.8 h̃ₜ = 0.2 zₜ = 0.25
Then:
Mathematical Formulationhₜ = (1 - 0.25)(0.8) + 0.25(0.2)
Mathematical Formulationhₜ = 0.75(0.8) + 0.25(0.2)
Mathematical Formulationhₜ = 0.60 + 0.05
Mathematical Formulationhₜ = 0.65
The new hidden state is closer to the old hidden state because the update gate is relatively small.
Mental Model#
Architecture & Data FlowPrevious Hidden State | +--------------------+ | | | (1 - zₜ) | | v v Retained Information Candidate Hidden State | | zₜ v New Information \ / \ / +-------------+ | v hₜ
3. Reset Gate
The Reset Gate determines how much of the previous hidden state should be used when creating the candidate hidden state.
It is represented as:
›rₜ
The equation is:
Mathematical Formulationrₜ = σ(W_r xₜ + U_r hₜ₋₁ + b_r)
Again:
›0 ≤ rₜ ≤ 1
Interpretation#
Mathematical Formulationrₜ ≈ 0 → largely ignore the previous hidden state when forming the candidate rₜ ≈ 1 → use more of the previous hidden state
The reset gate appears in the candidate calculation:
Mathematical Formulationh̃ₜ = tanh(W_h xₜ + U_h (rₜ ⊙ hₜ₋₁) + b_h)
The key operation is:
›rₜ ⊙ hₜ₋₁
This controls how much previous information reaches the candidate computation.
Example#
Suppose:
Mathematical Formulationhₜ₋₁ = 0.8 rₜ = 0.2
Then:
Mathematical Formulationrₜ × hₜ₋₁ = 0.2 × 0.8 = 0.16
Only a small portion of the previous hidden information is passed into the candidate calculation.
Mental Model#
Architecture & Data FlowPrevious Hidden State | v [Reset Gate] | v How much previous information to use? | v Candidate Calculation
4. Candidate Hidden State
The Candidate Hidden State is the proposed new hidden representation.
It is commonly represented as:
›h̃ₜ
and calculated as:
Mathematical Formulationh̃ₜ = tanh(W_h xₜ + U_h (rₜ ⊙ hₜ₋₁) + b_h)
The candidate uses:
textCurrent Input + Reset-controlled Previous Hidden State
Why the Reset Gate Matters Here#
The reset gate controls the previous hidden state before it enters the candidate computation.
Architecture & Data Flowhₜ₋₁ | v rₜ ⊙ hₜ₋₁ | v Candidate computation | v h̃ₜ
Therefore:
textReset Gate → controls previous information used to create candidate Candidate Hidden State → represents the proposed new information
Candidate Range#
Because tanh is used:
›-1 ≤ h̃ₜ ≤ 1
for each component.
5. Updating the Hidden State
Once the update gate and candidate hidden state are available, the GRU computes the new hidden state.
The standard formulation used here is:
Mathematical Formulationhₜ = (1 - zₜ) ⊙ hₜ₋₁ + zₜ ⊙ h̃ₜ
This can be understood as a weighted interpolation between:
›Previous Hidden State
and:
›Candidate Hidden State
If Update Gate Is Near Zero#
Mathematical Formulationzₜ ≈ 0
Then:
Mathematical Formulationhₜ ≈ hₜ₋₁
So the old hidden state is mostly preserved.
If Update Gate Is Near One#
Mathematical Formulationzₜ ≈ 1
Then:
Mathematical Formulationhₜ ≈ h̃ₜ
So the candidate largely replaces the old hidden state.
Important Note#
Different texts and implementations may use an alternative update-gate convention:
Mathematical Formulationhₜ = zₜ ⊙ hₜ₋₁ + (1 - zₜ) ⊙ h̃ₜ
In that convention, the interpretation of zₜ is reversed.
Therefore, when studying a specific implementation, always check its exact equations.
The equations in these notes use:
Mathematical Formulationhₜ = (1 - zₜ) ⊙ hₜ₋₁ + zₜ ⊙ h̃ₜ
6. Complete GRU Flow
At every time step, the GRU performs the following operations.
Step 1: Calculate Update Gate#
Mathematical Formulationzₜ = σ(W_z xₜ + U_z hₜ₋₁ + b_z)
Question:
›How much should the candidate replace the old hidden state?
Step 2: Calculate Reset Gate#
Mathematical Formulationrₜ = σ(W_r xₜ + U_r hₜ₋₁ + b_r)
Question:
›How much previous information should influence the candidate?
Step 3: Calculate Candidate Hidden State#
Mathematical Formulationh̃ₜ = tanh(W_h xₜ + U_h (rₜ ⊙ hₜ₋₁) + b_h)
Question:
›What new hidden representation could be created?
Step 4: Calculate New Hidden State#
Mathematical Formulationhₜ = (1 - zₜ) ⊙ hₜ₋₁ + zₜ ⊙ h̃ₜ
Question:
›How much old state and candidate state should be combined?
Complete Diagram#
Architecture & Data Flowxₜ | | +-------+-------+ | | v v Update Gate Reset Gate zₜ rₜ | | | v | rₜ ⊙ hₜ₋₁ | | | v | Candidate State | h̃ₜ | | | | +-------+-------+ | v Combine old + candidate | v hₜ
7. Numerical Example
Consider a simplified scalar GRU.
Suppose:
Mathematical Formulationhₜ₋₁ = 0.8
and the gates and candidate have already been calculated:
Mathematical Formulationzₜ = 0.3 rₜ = 0.2 h̃ₜ = 0.5
The reset gate has already been used to create the candidate.
Now calculate the new hidden state:
Mathematical Formulationhₜ = (1 - zₜ)hₜ₋₁ + zₜh̃ₜ
Substitute:
Mathematical Formulationhₜ = (1 - 0.3)(0.8) + 0.3(0.5)
Mathematical Formulationhₜ = 0.7(0.8) + 0.3(0.5)
Mathematical Formulationhₜ = 0.56 + 0.15
Mathematical Formulationhₜ = 0.71
Therefore:
Mathematical FormulationNew Hidden State = 0.71
The previous state contributes strongly because:
Mathematical Formulationzₜ = 0.3
is relatively small.
8. GRU vs Basic RNN
A basic RNN performs a direct recurrent update:
Mathematical Formulationhₜ = tanh(Wₓh xₜ + Wₕh hₜ₋₁ + b)
A GRU introduces gates:
textUpdate Gate Reset Gate Candidate Hidden State
Comparison:
| Feature | Basic RNN | GRU |
|---|---|---|
| Hidden State | Yes | Yes |
| Separate Cell State | No | No |
| Update Gate | No | Yes |
| Reset Gate | No | Yes |
| Gated Memory Control | No | Yes |
| Architecture | Simpler | More complex |
| Long-range dependency handling | More difficult in many cases | Often better |
The GRU provides explicit control over how previous and new information are combined.
9. LSTM vs GRU
LSTM and GRU are both gated recurrent architectures.
The major architectural difference is:
textLSTM → Hidden State + Cell State → Three main gates GRU → Hidden State only → Two main gates
LSTM#
An LSTM maintains:
›cₜ → Cell State hₜ → Hidden State
and commonly uses:
textForget Gate Input Gate Output Gate Candidate State
GRU#
A GRU maintains:
›hₜ → Hidden State
and uses:
textUpdate Gate Reset Gate Candidate Hidden State
Direct Comparison#
| Feature | LSTM | GRU |
|---|---|---|
| Hidden State | Yes | Yes |
| Cell State | Yes | No |
| Forget Gate | Yes | No |
| Input Gate | Yes | No |
| Output Gate | Yes | No |
| Update Gate | No | Yes |
| Reset Gate | No | Yes |
| Candidate State | Yes | Yes |
| Number of main gates | 3 | 2 |
| Separate memory state | Yes | No |
| Typical parameter count | Higher | Lower |
| Architecture | More complex | Simpler |
Parameter Count#
For input size:
›D
and hidden size:
›H
a standard LSTM has four parameterized transformations:
›4 × [H(H + D) + H]
A standard GRU has three:
›3 × [H(H + D) + H]
Therefore, approximately:
Mathematical FormulationGRU parameters ≈ 3/4 of LSTM parameters
when comparing otherwise equivalent single-layer, single-direction configurations.
The exact parameter count in a framework can differ depending on implementation details and optional biases.
10. LSTM and GRU Memory Mechanisms
The two architectures manage memory differently.
LSTM#
LSTM has an explicit cell state:
Architecture & Data Flowcₜ₋₁ | v Forget + Add Information | v cₜ | v Output Gate | v hₜ
GRU#
GRU directly updates the hidden state:
Architecture & Data Flowhₜ₋₁ | +----> Reset-controlled candidate | +----> Update-controlled retention | v hₜ
Therefore:
textLSTM: separate memory pathway + hidden state GRU: single hidden-state pathway
11. LSTM vs GRU: Conceptual Mapping
Some concepts can be loosely related, but they are not exact one-to-one equivalents.
textLSTM: Forget Gate Input Gate Candidate State Output Gate Cell State Hidden State
versus:
textGRU: Update Gate Reset Gate Candidate Hidden State Hidden State
A useful high-level comparison is:
textLSTM ├── Decide what old memory to retain ├── Decide what new information to write ├── Maintain separate cell memory └── Decide what to expose GRU ├── Decide how much old/new state to combine ├── Decide how much previous state affects candidate └── Maintain one hidden state
Do not treat:
Mathematical FormulationLSTM Forget Gate = GRU Reset Gate
as an exact equivalence.
Their roles are related conceptually to information control, but their equations and positions in the computation are different.
12. When GRU Can Be Useful
GRUs can be attractive when:
textA recurrent architecture is appropriate + A simpler architecture is preferred + Lower parameter count is useful
Potential advantages include:
- Fewer parameters than a comparable standard LSTM
- Simpler internal state structure
- Potentially faster training or inference depending on implementation and workload
- Useful performance on many sequence tasks
There is no universal rule that GRU is always better or faster than LSTM.
The appropriate choice depends on:
textDataset Sequence length Task Model size Hardware Optimization Training behavior
13. Simple GRU Implementation with PyTorch
PyTorch provides an nn.GRU module.
🐍 PythonInteractive WebAssemblyimport torch
import torch.nn as nn
gru = nn.GRU(
input_size=10,
hidden_size=20,
batch_first=True
)
x = torch.randn(4, 5, 10)
output, hidden = gru(x)
print("Output shape:", output.shape)
print("Hidden shape:", hidden.shape)
For:
Mathematical Formulationbatch_size = 4 sequence_length = 5 input_size = 10 hidden_size = 20
the shapes are:
textInput: (4, 5, 10) Output: (4, 5, 20) Hidden: (1, 4, 20)
The final hidden state contains the last hidden representation for the sequence.
14. Stacked GRU
GRU layers can be stacked using num_layers.
🐍 PythonInteractive WebAssemblygru = nn.GRU(
input_size=10,
hidden_size=20,
num_layers=3,
batch_first=True
)
x = torch.randn(4, 5, 10)
output, hidden = gru(x)
print("Output shape:", output.shape)
print("Hidden shape:", hidden.shape)
For:
›3 layers 1 direction
the shapes are:
textOutput: (4, 5, 20) Hidden: (3, 4, 20)
The first hidden-state dimension represents:
›number of layers × number of directions
15. Bidirectional GRU
A GRU can also process sequences in both directions.
🐍 PythonInteractive WebAssemblygru = nn.GRU(
input_size=10,
hidden_size=20,
batch_first=True,
bidirectional=True
)
x = torch.randn(4, 5, 10)
output, hidden = gru(x)
print("Output shape:", output.shape)
print("Hidden shape:", hidden.shape)
For one bidirectional layer:
Mathematical FormulationNumber of directions = 2
Therefore:
textOutput: (4, 5, 40) Hidden: (2, 4, 20)
The output feature dimension is:
›2 × hidden_size
16. Complete GRU Mental Model
The entire GRU can be remembered using three questions:
text1. UPDATE GATE How much should I replace the old hidden state? 2. RESET GATE How much of the old hidden state should influence the candidate? 3. CANDIDATE HIDDEN STATE What new representation should I propose?
Then:
Architecture & Data FlowPrevious Hidden State | +-----------------------------+ | | | Reset Gate | | | v | Candidate State | | | | +----> Update Gate -----------+ | v New Hidden State
17. Summary
| Concept | Meaning |
|---|---|
| GRU | Gated recurrent architecture for sequential data |
| Update Gate | Controls the balance between previous and candidate hidden state |
| Reset Gate | Controls how much previous hidden information contributes to the candidate |
| Candidate Hidden State | Proposed new hidden representation |
| Hidden State | GRU's main recurrent memory/output representation |
| LSTM | Gated recurrent architecture with separate cell and hidden states |
| GRU vs LSTM | GRU uses fewer gates and no separate cell state |
18. Quick Recap
textGRU → A simpler gated alternative to LSTM. Update Gate → Controls how much candidate information replaces old hidden information. Reset Gate → Controls how much previous hidden information is used to form the candidate. Candidate Hidden State → Proposed new hidden representation. Hidden State → The GRU's recurrent state carried to the next time step. LSTM vs GRU → LSTM has a separate cell state and three main gates. → GRU has one hidden state and two main gates. → GRU generally has fewer parameters.
The core GRU equations are:
Mathematical Formulationzₜ = σ(W_z xₜ + U_z hₜ₋₁ + b_z) rₜ = σ(W_r xₜ + U_r hₜ₋₁ + b_r) h̃ₜ = tanh(W_h xₜ + U_h (rₜ ⊙ hₜ₋₁) + b_h) hₜ = (1 - zₜ) ⊙ hₜ₋₁ + zₜ ⊙ h̃ₜ
Final Mental Model
Architecture & Data FlowGRU | +-----------+-----------+ | | v v UPDATE GATE RESET GATE | | | v | Previous Hidden State | | | v | Candidate Hidden State | | +-----------+-----------+ | v New Hidden State hₜ | v Next Time Step
The simplest way to remember the difference is:
textLSTM: "Maintain a separate memory cell and control forgetting, writing, and exposing." GRU: "Use gates to control how the previous hidden state and a new candidate are combined."
16. GRU Checkpoint
Finished studying this notebook?
Mark this guide as completed to update your course progress roadmap.