One GRU Step: Update and Reset Gates

Neural NetworksMedium+20 XP+15

One GRU Step: Update and Reset Gates

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 gate
2r = sigmoid(Wr·x + Ur·h + br) reset gate
3h_tilde = tanh(Wh·x + Uh·(r * h) + bh) candidate state
4h_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.
  • Everything else is element-wise too, apart from the matrix-vector products.

The interesting term is (1 - z) * h. When z is 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.

Examples

x = [1.0]
h_prev = [0.0]
params = {'Wz': [[0.5]], 'Uz': [[0.1]], 'bz': [0.0], 'Wr': [[0.3]], 'Ur': [[0.2]], 'br': [0.0], 'Wh': [[0.7]], 'Uh': [[0.4]], 'bh': [0.0]}
returns [0.3762]
x = [0.5]
h_prev = [0.2]
params = {'Wz': [[0.5]], 'Uz': [[0.1]], 'bz': [0.0], 'Wr': [[0.3]], 'Ur': [[0.2]], 'br': [0.0], 'Wh': [[0.7]], 'Uh': [[0.4]], 'bh': [0.0]}
returns [0.299]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run