Problems
Loading...
Step 5 of 6
GRU Cell
Easy

Implement one complete GRU cell. Compute the reset gate, update gate, candidate state, and new hidden state in this order:

rt=σ ⁣(Wr[ht−1,xt]+br).r_t=\sigma\!\left(W_r[h_{t-1},x_t]+b_r\right). zt=σ ⁣(Wz[ht−1,xt]+bz).z_t=\sigma\!\left(W_z[h_{t-1},x_t]+b_z\right). h~t=tanh⁡ ⁣(Wh[rt⊙ht−1,xt]+bh).\widetilde{h}_t=\tanh\!\left(W_h[r_t\odot h_{t-1},x_t]+b_h\right). ht=zt⊙ht−1+(1−zt)⊙h~t.h_t=z_t\odot h_{t-1}+(1-z_t)\odot\widetilde{h}_t.

Here, H is the hidden width, D is the input width, square brackets mean concatenation in the displayed order, and σ\sigma is sigmoid. Accept one sample or a batch and return hth_t as a matching float64 NumPy array.

Loading visualization...

Examples

Input: x_t = [1,0.5], h_prev = [0,0], W_r = [[0.2,0.1,0.1,0.2],[0.1,0.3,0.2,0.1]], W_z = [[0.1,0.2,0.3,0.1],[0.2,0.1,0.1,0.3]], W_h = [[0.3,0.1,0.2,0.1],[0.1,0.2,0.1,0.3]], b_r = [0,0], b_z = [0,0], b_h = [0,0]

Output: [0.101245,0.107231]

Explanation: The two gates and reset-aware candidate combine to produce the next hidden state.

Input: x_t = [0.5,1], h_prev = [0.3,-0.2], W_r = [[0.2,0.1,0.1,0.2],[0.1,0.3,0.2,0.1]], W_z = [[0.1,0.2,0.3,0.1],[0.2,0.1,0.1,0.3]], W_h = [[0.3,0.1,0.2,0.1],[0.1,0.2,0.1,0.3]], b_r = [0.1,0], b_z = [0,-0.1], b_h = [0.1,0.1]

Output: [0.313204,0.064833]

Input: x_t = [[1,0],[0,1]], h_prev = [[0,0],[0.5,-0.3]], W_r = [[0.2,0.1,0.1,0.2],[0.1,0.3,0.2,0.1]], W_z = [[0.1,0.2,0.3,0.1],[0.2,0.1,0.1,0.3]], W_h = [[0.3,0.1,0.2,0.1],[0.1,0.2,0.1,0.3]], b_r = [0,0], b_z = [0,0], b_h = [0,0]

Output: [[0.083995,0.047344],[0.341468,-0.059377]]

Hint 1

Reuse np.concatenate([h_prev, x_t], axis=-1) for both gates.

Hint 2

Build the candidate input from r_t * h_prev followed by x_t.

Hint 3

Finish with z_t * h_prev + (1 - z_t) * h_tilde.

Requirements

  • Compute both gates from [h_prev, x_t].
  • Apply the reset gate to h_prev before the candidate transform.
  • Blend h_prev and the candidate with the update gate.
  • Return only the float64 new hidden state.

Constraints

  • Unbatched x_t and h_prev have shapes (D,)(D,) and (H,)(H,); batched inputs have shapes (N,D)(N,D) and (N,H)(N,H).
  • W_r, W_z, and W_h have shape (H,H+D)(H,H+D).
  • b_r, b_z, and b_h have shape (H,)(H,).