Problems
Loading...
1 / 1

Build a Mini GRU Cell (Forward Pass)

NLPNeural Networks
Medium

Implement a single-step GRU cell forward pass with the following equations:

Update Gate:

zt=σ(xtWz+ht1Uz+bz)z_t = \sigma(x_t W_z + h_{t-1} U_z + b_z)

Reset Gate:

rt=σ(xtWr+ht1Ur+br)r_t = \sigma(x_t W_r + h_{t-1} U_r + b_r)

Candidate Hidden State:

h~t=tanh(xtWh+(rtht1)Uh+bh)\tilde{h}_t = \tanh(x_t W_h + (r_t \odot h_{t-1}) U_h + b_h)

New Hidden State (Final Output):

ht=(1zt)ht1+zth~th_t = (1 - z_t) \odot h_{t-1} + z_t \odot \tilde{h}_t

Function Arguments

  • x: input vector/batch with shape (D,) or (N, D)
  • h_prev: previous hidden state with shape (H,) or (N, H)
  • params: dict containing weight matrices and biases:
params = {
    "Wz": (D, H), "Uz": (H, H), "bz": (H,),  # Update gate
    "Wr": (D, H), "Ur": (H, H), "br": (H,),  # Reset gate
    "Wh": (D, H), "Uh": (H, H), "bh": (H,)   # Candidate
}

Parameter Shapes

W:(D,H) - input-to-hidden weightsU:(H,H) - hidden-to-hidden weightsb:(H,) - bias vectors \begin{aligned} W^* &: (D, H) \text{ - input-to-hidden weights} \\ U^* &: (H, H) \text{ - hidden-to-hidden weights} \\ b^* &: (H, ) \text{ - bias vectors} \end{aligned}

Helper Functions Provided

  • _sigmoid(x): numerically stable sigmoid activation
  • _as2d(a, feat): reshapes 1D array to (1, feat). Returns (array_2d, was_1d_bool) to track if conversion happened
Loading visualization...

Examples

Input: x = np.zeros((2,3)), h_prev = [[1.0,-1.0],[2.0,0.0]], all params = 0

Output: [[0.5, -0.5], [1.0, 0.0]]

Input: x = [0.5,-1.0,0.0,0.25,0.75], h_prev = [0.0,0.1,-0.1,0.2]

Output: [-0.1115, 0.0543, -0.2421, 0.0817]

Hint 1

Use _as2d() to handle both 1D and 2D inputs. Convert x to (N, D) and h_prev to (N, H) before any matrix ops, then squeeze back at the end if the input was 1D.

Hint 2

Compute gates in order: z_t = sigmoid(x @ W_z + h_prev @ U_z + b_z), r_t = sigmoid(x @ W_r + h_prev @ U_r + b_r), then candidate = tanh(x @ W_h + (r_t * h_prev) @ U_h + b_h). Final output is (1 - z_t) * h_prev + z_t * candidate.

Requirements

  • NumPy only. Vectorized; no Python loops over batch/features
  • Support both no-batch (1D) and batch (2D) inputs
  • Do not modify inputs in-place; return a new array

Constraints

  • D, H ≤ 256; batch N ≤ 64
  • Time ≤ 300 ms; Memory ≤ 128 MB
Try Similar Problems