#395Unroll the recurrence over a sequenceMedium

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:

ht=tanh(Wxhxt+Whhht1+b),h1=h0\mathbf{h}_t = \tanh(W_{xh}\mathbf{x}_t + W_{hh}\mathbf{h}_{t-1} + \mathbf{b}), \qquad \mathbf{h}_{-1} = \mathbf{h}_0

Collecting every ht\mathbf{h}_t shows how memory accumulates: ht\mathbf{h}_t depends on all inputs up to tt.

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 — length H: the initial hidden state.
  • W_xh(H, D), W_hh(H, H), b — length H.

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: h0=tanh(1)0.7616h_0 = \tanh(1) \approx 0.7616; then h1=tanh(0+10.7616)=tanh(0.7616)0.6420h_1 = \tanh(0 + 1\cdot0.7616) = \tanh(0.7616) \approx 0.6420 — the second step has no input yet still moves, because it remembers.

Constraints

  • Start from h0; at each t set h = 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