Lensa ML
Lensa ML

How Loss Functions Work

Step 1 of 6

Prediction vs Reality

Networks guess — loss measures how wrong they are

PREDICTED0.60ACTUAL0.85error: 0.25
Adjust the predictionpredicted = 0.60
ERROR
0.250
LOSS (SQUARED)
0.0625

A loss function measures how far the network's prediction is from the true value. Drag the slider to see how the error changes as the prediction moves closer to the actual value.

Step 1 of 6: Prediction vs Reality

Networks guess — loss measures how wrong they are

PREDICTED0.60ACTUAL0.85error: 0.25
Adjust the predictionpredicted = 0.60
ERROR
0.250
LOSS (SQUARED)
0.0625

A loss function measures how far the network's prediction is from the true value. Drag the slider to see how the error changes as the prediction moves closer to the actual value.

Step 2 of 6: Mean Squared Error

Squaring mistakes so big errors hurt more

xy0.10.40.40.51.00.41.3y = 1.30x + 0.80MSE = (1/n) Σ (yᵢ − ŷᵢ)²MSE= (0.1² + 0.4² + 0.4² + 0.5² + 1.0² + 0.4² + 1.3²) / 7= (0.0 + 0.2 + 0.2 + 0.3 + 1.0 + 0.2 + 1.7) / 7= 3.43 / 7= 0.490
Slope1.30
Intercept0.80

MSE squares each residual so that large errors are penalized much more than small ones. The pink squares show the "squared" error visually. Adjust the slope to minimize the total area.

Step 3 of 6: Binary Cross-Entropy

The loss function for yes-or-no decisions

predicted probability (p)loss-log(p)-log(1-p)loss = 0.357
Predicted probabilityp = 0.70

Binary cross-entropy uses -log(p) when the label is 1 and -log(1-p) when it's 0. A confident wrong prediction produces a huge loss that pushes the network to correct itself.

Step 4 of 6: Categorical Cross-Entropy

Extending to multiple classes

LOGITSSOFTMAXPROBABILITIESCat2.51.0 / 1.40.73673.6%← TRUEDog1.00.2 / 1.40.16416.4%Bird0.50.1 / 1.40.10010.0%Loss = −log(p_true)= −log(0.7361)= 0.3064
Cat logit2.5
Dog logit1.0
Bird logit0.5

Categorical cross-entropy only cares about the probability assigned to the true class. The loss is -log(p_true) -- it doesn't matter how the remaining probability is split among wrong classes.

Step 5 of 6: The Loss Landscape

A terrain of peaks and valleys

w1w2min
Loss:
<0.5
0.5–1.5
1.5–3
3–5
5–8
8–12
12–18
≥18
w13.00
w22.50
LOSS
13.13
GRADIENT
6.67

The loss landscape is a terrain where every point represents a set of weights, and the height is the loss. The yellow arrow shows the direction of steepest descent -- where gradient descent wants to go.

Step 6 of 6: Loss Guides Learning

Gradients flow from loss back through the network

1. Forward Pass
2. Compute Loss
3. Backward Pass
LInputHiddenOutputLossdata flows forward →

Forward pass: data flows through the network to produce a prediction. The loss measures the error, then gradients flow backward, telling each weight how to adjust. This cycle repeats thousands of times during training.