Join Our 5-Week ML/AI Engineer Interview Bootcamp πŸš€ led by ML Tech Leads at FAANGs

Back to Questions

217. GRU forward pass

medium
GeneralGeneral
staff

Implement the forward pass of a single-layer GRU to process a sequence of input vectors and produce the sequence of hidden states. You’ll compute the GRU gates and hidden updates step-by-step using the standard GRU equations.

A GRU update at time step (t) is defined as:

zt=Οƒ(Wzxt+Uzhtβˆ’1+bz)rt=Οƒ(Wrxt+Urhtβˆ’1+br)h~t=tanh⁑(Whxt+Uh(rtβŠ™htβˆ’1)+bh)ht=(1βˆ’zt)βŠ™htβˆ’1+ztβŠ™h~t\begin{aligned} z_t &= \sigma(W_z x_t + U_z h_{t-1} + b_z) \\ r_t &= \sigma(W_r x_t + U_r h_{t-1} + b_r) \\ \tilde{h}_t &= \tanh(W_h x_t + U_h (r_t \odot h_{t-1}) + b_h) \\ h_t &= (1 - z_t)\odot h_{t-1} + z_t \odot \tilde{h}_t \end{aligned}

Requirements

Implement the function

python

Rules:

  • Treat each x_t as a row from X and compute h_t sequentially from t=0 to T-1.
  • Implement sigmoid and tanh yourself using NumPy (no deep learning frameworks).
  • Use elementwise multiply for (\odot) and keep the math consistent with the formulas above.
  • Return the full set of hidden states (not just the last one) as a NumPy array.
  • Keep everything inside this single function (helper inner functions are fine).

Example

python

Output:

python
Input Signature
ArgumentType
Xnp.ndarray
h0np.ndarray
paramsdict
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • Use only NumPy; no deep learning frameworks.

  • Matrix shapes must match specified dimensions.

  • Return np.ndarray of T hidden states.

Hint 1

Convert X, h0, and all params entries to NumPy arrays first so you can use @ for matrix–vector products and * for elementwise multiply.

Hint 2

Inside a loop over timesteps, compute the gates in the exact order: z_t = sigmoid(Wz@x_t + Uz@h + bz) and r_t = sigmoid(Wr@x_t + Ur@h + br); watch shapes: x_t is (input_dim,), h is (hidden_dim,).

Hint 3

Compute h_tilde = tanh(Wh@x_t + Uh@(r_t*h) + bh), then update: h = (1-z_t)*h + z_t*h_tilde. Append a copy (e.g., .tolist()) each step to return all hidden states.

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
staff
senior
entry
Tags
GRU
RNN-forward-pass
NumPy-linear-algebra
sequence-modeling
46 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