RNN Forward Step

Hard
~20 min
code completion

RNN: Single Time Step

A Recurrent Neural Network (RNN) processes sequences by maintaining a hidden state that is updated at each time step:

where:

  • : previous hidden state, shape (hidden_size,)
  • : current input, shape (input_size,)
  • : hidden-to-hidden weight matrix, shape (hidden_size, hidden_size)
  • : input-to-hidden weight matrix, shape (hidden_size, input_size)
  • : bias, shape (hidden_size,)
  • : applied element-wise, squashes to (-1, 1)
  • Your task:

    Implement rnn_step(h_prev, x, Wh, Wx, b) that returns the next hidden state .

    Example Tests

    Zero inputs and prev state: output is tanh(b)

    Input: {"b":[0,0],"x":[0,0],"Wh":[[1,0],[0,1]],"Wx":[[1,0],[0,1]],"h_prev":[0,0]}

    Expected: [0,0]

    Identity Wh, zero Wx and b: output = tanh(h_prev)

    Input: {"b":[0,0],"x":[0,0],"Wh":[[1,0],[0,1]],"Wx":[[0,0],[0,0]],"h_prev":[1,0]}

    Expected: [0.76159,0]

    tanh squashes large values to ±1

    Input: {"b":[0,0],"x":[1,0],"Wh":[[0,0],[0,0]],"Wx":[[100,0],[0,1]],"h_prev":[0,0]}

    Expected: [1,0]

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.