LLM Basics
Lesson 4 of 6

A Loss Function for Classification

Suppose the MLP's single output is meant to answer a yes/no question: positive means one class, negative means the other. To train it, you need a loss function — something that scores how wrong a prediction is, in a way that's differentiable, so gradient descent (from the derivatives lesson, all the way back in Zero to GPT) has something to work with.

A simple, effective choice for this is hinge loss. For a true label y of +1 or −1 and the network's raw score:

loss = max(0, 1 − y·score)

Read it as: if the score is confidently on the correct side (large and same sign as y), the loss is exactly 0 — no penalty, nothing more to learn from this example. If it's on the wrong side, or right-but-not-confident, the loss is positive and grows the more wrong it is.

Hinge loss: max(0, 1 − label × score)

[0.8, 0.9]label +1score 0.70loss 0.30
[-0.9, -0.8]label +1score 0.30loss 0.70
[0.9, -0.8]label -1score -0.60loss 0.40
[-0.8, 0.9]label -1score 0.20loss 1.20 ⚠ wrong side
[1.1, 0.7]label +1score -0.10loss 1.10 ⚠ wrong side
[-1.1, -1.3]label +1score 0.85loss 0.15

Total loss: 3.85

Notice the two flagged rows: a wrong-side prediction and a right-side-but-unconfident one both get penalized, while confidently-correct predictions contribute nothing. Add up every point's loss and you get a single number summarizing "how bad is the network right now" — exactly what gradient descent needs to improve it.

Matching numbers, in Python:

Python

Your turn

Using the hinge_loss function above, implement total_loss(points): sum hinge_loss across every (x, label, score) triple in points.

Python

You now have every ingredient: a network that produces a score, and a loss that scores the score. All that's left is running gradient descent to actually make the loss go down — on real, two-dimensional data.

Under hinge loss, what happens to a prediction that's already confidently correct?