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). zt=σ(Wz[ht−1,xt]+bz). ht=tanh(Wh[rt⊙ht−1,xt]+bh). ht=zt⊙ht−1+(1−zt)⊙ht.Here, H is the hidden width, D is the input width, square brackets mean concatenation in the displayed order, and σ is sigmoid. Accept one sample or a batch and return ht as a matching float64 NumPy array.
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,) and (H,); batched inputs have shapes (N,D) and (N,H).
- W_r, W_z, and W_h have shape (H,H+D).
- b_r, b_z, and b_h have shape (H,).
All steps in GRU
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
GRU Cell
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). zt=σ(Wz[ht−1,xt]+bz). ht=tanh(Wh[rt⊙ht−1,xt]+bh). ht=zt⊙ht−1+(1−zt)⊙ht.Here, H is the hidden width, D is the input width, square brackets mean concatenation in the displayed order, and σ is sigmoid. Accept one sample or a batch and return ht as a matching float64 NumPy array.
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,) and (H,); batched inputs have shapes (N,D) and (N,H).
- W_r, W_z, and W_h have shape (H,H+D).
- b_r, b_z, and b_h have shape (H,).
All steps in GRU
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array