GRU gates
Quick answer
A gated recurrent unit (GRU) combines memory and output into a single hidden state. Its reset and update gates control candidate construction and how much previous state to retain, often with fewer parameters than an LSTM of equal width. Examples are static Python snippets tested locally on CPU; PyTorch is required for selected examples. ByteMind execution is unverified.
Controlled GRU update
import torchcell=torch.nn.GRUCell(1,1)with torch.no_grad(): for p in cell.parameters():p.zero_() h=cell(torch.zeros(1,1),torch.tensor([[2.]]))print(h.item())Expected output (locally tested): 1.0. Zero weights imply z=0.5 and n=0, so half the previous state survives.
How it works
Use PyTorch convention: r=sigmoid(input_r+hidden_r), z=sigmoid(input_z+hidden_z), n=tanh(input_n+rhidden_n), h=(1-z)n+zh_prev. The input and hidden terms each include their own bias. PyTorch applies reset after the hidden affine candidate transform, which differs from formulations that reset before matrix multiplication; do not mix conventions when copying parameters. The packed weight rows are r,z,n. The update gate z near 1 preserves old state; near 0 accepts the candidate. GRU has no separate cell state. With one layer and both bias vectors, parameter count is 3H*(I+H+2), compared with 4H(I+H+2) for a standard LSTM of equal input width I and hidden width H. Readout parameters are extra. The smaller count does not imply faster runtime or superior accuracy on all workloads.
Count native recurrent parameters
import torchfor cls in (torch.nn.GRU,torch.nn.LSTM): m=cls(3,5) print(cls.__name__,sum(p.numel() for p in m.parameters()))Expected output (locally tested): GRU 150 LSTM 200. Three versus four sets of input and hidden affine parameters including both biases.
Common mistakes
Do not invert the meaning of z when using PyTorch equations. Do not assume GRU accepts an LSTM-style (h,c) tuple. Do not move the reset multiplication across a general matrix product.
Practice and next steps
Check the interpolation for z=0,0.5,1 and compare parameter counts using a native GRU and LSTM.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
GRU gates — knowledge check
Apply gru gates to small examples and debugging scenarios.
In PyTorch GRU, z near 1 does what?
How many separate recurrent states does GRU maintain?
PyTorch GRU gate order?
Where does PyTorch apply reset in candidate?
Does fewer parameters ensure lower latency?