#160End-to-end binary classifier (ReLU hidden, sigmoid output)Medium

End-to-end binary classifier (ReLU hidden, sigmoid output)

Background

The production-style binary classifier from the recap: a ReLU hidden layer feeds a single sigmoid output, and the prediction is thresholded at 0.5.

a1=ReLU(W1x+b1),a2=σ(W2a1+b2),y^=1[a2>0.5]\mathbf{a}_1 = \text{ReLU}(W_1\mathbf{x} + \mathbf{b}_1), \qquad a_2 = \sigma(W_2\mathbf{a}_1 + b_2), \qquad \hat{y} = \mathbb{1}[a_2 > 0.5]

with W1 shape (h, i), W2 shape (1, h), b2 length 1 (since o = 1).

Problem statement

Implement classify(x, W1, b1, W2, b2) returning (prob, label) — the class-1 probability and the predicted 0/1 label.

Input

  • x — input vector (i,).
  • W1 (h, i), b1 (h,) — hidden layer.
  • W2 (1, h), b2 (1,) — output layer.

Output

Returns (prob, label): prob is a float in (0,1)(0,1), label is int (1 if prob > 0.5 else 0).

Examples

Example 1 — zeros land at the toss-up

Input:  x = [3, -2], W1 = zeros(2×2), b1 = [0, 0], W2 = [[1, 1]], b2 = [0]
Output: (0.5, 0)

Explanation: ReLU(0) = 0, so z2=0z_2 = 0, σ(0)=0.5\sigma(0) = 0.5, and 0.5>0.50.5 > 0.5 is false → class 0.

Example 2 — a positive case

Input:  x = [1, 1], W1 = [[1, 1], [1, 1]], b1 = [0, 0], W2 = [[1, 1]], b2 = [0]
Output: (0.982..., 1)

Explanation: hidden =ReLU([2,2])=[2,2]=\text{ReLU}([2,2])=[2,2]; z2=4z_2 = 4; σ(4)0.982\sigma(4)\approx0.982 → class 1.

Constraints

  • Hidden layer uses ReLU; output uses sigmoid.
  • prob is a plain float; label = int(prob > 0.5).

Notes

  • This is the architecture the lesson recommends for real binary classifiers — the all-sigmoid teaching net with two standard swaps (ReLU hidden, and BCE loss when you train it).
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 land at the 0.5 toss-up
  • Reference positive case predicts class 1
  • Sample large negative bias predicts class 0