RNN Forward Step

~20 mincode completion

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

Examples

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

Input
rnn_step([0, 0], [0, 0], [[1, 0], [0, 1]], [[1, 0], [0, 1]], [0, 0])
Output
[0, 0]

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

Input
rnn_step([1, 0], [0, 0], [[1, 0], [0, 1]], [[0, 0], [0, 0]], [0, 0])
Output
[0.76159, 0]

tanh squashes large values to ±1

Input
rnn_step([0, 0], [1, 0], [[0, 0], [0, 0]], [[100, 0], [0, 1]], [0, 0])
Output
[1, 0]

Hints

Hint 1

Use a matrix product rather than nested loops, and check which operand transposes.

Hint 2

Reach for tanh rather than sigmoid.

Requirements

  • h_prev: Previous hidden state, shape (hidden_size,)

  • x: Current input, shape (input_size,)

  • Wh: Hidden-to-hidden weights, shape (hidden_size, hidden_size)

  • Wx: Input-to-hidden weights, shape (hidden_size, input_size)

  • b: Bias, shape (hidden_size,)

  • Return Next hidden state h_t, shape (hidden_size,).

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~20 min

8 employers weight this skill

3 quant funds, 3 frontier labs, 2 AI product companies. Top match scores 82.

Python
import numpy as np

def rnn_step(h_prev: np.ndarray, x: np.ndarray, Wh: np.ndarray, Wx: np.ndarray, b: np.ndarray) -> np.ndarray:
    """
    Perform one RNN time step.

    Args:
        h_prev: Previous hidden state, shape (hidden_size,)
        x:      Current input, shape (input_size,)
        Wh:     Hidden-to-hidden weights, shape (hidden_size, hidden_size)
        Wx:     Input-to-hidden weights, shape (hidden_size, input_size)
        b:      Bias, shape (hidden_size,)

    Returns:
        Next hidden state h_t, shape (hidden_size,).
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.