RNN Over a Sequence

Hard
~20 min
code completion

RNN: Unrolling Over Time

To process a sequence , we apply the RNN step repeatedly, feeding each output hidden state back as the next input:

h_0 → [RNN] → h_1 → [RNN] → h_2 → ... → h_T
         ↑               ↑
         x_1             x_2

The final hidden state is often used as a fixed-size encoding of the entire input sequence (e.g., for sequence classification or as an encoder in seq2seq models).

Your task:

Implement rnn_sequence(X, h0, Wh, Wx, b) that processes all time steps and returns the final hidden state.

Example Tests

T=1 equals single rnn_step

Input: {"X":[[1,0]],"b":[0,0],"Wh":[[0,0],[0,0]],"Wx":[[1,0],[0,1]],"h0":[0,0]}

Expected: [0.76159,0]

All-zero sequence: final state = h0 processed T times

Input: {"X":[[0],[0],[0]],"b":[0],"Wh":[[1]],"Wx":[[1]],"h0":[0]}

Expected: [0]

Output shape matches hidden size

Input: {"X":[[1,2],[3,4],[5,6]],"b":[0,0,0],"Wh":[[0,0,0],[0,0,0],[0,0,0]],"Wx":[[1,0],[0,1],[1,1]],"h0":[0,0,0]}

Expected: [3]

Sign in to solve this problem

You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.