Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Two valves instead of three
The GRU simplifies the LSTM. There is no separate memory: the hidden state is the memory. One valve, the update gate, slides between “keep what I have” and “replace it with something new”. A second valve, the reset gate, decides how much of the old state to consult when proposing the new value. Fewer parts means fewer parameters and faster steps, with similar accuracy on most tasks. The demo compares how long a stored value survives.
On small devices, a GRU’s 25% saving in parameters and compute matters, for example for on-board anomaly detection or keyword spotting.
- 1. A GRU has how many gates?
- 2. The GRU's memory is…
- 3. The update gate chooses between…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Equations (PyTorch and cuDNN convention):
- r = σ(xWₓᵣ + bₓᵣ + hWₕᵣ + bₕᵣ);
- z = σ(xWₓ_z + bₓ_z + hWₕ_z + bₕ_z);
- n = tanh(xWₓₙ + bₓₙ + r ⊙ (hWₕₙ + bₕₙ));
- h′ = (1 − z) ⊙ n + z ⊙ h.
- Example: h = 1, n = −1, z = 0.75 gives h′ = 0.25·(−1) + 0.75·1 = 0.5.
- Reset gate placement: Cho et al.’s original applies r before the matrix, Wₕₙ(r ⊙ h). PyTorch applies it after, r ⊙ (Wₕₙh), which is faster because hWₕ can be computed for all gates at once. The results are similar.
- Parameters: 3(n(d + n)) plus 6n biases in PyTorch, or 3n with one bias. For d = 10 and n = 32 that is 4224 against the LSTM’s 5632, exactly 3/4. An equal-parameter GRU for that LSTM has 37 units.
- Memory: along the direct path ∂h′/∂h = diag(z) plus gate terms. Update-gate bias b starts z at σ(b): bias 0 halves the state each step, bias 3 keeps 0.233 after 30 steps.
- 1. h′ for h = 1, n = −1, z = 0.75:
- 2. GRU parameters relative to LSTM:
- 3. z near 1 means…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.GRUs in code
Exercise 1.
- With z = 1 a GRU step copied the state.
- With z = 0 it replaced the state by the candidate tanh(0.5).
- The parameter count was 4224.
Practice.
nn.GRU(input_size, hidden_size, batch_first=True)uses the “reset after” convention. Keras’sGRU(reset_after=True)matches it (the default in TF 2).- To start in keep mode, add a positive bias to the update gate. In PyTorch’s bias layout [r, z, n], that is the slice [n:2n].
- GRUs are a good first choice when compute is tight. LSTMs remain competitive and are often slightly better for language modelling (Jozefowicz et al., 2015).
- Weiss, Goldberg and Yahav (2018) showed that with finite precision, LSTMs can implement counters (aⁿbⁿ) while GRUs cannot, because the GRU’s state is bounded and interpolated.
gru = nn.GRU(input_size=1, hidden_size=8, batch_first=True)
for name, p in gru.named_parameters():
if name.startswith("bias_ih"):
p.data[8:16].fill_(3.0) # update gate z: start in 'keep' mode
out, h = gru(x)- 1. PyTorch's GRU bias layout:
- 2. Keras option matching PyTorch's convention:
- 3. Which can count aⁿbⁿ with finite precision?
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Relation to the LSTM. A GRU is like an LSTM with:
- the cell and hidden state merged;
- the input and forget gates tied (i = 1 − z, f = z), so the state is a convex combination of old and new;
- no output gate.
The tie means it cannot both keep everything and add something new. The state stays in [−1, 1], unlike an LSTM cell, which can grow.
Gradient path. ∂h′/∂h contains diag(z) directly, like the LSTM’s forget gate. With z near 1, gradients pass through many steps. As with the LSTM, this only helps if z starts there. A zero bias gives z ≈ 0.5, which halves the gradient each step.
Evidence.
- Chung et al. (2014) found GRUs and LSTMs comparable on music and speech modelling, both well ahead of tanh RNNs.
- Jozefowicz et al. (2015) searched over 10,000 architectures: none beat both consistently. GRUs beat LSTMs on most tasks except language modelling, and the forget bias closed most of the gap.
- 1. A GRU ties…
- 2. The GRU state is bounded because it is…
- 3. Chung et al. found GRU vs LSTM…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Does the GRU need a keep bias too?
History.
- K. Cho, B. van Merriënboer, Ç. Gülçehre, D. Bahdanau, F. Bougares, H. Schwenk and Y. Bengio introduced the GRU in their RNN encoder–decoder for translation (EMNLP 2014).
- J. Chung, Ç. Gülçehre, K. Cho and Y. Bengio compared gated units (2014).
- R. Jozefowicz, W. Zaremba and I. Sutskever ran a large architecture search (ICML 2015).
- G. Weiss, Y. Goldberg and E. Yahav analysed counting ability (ACL 2018).
Exercise 2. The same 30-step memory task and settings as E11-3’s LSTM:
- remember a ±1 bit through 30 noise steps;
- 8 units, 300 Adam steps, batch 64, 3 seeds (the LSTM was rerun at 300 steps for this comparison);
- update-gate bias 0 or 3.
| model (8 units) | parameters | bias 0 | bias 3 |
|---|---|---|---|
| GRU | 264 | 0.461, 0.517, 0.494 | 1.000, 0.996, 1.000 |
| LSTM (E11-3’s model) | 352 | 0.524, 0.526, 0.475 | 1.000, 1.000, 1.000 |
- The keep bias is just as necessary for a GRU. With bias 0 neither model learns the 30-step memory.
- With it, both solve the task, the GRU with 25% fewer parameters (counting both models with PyTorch’s two biases).
- No reliable winner. At 200 steps the native run favoured the GRU (1.000, 0.997, 1.000 against the LSTM’s 0.844, 0.990, 1.000), but a browser run of the same code left one GRU seed at 0.72. Small floating-point differences change individual training runs, so differences of this size are noise.
- A single small task proves little about general superiority, consistent with the mixed evidence in the literature. In a longer trial at lag 60, the GRU with bias 3 succeeded on one of three seeds.
GRUs in AI, robotics and aerospace
- Embedded inference: keyword spotting and sensor classifiers on microcontrollers often use GRUs for their size.
- Trajectory prediction: GRU encoders summarise agent histories in driving and drone-traffic models.
- Health monitoring: GRUs are a common baseline for spacecraft and aircraft telemetry anomaly detection.
References
- K. Cho et al. (2014). “Learning phrase representations using RNN encoder–decoder for statistical machine translation.” Proc. EMNLP.
- J. Chung, Ç. Gülçehre, K. Cho, Y. Bengio (2014). “Empirical evaluation of gated recurrent neural networks on sequence modeling.” arXiv:1412.3555.
- R. Jozefowicz, W. Zaremba, I. Sutskever (2015). “An empirical exploration of recurrent network architectures.” Proc. ICML.
- G. Weiss, Y. Goldberg, E. Yahav (2018). “On the practical computational power of finite precision RNNs for language recognition.” Proc. ACL.