Problems
Loading...
Step 3 of 6
Forward Through Sequence
Medium

Process an entire sequence with a vanilla RNN while retaining every hidden state.

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

Return a dictionary containing hidden_states with shape (N, T, H) and final_hidden_state with shape (N, H), both float64 NumPy arrays.

Loading visualization...

Examples

Input: X = [[[1,0.5],[0.5,1]]], h_0 = [[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: {"hidden_states":[[[0.197375,0.462117],[0.375576,0.621908]]],"final_hidden_state":[[0.375576,0.621908]]}

Explanation: Each hidden state becomes the recurrent input to the following time step and is also stored in the output sequence.

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

Output: {"hidden_states":[[[0.438199,-0.300437],[0.240435,-0.161161],[-0.11937,0.067404]]],"final_hidden_state":[[-0.11937,0.067404]]}

Input: X = [[[1],[2]],[[0],[-1]]], h_0 = [[0,0],[0.5,-0.5]], W_xh = [[0.2],[0.4]], W_hh = [[0.3,0.1],[0.2,0.4]], b_h = [0,0]

Output: {"hidden_states":[[[0.197375,0.379949],[0.459918,0.757982]],[[0.099668,-0.099668],[-0.178145,-0.396874]]],"final_hidden_state":[[0.459918,0.757982],[-0.178145,-0.396874]]}

Hint 1

Initialize hidden with h_0.copy().

Hint 2

Read X[:, step, :] inside the time loop.

Hint 3

Stack saved states along axis 1.

Requirements

  • Begin recurrence from the supplied h_0.
  • Process time steps in their original order with shared parameters.
  • Return exactly the keys hidden_states and final_hidden_state.

Constraints

  • X has shape (N, T, D), and h_0 has shape (N, H).
  • W_xh has shape (H, D), W_hh has shape (H, H), and b_h has shape (H,).
  • Returned arrays use float64.