Problems
Loading...
Step 3 of 6
Candidate Hidden
Easy

Compute the candidate hidden state of a GRU:

h~t=tanh⁡ ⁣(Wh[rt⊙ht−1,xt]+bh).\widetilde{h}_t=\tanh\!\left(W_h[r_t\odot h_{t-1},x_t]+b_h\right).

Here, rtr_t is the reset gate, ht−1h_{t-1} is the previous hidden state with H features, and xtx_t 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.

Loading visualization...

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,)(H,) and x_t has shape (D,)(D,).
  • Batched inputs have shapes (N,H)(N,H), (N,H)(N,H), and (N,D)(N,D).
  • W_h has shape (H,H+D)(H,H+D) and b_h has shape (H,)(H,).