Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

270. LSTM gates forward

medium
GeneralGeneral
staff

Implement the forward pass of the gates in a single LSTM cell, which controls how information is forgotten, added, and exposed at each timestep in a sequence model. Given an input vector and the previous hidden/cell states, you’ll compute the new hidden and cell states for one step.

ft=σ(Wfxt+Ufht1+bf)it=σ(Wixt+Uiht1+bi)c~t=tanh(Wcxt+Ucht1+bc)ct=ftct1+itc~tot=σ(Woxt+Uoht1+bo)ht=ottanh(ct)\begin{aligned} f_t &= \sigma(W_f x_t + U_f h_{t-1} + b_f) \\ i_t &= \sigma(W_i x_t + U_i h_{t-1} + b_i) \\ \tilde{c}_t &= \tanh(W_c x_t + U_c h_{t-1} + b_c) \\ c_t &= f_t \odot c_{t-1} + i_t \odot \tilde{c}_t \\ o_t &= \sigma(W_o x_t + U_o h_{t-1} + b_o) \\ h_t &= o_t \odot \tanh(c_t) \end{aligned}

Requirements

Implement the function:

python

Rules:

  • Compute the 4 gates f_t, i_t, o_t and candidate state c_tilde using the formulas above.
  • Use sigmoid for gates and tanh for candidate/hidden activation.
  • Use elementwise multiplication for \odot.
  • Use only NumPy and Python built-in libraries (no deep learning frameworks).
  • Return h_t and c_t as NumPy arrays.

Example

python

Output:

python
Input Signature
ArgumentType
x_tnp.ndarray
c_prevnp.ndarray
h_prevnp.ndarray
paramsdict
Output Signature
Return NameType
valuetuple

Constraints

  • Use NumPy only; no deep learning frameworks.

  • Ensure shapes: W(hid,in), U(hid,hid), b(hid).

  • Return NumPy arrays.

Hint 1

Ensure inputs are NumPy arrays for @ matrix multiplication.

Hint 2

Implement sigmoid(z)=1/(1+exp(-z)); use np.tanh for candidate/hidden.

Hint 3

Compute gates: f,i,o = sigmoid(W@x + U@h + b); then c=f*c_prev+i*c_tilde, h=o*tanh(c); return NumPy arrays.

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
staff
senior
entry
Tags
LSTM
forward-pass
matrix-vector-multiplication
activation-functions
25 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit