Problems
Loading...
Step 1 of 6
RNN Cell
Easy

Implement one vanilla RNN cell.

ht=tanh⁡(Wxhxt+Whhht−1+bh).h_t=\tanh\left(W_{xh}x_t+W_{hh}h_{t-1}+b_h\right).

Here, x_t has D input features and h_prev has H hidden features. Accept one sample or a batch and return h_t as a matching float64 NumPy array.

Loading visualization...

Examples

Input: x_t = [1,0.5], h_prev = [0,0], W_xh = [[0.1,0.2],[0.3,0.4]], W_hh = [[0.5,0.1],[0.2,0.3]], b_h = [0,0]

Output: [0.197375,0.462117]

Explanation: With a zero previous state, only the input projection and bias contribute before tanh.

Input: x_t = [0.5,1], h_prev = [0.2,-0.4], W_xh = [[0.1,0.2],[0.3,0.4]], W_hh = [[0.5,0.1],[0.2,0.3]], b_h = [0.1,-0.1]

Output: [0.388473,0.353992]

Input: x_t = [[1,0],[0,1]], h_prev = [[0,0],[0.5,-0.3]], W_xh = [[0.2,0.1],[0.1,0.3]], W_hh = [[0.4,0.2],[0.2,0.5]], b_h = [0,0]

Output: [[0.197375,0.099668],[0.235496,0.244919]]

Hint 1

Use x_t @ W_xh.T and h_prev @ W_hh.T.

Hint 2

Apply np.tanh after adding both projections and b_h.

Requirements

  • Add the input projection, recurrent projection, and hidden bias.
  • Apply tanh elementwise.
  • Return a float64 array with H features per sample.

Constraints

  • Unbatched x_t and h_prev have shapes (D,) and (H,); batched versions have shapes (N, D) and (N, H).
  • W_xh has shape (H, D), W_hh has shape (H, H), and b_h has shape (H,).