How machines learn

Loss function (Cost function)

A loss function is the score that measures how wrong a model's predictions are; training adjusts the model to make that score as small as possible.

To improve, a model needs to know how badly it did. The loss function is that judgment, expressed as a single number. A perfect prediction gives a loss of zero. Wild guesses give a big loss. Every training step is an attempt to lower this number a little, across all the examples.

Different tasks use different loss functions. Predicting a number often uses the squared gap between the guess and the truth, so big misses are punished hard. Predicting categories uses a loss that punishes being confidently wrong more than being unsure. Choosing the loss is choosing what 'good' means.

The loss is only measured during training, and only on examples with known answers. It is not the same as accuracy. A model can lower its loss without changing how many answers are right, simply by becoming more confident on the ones it already gets correct.

Example

A model guessing that a house is worth 300,000 when it sold for 350,000 gets a loss based on that 50,000 gap, and a bigger gap would score far worse.

Topic: Deep learning fundamentals →