RNN Forward Pass
Implement a simple RNN forward pass (Elman RNN) using NumPy.
At each time step t, the RNN computes:
Where:
- is the input at time step t
- is the previous hidden state
- is the input-to-hidden weight matrix
- is the hidden-to-hidden weight matrix
- is the hidden bias
Input format:
- Line 1: input_size hidden_size seq_length (space-separated ints)
- Next input_size lines: W_xh matrix (hidden_size columns per line)
- Next hidden_size lines: W_hh matrix (hidden_size columns per line)
- Next line: b_h (hidden_size values)
- Next seq_length lines: one input vector per time step (input_size values)
- Next line: h_0 initial hidden state (hidden_size values)
Output: The final hidden state as a list, rounded to 4 decimal places.
Example
2 3 2 0.1 0.2 0.3 0.4 0.5 0.6 0.1 0.0 0.1 0.0 0.1 0.0 0.1 0.0 0.1 0.01 0.02 0.03 1.0 0.5 0.5 1.0 0.0 0.0 0.0
[0.2729, 0.1567, 0.3878]
Time step 1: x=[1.0, 0.5] W_xh @ x = [0.11+0.40.5, 0.21+0.50.5, 0.31+0.60.5] = [0.3, 0.45, 0.6] W_hh @ h_0 = [0, 0, 0] (h_0 is zeros) h_1 = tanh([0.3+0+0.01, 0.45+0+0.02, 0.6+0+0.03]) = tanh([0.31, 0.47, 0.63]) h_1 = [0.3004, 0.4382, 0.5581]
Time step 2: x=[0.5, 1.0] Similarly compute W_xh @ x + W_hh @ h_1 + b_h, then tanh. Final h_2 = [0.2729, 0.1567, 0.3878]
Constraints
- Use numpy for matrix operations
- Use np.tanh for activation
- Initial hidden state h_0 is provided
- Round final hidden state to 4 decimal places