๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #232

GRU Equations

This note assembles GRU's complete set of equations โ€” just three, compared to LSTM's six โ€” into one unified reference, then works through a full numerical example by hand.

The Complete Set of Equations

\[ \mathbf{z}_t = \sigma(\mathbf{W}_z[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_z) \qquad \text{(update gate)} \] \[ \mathbf{r}_t = \sigma(\mathbf{W}_r[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_r) \qquad \text{(reset gate)} \] \[ \tilde{\mathbf{h}}_t = \tanh(\mathbf{W}_h[\mathbf{r}_t\odot\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_h) \qquad \text{(candidate hidden state)} \] \[ \mathbf{h}_t = (1-\mathbf{z}_t)\odot\mathbf{h}_{t-1} + \mathbf{z}_t\odot\tilde{\mathbf{h}}_t \qquad \text{(final hidden state)} \]

Full Numerical Worked Example

Setup: \(h_{t-1}=0.3\), \(x_t=1.0\), combined \([h_{t-1},x_t]=[0.3,1.0]\).

\[ \mathbf{W}_z=[0.4,0.2], b_z=0 \qquad \mathbf{W}_r=[-0.3,0.5], b_r=0.1 \qquad \mathbf{W}_h=[0.6,-0.2], b_h=0 \]

Update gate:

\[ z_t = \sigma(0.4(0.3)+0.2(1.0)) = \sigma(0.12+0.2) = \sigma(0.32) \approx 0.5793 \]

Reset gate:

\[ r_t = \sigma(-0.3(0.3)+0.5(1.0)+0.1) = \sigma(-0.09+0.5+0.1) = \sigma(0.51) \approx 0.6248 \]

Candidate hidden state (using \(r_t \odot h_{t-1} = 0.6248 \times 0.3 \approx 0.1874\)):

\[ \tilde h_t = \tanh(0.6(0.1874)+(-0.2)(1.0)) = \tanh(0.1124-0.2) = \tanh(-0.0876) \approx -0.0874 \]

Final hidden state:

\[ h_t = (1-0.5793)(0.3)+(0.5793)(-0.0874) \approx (0.4207)(0.3)+(0.5793)(-0.0874) \approx 0.1262-0.0506 \approx 0.0756 \]

Code โ€” Verifying Every Value

import torch

h_prev = torch.tensor(0.3)
x_t = torch.tensor(1.0)

W_z, b_z = torch.tensor([0.4, 0.2]), torch.tensor(0.0)
W_r, b_r = torch.tensor([-0.3, 0.5]), torch.tensor(0.1)
W_h, b_h = torch.tensor([0.6, -0.2]), torch.tensor(0.0)

combined = torch.stack([h_prev, x_t])
z_t = torch.sigmoid(torch.dot(W_z, combined) + b_z)
r_t = torch.sigmoid(torch.dot(W_r, combined) + b_r)

reset_combined = torch.stack([r_t * h_prev, x_t])
h_candidate = torch.tanh(torch.dot(W_h, reset_combined) + b_h)

h_t = (1 - z_t) * h_prev + z_t * h_candidate

print("z_t:", z_t.item())          # 0.5793
print("r_t:", r_t.item())          # 0.6248
print("h_candidate:", h_candidate.item())   # -0.0874
print("h_t:", h_t.item())          # 0.0756 -- matches every hand-computed value

Comparing Complexity Directly: 3 Equations vs 6

GRU's three equations (plus the final blend) achieve a structurally similar gated-memory effect to LSTM's six, with fewer independent weight sets to learn โ€” this is the concrete numerical face of the parameter-count reduction discussed in GRU Architecture.

Common Mistakes

  • Forgetting that the reset gate must be applied before concatenating with \(x_t\) for the candidate computation, not as a separate later step โ€” \(\mathbf{r}_t\odot\mathbf{h}_{t-1}\) is computed first, then concatenated with \(\mathbf{x}_t\), then passed through \(\mathbf{W}_h\).
  • Using \(\mathbf{z}_t\) instead of \((1-\mathbf{z}_t)\) for the old-state term in the final blend, or vice versa โ€” mixing these up inverts the intended behavior of the update gate entirely.

Interview Relevance

Q: "Write out all four GRU equations from memory." The update gate, reset gate, candidate hidden state (using the reset-gated previous hidden state), and the final hidden state blend using the update gate and its complement โ€” being able to reproduce these correctly, including exactly where the reset gate's multiplication occurs, is a common practical check of genuine understanding versus memorized keywords.

Practice Question

Using the same weights as the worked example, compute the update gate value for a new input \(x_t = -1.0\) with the same \(h_{t-1}=0.3\).

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

GRU Equations โ€“ FAQs

Quick answers about learning GRU Equations in Deep Learning.

This free note from CodingNow 2.0 explains GRU Equations in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including GRU Equations, is 100% free with no signup required.
With focused practice, most students grasp GRU Equations in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now