Problems
Loading...
Step 1 of 6
Reset Gate
Easy

Implement the GRU reset gate:

rt=σ ⁣(Wr[ht−1,xt]+br).r_t=\sigma\!\left(W_r[h_{t-1},x_t]+b_r\right).

Here, ht−1h_{t-1} is the previous hidden state with H features, xtx_t is the current input with D features, square brackets mean concatenation in that order, and σ\sigma is the sigmoid function. The weight rows produce H gate values. Accept one sample or a batch and return a matching float64 NumPy array.

Loading visualization...

Examples

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

Output: [0.549834,0.562177]

Explanation: Concatenation, the reset-gate affine transform, and sigmoid produce one gate value per hidden feature.

Input: h_prev = [0.5,-0.3], x_t = [1,0], W_r = [[0.3,0.2,0.1,0.4],[0.1,0.4,0.3,0.2]], b_r = [0.1,-0.1]

Output: [0.571996,0.532454]

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

Output: [[0.549834,0.562177],[0.571996,0.542398]]

Hint 1

Use np.concatenate([h_prev, x_t], axis=-1).

Hint 2

The affine transform is joined @ W_r.T + b_r.

Requirements

  • Concatenate h_prev before x_t along the last axis.
  • Apply the supplied weight matrix and bias.
  • Apply sigmoid elementwise.
  • Return a float64 NumPy array with the matching unbatched or batched shape.

Constraints

  • Unbatched inputs have shapes (H,)(H,) and (D,)(D,); batched inputs have shapes (N,H)(N,H) and (N,D)(N,D).
  • W_r has shape (H,H+D)(H,H+D) and b_r has shape (H,)(H,).
  • Every input is float64 and finite.