Deep Learning · TEE · section
GRU two gates and one state
Recurrent neural networks
Definition. A gated recurrent unit (GRU) is a recurrent architecture that uses reset and update gates to control its hidden-state transition. Unlike a standard LSTM, it does not carry a separate cell state alongside the hidden state.
Gate equations and the convention used here
The reset gate r controls how the previous hidden state enters candidate formation. The update gate z controls the mixture of the old state and the candidate. Both are calculated from the current input and previous hidden state:
r_t=\sigma(W_rx_t+U_rh_{t-1}+b_r)z_t=\sigma(W_zx_t+U_zh_{t-1}+b_z)The candidate equation used in these notes applies reset before the recurrent candidate transformation:
\widetilde h_t=\tanh(W_hx_t+U_h(r_t\odot h_{t-1})+b_h)These notes define z as the fraction of new candidate admitted:
h_t=(1-z_t)\odot h_{t-1}+z_t\odot\widetilde h_tThus z near 0 favors retention of the old hidden state, while z near 1 favors the candidate. The reset gate affects the candidate calculation; it does not directly multiply the retained old-state term in the final mixture.
Complete numerical with supplied gate values
Question. A scalar GRU has hₜ₋₁ = 0.4, reset gate rₜ = 0.5, update gate zₜ = 0.25, xₜ = 1, Wₕ = 0.2, Uₕ = 0.5 and bₕ = 0. Calculate the candidate and new hidden state using hₜ = (1−zₜ)hₜ₋₁ + zₜh̃ₜ.
Given
Old hidden state = 0.4; r = 0.5; z = 0.25.
x = 1; W = 0.2; U = 0.5; b = 0.
Here z weights the candidate in the final mixture.
To find
Find candidate and new hidden state.
Formula — which one and why
GRU reset and state mixture. The reset gate controls old-state information entering the candidate. The stated update convention then mixes the original old state and the candidate.
r = reset gate; z = update gate; h_old = previous state; h_tilde = candidate. Here z weights the candidate; some questions instead define z to weight the old state.
\widetilde h=\tanh(Wx+U(r\odot h_{\mathrm{old}})+b)h=(1-z)\odot h_{\mathrm{old}}+z\odot\widetilde hYaad rakhne ka cue: Reset candidate ko control; update gate old-new mixture ko control.
Solution — step by step
Step 1: apply the reset gate to the previous hidden state.
r_th_{t-1}=0.5(0.4)=0.2The gates are already supplied as outputs, so no further sigmoid calculation is needed.
Step 2: calculate the candidate preactivation.
a_{\mathrm{candidate}}=W_hx_t+U_h(r_th_{t-1})+b_ha_{\mathrm{candidate}}=0.2(1)+0.5(0.2)+0=0.3Step 3: apply tanh to obtain the candidate.
\widetilde h_t=\tanh(0.3)\approx0.291312612Step 4: calculate the two mixture contributions.
(1-z_t)h_{t-1}=(1-0.25)(0.4)=0.3z_t\widetilde h_t=0.25\tanh(0.3)\approx0.072828153Step 5: add the contributions.
h_t=0.3+0.25\tanh(0.3)\approx0.372828153Final answer: candidate ≈ 0.291313 and new hidden state ≈ 0.372828. The new scalar state lies between the old state 0.4 and the candidate 0.291313, as expected for this convex mixture.
If the question uses the other update-gate convention
Some texts define z as the fraction of the old state retained:
h_t=z_th_{t-1}+(1-z_t)\widetilde h_tUnder that explicitly different convention, the same numerical value z = 0.25 gives:
h_t=0.25(0.4)+0.75\tanh(0.3)\approx0.318484459This is not the answer to the worked question above, which states the first convention. To represent an identical mixture while switching conventions, the gate values must be complemented as well. Always identify the final mixture equation before substituting values.
Relationship to LSTM
A basic GRU has three affine transformations: reset, update and candidate. A standard LSTM has four: forget, input, output and candidate. With equal input and hidden dimensions and the same bias convention, GRU therefore has fewer recurrent parameters. This is a parameter-count comparison, not a guarantee of higher accuracy or faster execution for every task.
Retain-old GRU with gate 0
Question. Use h_new = z h_old+(1−z) candidate. Given h_old = 0.4, z = 0, r = 0.5, x = 1, W = 0.2, U = 0.5 and b = 0, calculate the candidate and new state.
Given
h_old = 0.4; z = 0; r = 0.5; x = 1.
W = 0.2; U = 0.5; b = 0; z is the retain-old gate.
To find
Candidate and final hidden state.
Formula — which one and why
The reset gate changes old-state input to the candidate. The update gate separately mixes the original old state and candidate.
The question explicitly makes z multiply the old state. r and z do different jobs.
\widetilde h=\tanh(Wx+U(rh_{\mathrm{old}})+b)h_{\mathrm{new}}=zh_{\mathrm{old}}+(1-z)\widetilde hYaad rakhne ka cue: Reset sirf candidate mein; update mix mein original old state.
Solution — step by step
Step 1: reset the old state for candidate formation.
rh_{\mathrm{old}}=0.5(0.4)=0.2Step 2: calculate the candidate raw sum.
a=0.2(1)+0.5(0.2)+0=0.3Step 3: apply tanh.
\widetilde h=\tanh(0.3)\approx0.291312612Step 4: retain the old-state fraction.
0(0.4)=0Step 5: calculate the candidate fraction.
(1-0)(0.291312612)\approx0.291312612Step 6: add the two fractions.
h_{\mathrm{new}}=0+(0.291312612)\approx0.291312612Final answer: Candidate ≈ 0.291312612; new state ≈ 0.291312612.
Retain-old GRU with gate 0.8
Question. Use h_new = z h_old+(1−z) candidate. Given h_old = -0.2, z = 0.8, r = 0.5, x = 1, W = 0.2, U = 0.5 and b = 0, calculate the candidate and new state.
Given
h_old = -0.2; z = 0.8; r = 0.5; x = 1.
W = 0.2; U = 0.5; b = 0; z is the retain-old gate.
To find
Candidate and final hidden state.
Formula — which one and why
The reset gate changes old-state input to the candidate. The update gate separately mixes the original old state and candidate.
The question explicitly makes z multiply the old state. r and z do different jobs.
\widetilde h=\tanh(Wx+U(rh_{\mathrm{old}})+b)h_{\mathrm{new}}=zh_{\mathrm{old}}+(1-z)\widetilde hYaad rakhne ka cue: Reset sirf candidate mein; update mix mein original old state.
Solution — step by step
Step 1: reset the old state for candidate formation.
rh_{\mathrm{old}}=0.5(-0.2)=-0.1Step 2: calculate the candidate raw sum.
a=0.2(1)+0.5(-0.1)+0=0.15Step 3: apply tanh.
\widetilde h=\tanh(0.15)\approx0.148885034Step 4: retain the old-state fraction.
0.8(-0.2)=-0.16Step 5: calculate the candidate fraction.
(1-0.8)(0.148885034)\approx0.029777007Step 6: add the two fractions.
h_{\mathrm{new}}=-0.16+(0.029777007)\approx-0.130222993Final answer: Candidate ≈ 0.148885034; new state ≈ -0.130222993.