PIXELBANKv9.1.0
Menu

Implement a single step of a vanilla RNN cell.

The RNN update equations for one timestep:

ht=tanh⁡(Whh⋅ht−1+Wxh⋅xt+bh)h_t = \tanh(W_{hh} \cdot h_{t-1} + W_{xh} \cdot x_t + b_h)

Given the previous hidden state ht−1h_{t-1}, current input xtx_t, and weight matrices, compute the new hidden state hth_t.

Return hth_t as a list, rounded to 4 decimal places.

Example:

Input:
h_prev = [0, 0]
x = [1]
W_hh = [[0.1, 0.2], [0.3, 0.4]]
W_xh = [[0.5], [0.6]]
b_h = [0, 0]
Output:
[0.4621, 0.5370]
Reasoning:
  • First, we calculate the product of WhhW_{hh} and ht−1h_{t-1}: Whh⋅ht−1=[0.10.20.30.4]⋅[00]=[00]W_{hh} \cdot h_{t-1} = \begin{bmatrix} 0.1 & 0.2 \\ 0.3 & 0.4 \end{bmatrix} \cdot \begin{bmatrix} 0 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \end{bmatrix}
  • Then, we calculate the product of WxhW_{xh} and xtx_t: Wxh⋅xt=[0.50.6]⋅[1]=[0.50.6]W_{xh} \cdot x_t = \begin{bmatrix} 0.5 \\ 0.6 \end{bmatrix} \cdot \begin{bmatrix} 1 \end{bmatrix} = \begin{bmatrix} 0.5 \\ 0.6 \end{bmatrix}
  • Next, we add the results of the previous steps and bhb_h: [00]+[0.50.6]+[00]=[0.50.6]\begin{bmatrix} 0 \\ 0 \end{bmatrix} + \begin{bmatrix} 0.5 \\ 0.6 \end{bmatrix} + \begin{bmatrix} 0 \\ 0 \end{bmatrix} = \begin{bmatrix} 0.5 \\ 0.6 \end{bmatrix}
  • Finally, we apply the tanh⁡\tanh function to get hth_t: ht=tanh⁡[0.50.6]=[tanh⁡(0.5)tanh⁡(0.6)]=[0.46210.5370]h_t = \tanh\begin{bmatrix} 0.5 \\ 0.6 \end{bmatrix} = \begin{bmatrix} \tanh(0.5) \\ \tanh(0.6) \end{bmatrix} = \begin{bmatrix} 0.4621 \\ 0.5370 \end{bmatrix}

Constraints:

  • h_prev: 1D list (hidden_size)
  • x: 1D list (input_size)
  • W_hh: 2D list (hidden_size x hidden_size)
  • W_xh: 2D list (hidden_size x input_size)
  • b_h: 1D list (hidden_size)
  • Return 1D list of new hidden state values rounded to 4 decimal places
🔒

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

0/0
Run code to see test results.
RNN Cell Forward - Medium | PixelBank