Skip to content

← Model Architectures step 7 of 9

Medium End-to-End

GRU Sequence Forward Pass

A gated recurrent unit (GRU) updates a hidden state using a candidate value and two gates. The reset gate controls how much of the old state contributes to the candidate. The update gate blends that candidate with the old state.

This exercise processes a complete sequence and returns only the final state. Use the particular reset placement and update convention specified below.

Inputs and output

Implement gru_forward(x, W_xr, W_xz, W_xn, W_hr, W_hz, W_hn, b_r, b_z, b_n, h0).

  • x has shape (seq_len, input_dim).
  • W_xr, W_xz and W_xn each have shape (input_dim, hidden_dim).
  • W_hr, W_hz and W_hn each have shape (hidden_dim, hidden_dim).
  • b_r, b_z, b_n and h0 each have shape (hidden_dim,).

All arrays are floating-point and all dimensions are positive. There is no batch axis. Return shape (hidden_dim,), not a stack of intermediate states. Do not change any input.

Update one position

Start with h = h0. For each row x[t], compute the following using the same previous h throughout the step:

r = sigmoid(x[t] @ W_xr + h @ W_hr + b_r)
z = sigmoid(x[t] @ W_xz + h @ W_hz + b_z)
candidate = tanh(x[t] @ W_xn + (r * h) @ W_hn + b_n)
new_h = (1 - z) * candidate + z * h

Then set h to new_h and continue. All * operations are elementwise; @ denotes matrix multiplication. Use stable sigmoid operations.

The reset multiplication happens before W_hn: (r * h) @ W_hn. It is generally different from r * (h @ W_hn) because the matrix can mix hidden features. Do not interchange these expressions or substitute a built-in GRU with a different reset convention.

In this update convention, z near one retains more of the old state; z near zero favors the candidate. The reset gate r affects the candidate branch only. It does not directly multiply the retained z * h branch.

Example

If all weights and biases are zero, both gates equal 0.5 and the candidate is zero. Starting from h0 = [1], one step returns [0.5] and two steps return [0.25]. This shows both the carry between positions and the rule that only the final state is returned.