#160End-to-end binary classifier (ReLU hidden, sigmoid output)MediumNeural Networks
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.
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 , 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 , , and 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 ; ; → class 1.
Constraints
- Hidden layer uses ReLU; output uses sigmoid.
probis a plainfloat;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