Compute the candidate hidden state of a GRU:
ht=tanh(Wh[rt⊙ht−1,xt]+bh).Here, rt is the reset gate, ht−1 is the previous hidden state with H features, and xt is the current input with D features. Concatenate the gated hidden state before the input. Accept one sample or a batch and return a matching float64 NumPy array.
Examples
Input: r_t = [0.5,0.5], h_prev = [0,0], x_t = [1,0.5], W_h = [[0.3,0.1,0.2,0.1],[0.1,0.2,0.1,0.3]], b_h = [0,0]
Output: [0.244919,0.244919]
Explanation: The reset gate first scales the old state, then tanh bounds the affine result.
Input: r_t = [0,1], h_prev = [2,-1], x_t = [0.5,1], W_h = [[0.2,0.3,0.4,0.1],[0.1,0.2,0.3,0.4]], b_h = [0.1,-0.1]
Output: [0.099668,0.244919]
Input: r_t = [[0.5,0.5],[0.2,0.8]], h_prev = [[0,0],[1,-1]], x_t = [[1,0.5],[0.5,1]], W_h = [[0.3,0.1,0.2,0.1],[0.1,0.2,0.1,0.3]], b_h = [0,0]
Output: [[0.244919,0.244919],[0.178081,0.206966]]
Hint 1
Form gated_h = r_t * h_prev before concatenating.
Hint 2
Apply np.tanh(joined @ W_h.T + b_h).
Requirements
- Multiply r_t and h_prev elementwise before concatenation.
- Concatenate the gated hidden state before x_t.
- Apply the supplied affine transform and tanh.
- Return a float64 NumPy array with the matching shape.
Constraints
- Unbatched r_t and h_prev have shape (H,) and x_t has shape (D,).
- Batched inputs have shapes (N,H), (N,H), and (N,D).
- W_h has shape (H,H+D) and b_h has shape (H,).
All steps in GRU
- Reset Gate
- Update Gate
- Candidate Hidden
- Hidden State Update
- GRU Cell
- Complete GRU
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Candidate Hidden
Compute the candidate hidden state of a GRU:
ht=tanh(Wh[rt⊙ht−1,xt]+bh).Here, rt is the reset gate, ht−1 is the previous hidden state with H features, and xt is the current input with D features. Concatenate the gated hidden state before the input. Accept one sample or a batch and return a matching float64 NumPy array.
Examples
Input: r_t = [0.5,0.5], h_prev = [0,0], x_t = [1,0.5], W_h = [[0.3,0.1,0.2,0.1],[0.1,0.2,0.1,0.3]], b_h = [0,0]
Output: [0.244919,0.244919]
Explanation: The reset gate first scales the old state, then tanh bounds the affine result.
Input: r_t = [0,1], h_prev = [2,-1], x_t = [0.5,1], W_h = [[0.2,0.3,0.4,0.1],[0.1,0.2,0.3,0.4]], b_h = [0.1,-0.1]
Output: [0.099668,0.244919]
Input: r_t = [[0.5,0.5],[0.2,0.8]], h_prev = [[0,0],[1,-1]], x_t = [[1,0.5],[0.5,1]], W_h = [[0.3,0.1,0.2,0.1],[0.1,0.2,0.1,0.3]], b_h = [0,0]
Output: [[0.244919,0.244919],[0.178081,0.206966]]
Hint 1
Form gated_h = r_t * h_prev before concatenating.
Hint 2
Apply np.tanh(joined @ W_h.T + b_h).
Requirements
- Multiply r_t and h_prev elementwise before concatenation.
- Concatenate the gated hidden state before x_t.
- Apply the supplied affine transform and tanh.
- Return a float64 NumPy array with the matching shape.
Constraints
- Unbatched r_t and h_prev have shape (H,) and x_t has shape (D,).
- Batched inputs have shapes (N,H), (N,H), and (N,D).
- W_h has shape (H,H+D) and b_h has shape (H,).
All steps in GRU
- Reset Gate
- Update Gate
- Candidate Hidden
- Hidden State Update
- GRU Cell
- Complete GRU
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array