A GRU is an RNN cell with two learned gates deciding what to keep and what to overwrite. One forward step, given input x and previous hidden state h:
1z = sigmoid(Wz·x + Uz·h + bz) update gate2r = sigmoid(Wr·x + Ur·h + br) reset gate3h_tilde = tanh(Wh·x + Uh·(r * h) + bh) candidate state4h_new = (1 - z) * h + z * h_tilde
Task: write gru_cell(x, h_prev, params) returning the new hidden state, rounded to 4 decimal places.
params is a dict with keys Wz, Uz, bz, Wr, Ur, br, Wh, Uh, bh. Each W/U is a matrix stored as a list of rows; each b is a vector.
r multiplies h_prev element-wise, before that product is fed through Uh.The interesting term is
(1 - z) * h. Whenzis near zero the cell copies its previous state forward untouched, and a gradient flowing back through that path is multiplied by roughly 1 — which is how gated cells avoid the vanishing gradients that sink a plain RNN.