LSTM Cell Forward Pass
Implement lstm_cell(x, h_prev, c_prev, W, b).
xhas shape(D,);h_prevandc_prevhave shape(H,).- Return a
(2, H)array: row 0 is ht, row 1 is ct.
Sanity check: with all weights zero and all biases zero, and , so ct=0.5ct−1 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 vectorh_prev: (H,) previous hidden statec_prev: (H,) previous cell state: (4H, D+H) stacked gate weights, ordered i, f, g, o
b: (4H,) stacked gate biases, same orderReturn (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
8 employers weight this skill
3 quant funds, 3 frontier labs, 2 AI product companies. Top match scores 82.
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