#395Unroll the recurrence over a sequenceMediumRNN
Unroll the recurrence over a sequence
Background
The "unrolled" view of an RNN runs the same cell at each timestep, threading the hidden state through:
Collecting every shows how memory accumulates: depends on all inputs up to .
Problem statement
Implement unroll(X, h0, W_xh, W_hh, b) returning the stacked hidden states for the whole sequence.
Input
X— shape(T, D): input at each timestep.h0— lengthH: the initial hidden state.W_xh—(H, D),W_hh—(H, H),b— lengthH.
Output
Returns an np.ndarray of shape (T, H): the hidden state after each timestep.
Examples
Example 1 — memory carries forward
Input: X = [[1], [0]], h0 = [0], W_xh = [[1]], W_hh = [[1]], b = [0]
Output: [[0.76159416], [0.64200466]]
Explanation: ; then — the second step has no input yet still moves, because it remembers.
Constraints
- Start from
h0; at eachtseth = tanh(W_xh @ X[t] + W_hh @ h + b). - Reuse the same weights at every step.
- Return shape
(T, H).
Notes
- The final row
h[-1]is the "many-to-one" summary used for sequence classification; the full stack feeds backprop-through-time.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example -- memory carries with zero input
- •Reference 2D step-by-step
- •Sample nonzero initial state