Problems
Loading...
Step 2 of 6
Update Gate
Easy

Implement the GRU update gate:

zt=σ ⁣(Wz[ht−1,xt]+bz).z_t=\sigma\!\left(W_z[h_{t-1},x_t]+b_z\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_z = [[0.1,0.2,0.3,0.1],[0.2,0.1,0.1,0.3]], b_z = [0,0]

Output: [0.586618,0.562177]

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

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

Output: [0.559714,0.571996]

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

Output: [[0.574443,0.524979],[0.522485,0.591459]]

Hint 1

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

Hint 2

The affine transform is joined @ W_z.T + b_z.

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_z has shape (H,H+D)(H,H+D) and b_z has shape (H,)(H,).
  • Every input is float64 and finite.