#158From scratch — the batched forward passMedium

From scratch — the batched forward pass

Background

The notebook's forward pass runs a whole batch at once, with examples as rows:

Z1=XW1+b1,    A1=σ(Z1),Z2=A1W2+b2,    A2=σ(Z2)\mathbf{Z}_1 = X W_1 + \mathbf{b}_1,\;\; \mathbf{A}_1 = \sigma(\mathbf{Z}_1), \qquad \mathbf{Z}_2 = \mathbf{A}_1 W_2 + \mathbf{b}_2,\;\; \mathbf{A}_2 = \sigma(\mathbf{Z}_2)

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 P(class 1)\mathbb{P}(\text{class }1).

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 exp overflow (e.g. np.clip(z, -500, 500)).

Notes

  • The batch form is just the single-example forward pass with X stacked 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