Implement the GRU update gate:
zt=σ(Wz[ht−1,xt]+bz).Here, ht−1 is the previous hidden state with H features, xt is the current input with D features, square brackets mean concatenation in that order, and σ is the sigmoid function. The weight rows produce H gate values. Accept one sample or a batch and return a matching float64 NumPy array.
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,) and (D,); batched inputs have shapes (N,H) and (N,D).
- W_z has shape (H,H+D) and b_z has shape (H,).
- Every input is float64 and finite.
All steps in GRU
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Update Gate
Implement the GRU update gate:
zt=σ(Wz[ht−1,xt]+bz).Here, ht−1 is the previous hidden state with H features, xt is the current input with D features, square brackets mean concatenation in that order, and σ is the sigmoid function. The weight rows produce H gate values. Accept one sample or a batch and return a matching float64 NumPy array.
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,) and (D,); batched inputs have shapes (N,H) and (N,D).
- W_z has shape (H,H+D) and b_z has shape (H,).
- Every input is float64 and finite.
All steps in GRU
Accepts: array
Accepts: array
Accepts: array
Accepts: array