Implement the GRU reset gate:
rt=σ(Wr[ht−1,xt]+br).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_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,) and (D,); batched inputs have shapes (N,H) and (N,D).
- W_r has shape (H,H+D) and b_r has shape (H,).
- Every input is float64 and finite.
All steps in GRU
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Reset Gate
Implement the GRU reset gate:
rt=σ(Wr[ht−1,xt]+br).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_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,) and (D,); batched inputs have shapes (N,H) and (N,D).
- W_r has shape (H,H+D) and b_r has shape (H,).
- Every input is float64 and finite.
All steps in GRU
Accepts: array
Accepts: array
Accepts: array
Accepts: array