S1450 of 55
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

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?

Name it

Task 1: ridge both ways

ridge solves (XTX+λI)𝐰=XT𝐭, a D × D system. ridge_dual solves (XXT+λI)𝐚=𝐭, an N × N system, and 𝐰=XT𝐚 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 𝐰←𝐰−H−1𝐠 with 𝐠=XT(𝐲−𝐭)+λ𝐰 and H=XTRX+λI, where R is diagonal with entries yn(1−yn). 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 Z=tanh⁡(XW1+𝐛1), Y=ZW2+𝐛2 and E=12∑(Y−T)2, the backward pass is δY=Y−T, ∂E/∂W2=ZTδY, δA=(δYW2T)⊙(1−Z2) and ∂E/∂W1=XTδA, 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
  1. Normal equations. XTX+I=[4336] and XT𝐭 = (8, 11).
  2. Solve. 𝐰=(15/15,20/15)=(1,1.333)
  3. 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
  1. Match the group rates. The fit must reproduce the group rates 0.25 and 0.75.
  2. Bias. w0=ln⁡(0.25/0.75)=−1.099
  3. Weight. w0+w1 = ln 3, giving w1=ln⁡9=2.197 The feature multiplies the odds by 9.
  4. Convergence. Newton's method from zero gets there to ten digits within about five iterations.

Answer task 2 should give w0 = −1.099 and w1 = 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?

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.