#158From scratch — the batched forward passMediumNeural Networks
From scratch — the batched forward pass
Background
The notebook's forward pass runs a whole batch at once, with examples as rows:
Here X is (m, i), W1 is (i, h), b1 is (1, h) (broadcast over rows), W2 is (h, 1), b2 is (1, 1). The output A2 is (m, 1) — one probability per example.
Problem statement
Implement forward(X, W1, b1, W2, b2) returning the batch of output probabilities A2.
Input
X—(m, i)batch of inputs.W1(i, h),b1(1, h)— hidden layer.W2(h, 1),b2(1, 1)— output layer.
Output
Returns an np.ndarray of shape (m, 1): per-example .
Examples
Example 1 — all-zero weights
Input: X = [[0, 0], [1, 1]], W1 = zeros(2×4), b1 = zeros(1×4), W2 = zeros(4×1), b2 = zeros(1×1)
Output: [[0.5], [0.5]]
Explanation: every score is 0, so both layers output 0.5 for every row.
Constraints
- Use
X @ W1 + b1(matmul with the batch on the left), then sigmoid; repeat for layer 2. - Return shape
(m, 1). - Clip the sigmoid input to avoid
expoverflow (e.g.np.clip(z, -500, 500)).
Notes
- The batch form is just the single-example forward pass with
Xstacked as rows — the bias broadcasts down the batch automatically.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example - all-zero weights give 0.5 everywhere
- •Reference two-step computation on a 3-row batch
- •Sample 4-row grid batch with ones weights