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~tx: 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
}
_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 happenedInput: 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]
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.
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.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: any
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~tx: 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
}
_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 happenedInput: 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]
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.
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.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: any