#391SimpleRNN forward — outputs over a sequenceMediumRNNNeural Networks
SimpleRNN forward — outputs over a sequence
Background
The full RNN forward pass threads a hidden state through the sequence and emits an output at each step (the lesson's SimpleRNN.forward):
starting from , with the same weights reused at every step.
Problem statement
Implement rnn_forward(X, W_xh, W_hh, W_hy, b_h, b_y) returning the stacked outputs.
Input
X— shape(T, D): input at each timestep.W_xh—(D, H),W_hh—(H, H),W_hy—(H, O).b_h—(H,),b_y—(O,).
Output
Returns an np.ndarray of shape (T, O): the output at each timestep.
Examples
Example 1 — single timestep
Input: X = [[1]], W_xh = [[1]], W_hh = [[0]], W_hy = [[1]], b_h = [0], b_y = [0]
Output: [[0.76159416]]
Explanation: ; .
Constraints
- Start from
h = zeros(H)whereH = W_hh.shape[0]. - Each step:
h = tanh(x_t @ W_xh + h @ W_hh + b_h), theny = h @ W_hy + b_y. - Reuse the same weights every step; return shape
(T, O).
Notes
- Keep only
outputs[-1]for many-to-one; keep all for many-to-many. Differentiating through this loop is backpropagation 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.
- •Reference single timestep
- •Sample shape T by O
- •Example step-by-step