PIXELBANKv9.1.0
Menu

RNN Single Step Forward

Problem Statement

Compute the hidden state for a single timestep in a standard RNN.

Background

The update rule for a vanilla RNN is:

at=tanh⁑(Waaβ‹…atβˆ’1+Waxβ‹…xt+ba)a_t = \tanh(W_{aa} \cdot a_{t-1} + W_{ax} \cdot x_t + b_a)

Where:

  • ata_t is the new hidden state
  • atβˆ’1a_{t-1} is the previous hidden state
  • xtx_t is the input at timestep tt
  • WaaW_{aa} is the hidden-to-hidden weight matrix
  • WaxW_{ax} is the input-to-hidden weight matrix
  • bab_a is the bias vector

Your Task

Write a function rnn_step(prev_hidden, input_vec, W_aa, W_ax, b_a) that computes and returns the new hidden state using np.tanh.

Input Format

  • prev_hidden: numpy array of shape (hidden_dim,)
  • input_vec: numpy array of shape (input_dim,)
  • W_aa: numpy array of shape (hidden_dim, hidden_dim)
  • W_ax: numpy array of shape (hidden_dim, input_dim)
  • b_a: numpy array of shape (hidden_dim,)

Output Format

Return a numpy array of shape (hidden_dim,) representing the new hidden state.

Example:

Input:
prev_hidden=[0,0], input_vec=[1,2], W_aa=[[0.1,0.2],[0.3,0.4]], W_ax=[[0.5,0.6],[0.7,0.8]], b_a=[0,0]
Output:
[0.9354, 0.9801] (approximately)
Reasoning:

tanh(W_aa @ prev_h + W_ax @ x + b_a) = tanh([1.7, 2.3])

Constraints:

  • All inputs are valid numpy arrays with compatible dimensions
  • hidden_dim and input_dim are between 1 and 512
solution.py

Test Results

0/0
Run code to see test results.