v9.1.1
Menu

RNN Forward Pass

mediumPROSequence ProcessingNLP 2: Applications

Implement a simple RNN forward pass (Elman RNN) using NumPy.

At each time step t, the RNN computes: ht=tanh⁡(Wxh⋅xt+Whh⋅ht−1+bh)h_t = \tanh(W_{xh} \cdot x_t + W_{hh} \cdot h_{t-1} + b_h)

Where:

  • xtx_t is the input at time step t
  • ht−1h_{t-1} is the previous hidden state
  • WxhW_{xh} is the input-to-hidden weight matrix
  • WhhW_{hh} is the hidden-to-hidden weight matrix
  • bhb_h 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

Input
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
Output
[0.2729, 0.1567, 0.3878]
Reasoning

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

Learn this in

More from NLP 2: Applications

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.