Build a Mini GRU Cell (Forward Pass)
Build a Mini GRU Cell (Forward Pass)
Implement a single-step GRU cell forward pass with the following equations:
Update Gate:
zt=σ(xtWz+ht−1Uz+bz)Reset Gate:
rt=σ(xtWr+ht−1Ur+br)Candidate Hidden State:
h~t=tanh(xtWh+(rt⊙ht−1)Uh+bh)New Hidden State (Final Output):
ht=(1−zt)⊙ht−1+zt⊙h~tFunction 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∗U∗b∗:(D,H) - input-to-hidden weights:(H,H) - hidden-to-hidden weights:(H,) - bias vectorsHelper 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
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
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: any
Build a Mini GRU Cell (Forward Pass)
Build a Mini GRU Cell (Forward Pass)
Implement a single-step GRU cell forward pass with the following equations:
Update Gate:
zt=σ(xtWz+ht−1Uz+bz)Reset Gate:
rt=σ(xtWr+ht−1Ur+br)Candidate Hidden State:
h~t=tanh(xtWh+(rt⊙ht−1)Uh+bh)New Hidden State (Final Output):
ht=(1−zt)⊙ht−1+zt⊙h~tFunction 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∗U∗b∗:(D,H) - input-to-hidden weights:(H,H) - hidden-to-hidden weights:(H,) - bias vectorsHelper 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
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
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: any