LSTM Cell Forward Pass

~35 minimplementation

Implement lstm_cell(x, h_prev, c_prev, W, b).

  • x has shape (D,); h_prev and c_prev have shape (H,).
  • Return a (2, H) array: row 0 is , row 1 is .

Sanity check: with all weights zero and all biases zero, and , so and . If your gate ordering is wrong this test still passes. The later tests are the ones that catch it.

Examples

Zero weights and biases: all gates at 0.5, candidate 0

Input
lstm_cell([1, -2], [0.5], [2], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [0, 0, 0, 0])
Output
[[0.3808], [1]]

Forget gate saturated open, input gate saturated shut: c_t == c_prev

Input
lstm_cell([1], [0], [3], [[0, 0], [0, 0], [0, 0], [0, 0]], [-20, 20, 0, 20])
Output
[[0.99505], [3]]

Forget gate shut, input gate open: c_t is overwritten by the candidate

Input
lstm_cell([1], [0], [3], [[0, 0], [0, 0], [0, 0], [0, 0]], [20, -20, 1, 20])
Output
[[0.64201], [0.76159]]

Hints

Hint 1

applies elementwise, so negate the whole array and exponentiate it in one go.

Hint 2

Double check the order of the input and forget gate blocks.

Requirements

  • x: (D,) input vector

  • h_prev: (H,) previous hidden state

  • c_prev: (H,) previous cell state

  • : (4H, D+H) stacked gate weights, ordered i, f, g, o

  • b: (4H,) stacked gate biases, same order

  • Return (2, H) array where row 0 is h_t and row 1 is c_t.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~35 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 lstm_cell(x: np.ndarray, h_prev: np.ndarray, c_prev: np.ndarray,
              W: np.ndarray, b: np.ndarray) -> np.ndarray:
    """
    One LSTM timestep.

    Args:
        x:      (D,) input vector
        h_prev: (H,) previous hidden state
        c_prev: (H,) previous cell state
        W:      (4H, D+H) stacked gate weights, ordered i, f, g, o
        b:      (4H,) stacked gate biases, same order

    Returns:
        (2, H) array where row 0 is h_t and row 1 is c_t.
    """
    # 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.