S14 · Lesson 50 · Machine learning lab
Three models in NumPy: ridge regression in its primal and dual forms, logistic regression by Newton's method checked against an exact answer, and a two-layer network whose backpropagated gradients are verified by central differences
The formulas of the last thirty lessons become trustworthy once you've coded them and checked them against something independent. In this lab you'll write three small pieces that sit underneath real machine-learning libraries: ridge regression both ways, which shows the kernel trick's dual form is the same answer; logistic regression trained by Newton's method, tested against a case with an exact solution; and backpropagation for a small network, tested the way every framework's gradients are tested, against finite differences.
Predict
Your Newton's method for logistic regression, with no penalty (λ = 0), is run on data that a line separates perfectly. What happens?
With separable data the likelihood keeps rising as the weights grow along the separating direction (lesson 16), so there's no finite optimum. The predicted probabilities saturate at 0 and 1, the Hessian's weights y(1 − y) shrink toward zero, and each Newton step gets larger until the arithmetic overflows. Any λ > 0 gives a finite answer, which is why the function takes a penalty.
Name it
Task 1: ridge both ways
ridge solves , a D × D system. ridge_dual solves , an N × N system, and should be identical. The tests use 6 examples with 40 features, where the dual is the smaller problem.
Task 2: logistic regression by Newton's method
Start from w = 0 and repeat with and , where R is diagonal with entries . One test has an exact answer: with an intercept and a single binary feature, the fitted probability in each group equals that group's fraction of positives.
Task 3: backpropagation
For , and , the backward pass is , , and , with the biases' gradients as column sums of the δ's. The tests compare every entry with a central difference.
Worked example
What should task 1 give for the points (0, 1), (1, 3) and (2, 4), with a bias column and λ = 1 on both weights?
Show the solution
- Normal equations. and = (8, 11).
- Solve.
- Compare λ = 0. With λ = 0 it's lesson 1's least-squares line, intercept 1.167 and slope 1.5: the penalty pulled the slope down and the intercept with it.
Answer task 1 should give w = (1, 1.333), an intercept of 1 and a slope of 1.333.
Worked example
What should task 2 give when 2 of 8 examples are positive with the feature off and 6 of 8 with it on?
Show the solution
- Match the group rates. The fit must reproduce the group rates 0.25 and 0.75.
- Bias.
- Weight. = ln 3, giving The feature multiplies the odds by 9.
- Convergence. Newton's method from zero gets there to ten digits within about five iterations.
Answer task 2 should give = −1.099 and = 2.197 (ln 9).
Check
Step 1. Write ridge and ridge_dual: one answer from two linear systems.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write logistic_newton: logistic regression by iteratively reweighted least squares.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write forward_backward: a two-layer network and its gradients.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is comparing backpropagation against central differences such a reliable test?
The central difference estimates each derivative from the error function alone, with no knowledge of the network's structure, and its truncation error falls as the square of the step, so with a step of it's accurate to roughly relative, far better than the bugs it's meant to catch. A mistake in a backward pass, such as a missing activation derivative, a transposed matrix or a forgotten bias, typically changes some gradient entries by tens of percent. Checking every parameter on a small random network with random inputs exercises every code path, so agreement to one part in a million is strong evidence that the backward pass is right everywhere.
Practice
Keep practicing
3 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.