Regression losses: MSE, MAE and Huber
A model that predicts a number (regression) measures "how wrong was I" as one number and learns by making that number smaller. How you measure wrongness changes the answer the model finds. Drag one outlier and watch the answers of the three losses split apart.
Short answer. MSE (the average squared error) finds the mean, and one outlier can drag it far. MAE (the average error size) finds the median, so outliers barely move it, but it has a kink at 0 and its slope always has the same size, which makes training rough. Huber uses the square when the error is smaller than δ and a straight line when it is bigger, so it gets the good parts of both: ½e² if |e| ≤ δ, otherwise δ·(|e| − δ÷2). Start with MSE. If outliers are mixed in, use Huber or MAE.
Drag the orange point (data point 3) up or down, or type in the number box, to move the outlier (0 to 30). The three horizontal lines are the answers the three losses pick when the model can only output one number (a constant): solid green MSE, long blue dashes MAE, short purple dashes Huber. The horizontal axis is only the point number and plays no part in the answer. The data is five made-up numbers (3, 7, 15, 1, 4). The lines below say how far off the outlier is from each answer and how hard it pulls on it (the slope of the loss, section 3).
Try this. ① At the start, MSE gives 6, MAE gives 4 and Huber gives 4.5. ② Drag the outlier up to 30. The MSE answer follows it up to 9, but MAE stays at 4 and Huber stays at 4.5. The outlier pulls with force 21 on MSE (its whole error), 1 on MAE and 2 (δ) on Huber. ③ Bring the outlier down to 5 and all three become 4. Without an outlier the three answers are close. ④ Put the outlier back at 15 and change δ. At 0.5 Huber gives 4 (the median), at 5 it gives 5, at 8 it gives 5.75, and at 9 or more it gives 6 (the mean). δ is the handle that slides Huber from the MAE side to the MSE side.
Definitions
The text below explains with pictures and experiments. The textbook definitions are as follows. There are n data points, yᵢ is the i-th answer, and ŷᵢ is its prediction.
- error, loss
- The error eᵢ is the answer minus the prediction. The loss is one number made from all the errors, and training means finding the prediction (the model's parameters) that makes the loss smallest.
- mean squared error (MSE)
- The mean of the squared errors. The loss of one point is sometimes written ½e². That multiplies every point by the same number, so the prediction that makes it smallest stays the same. If one constant c must fit all the data, the c that makes MSE smallest is the mean of the yᵢ.
- mean absolute error (MAE)
- The mean of the absolute errors. If one constant c must fit all the data, the c that makes MAE smallest is the median of the yᵢ (when n is even, any value between the two middle values).
- Huber loss
- A loss for one point with a positive boundary δ: ½e² when the size of the error is δ or less, and a straight line with slope δ when it is larger. The two pieces join at |e| = δ with both the value and the slope matching, and the loss is averaged over the points. Its gradient with respect to e is e clipped to the range −δ to δ.
- outlier
- A value far away from the rest of the data. The MSE answer (the mean) moves with an outlier, while the MAE answer (the median) and the Huber answer move less when the outlier goes farther away.
1. What a loss is: wrongness as one number
Regression means predicting a number (a house price, a delivery time, tomorrow's temperature). Each example gives an error e = true value − prediction. The loss squeezes these errors into one number, and training searches for the prediction that makes the loss as small as possible (gradient descent). There are three common ways to squeeze them.
| Name | Loss for one point | Combine |
|---|---|---|
| MSE | e² | (e₁² + … + eₙ²) ÷ n |
| MAE | |e| | (|e₁| + … + |eₙ|) ÷ n |
| Huber | ½e² if |e| ≤ δ, otherwise δ·(|e| − δ÷2) | average over the points |
Finding the straight line with the smallest MSE is least squares. To focus only on how the shape of the loss changes the answer, this page uses the smallest possible regression: the model outputs a single number (a constant c). With a line or a neural network, the same thing happens point by point.
MSE is often written as ½e². The ½ makes the slope come out as a clean e. It multiplies every point by the same number, so the lowest point (the answer) does not move. The "pull" on this page is the slope with this ½ included.
2. How the formula is built: the Huber loss
In the picture below, the horizontal axis is the error e. The top panel is the loss and the bottom panel is the slope of the loss. The slope is how hard that point pulls on the answer (section 3). δ is the same value as in the lab above and starts at 2.
Each press of "Next" adds one piece to the formula (the orange background marks the new piece), changes the picture by that much, and explains why below. Change δ in the lab above and this picture changes too. If δ is bigger than 6, the seam is outside the picture.
- Square the error:
½e². Near 0 it is flat and smooth, so the slope shrinks as you get close to the answer and the steps get smaller on their own. But the slope is the error itself (e), so a point that is off by 10 pulls 10 times harder than a point that is off by 1. - Big errors get a straight line:
δ·|e| when |e| > δ. A straight line has a fixed slope, so no point, however far, pulls harder than δ. The slope is δ because that is the parabola's slope at the seame = δ. The slope joins up, but the values do not. At the seam the parabola is½δ²and the line isδ², so the line floatsδ²÷2too high. - Match the seam:
δ·|e| − δ²÷2 = δ·(|e| − δ÷2). Lower the line by δ²÷2 and the values meet at the seam too. Both the value and the slope join up, so there is no jump and no kink. - Done:
Huber(e) = ½e² (|e| ≤ δ), δ·(|e| − δ÷2) (|e| > δ). Inside it is MSE, outside it is a straight line with slope δ. The pull is the error cut off at −δ and δ.
The final formula is the one written in the PyTorch HuberLoss docs. The loss comes from Huber's paper on finding the center of data that has outliers mixed in (sources).
Try this. ① At the start (δ = 2), the slope in the bottom panel is a straight diagonal line, so it keeps growing as the error grows. ② Press "Next" and the outside turns into orange straight lines. The slope in the bottom panel stops at ±2 and joins up, but at the seam in the top panel the line (4) floats 2 above the parabola (2). ③ Press once more and the line drops by 2 and locks in at the seam (value 2, slope 2). ④ Set δ to 1 in the lab above and go to step ②: the gap becomes 0.5 (1² ÷ 2).
3. MSE finds the mean, MAE finds the median
The lowest point of the loss is where its slope is 0. Think of every point pulling the answer toward itself. Then the lowest point is where the pulls cancel out and add up to 0. The pull of a point is the slope of its loss.
- MSE (½e²): pull = the error e. If
(y₁ − c) + … + (yₙ − c) = 0thenc = (y₁ + … + yₙ) ÷ n, which is the mean. The farther a point is, the harder it pulls. - MAE (|e|): pull = the sign (+1 above, −1 below). The pulls add up to 0 only when there are as many points above as below, which is the median. Near or far, the pull is 1, so distance does not count.
- Huber: pull = the error cut off at −δ and δ. Close points pull by their distance, far points pull only by δ. A small δ moves the answer toward the median, a big δ toward the mean.
Work it out by hand: the data 3, 7, 15, 1, 4
- MSE. The mean is
(3 + 7 + 15 + 1 + 4) ÷ 5 = 30 ÷ 5 = 6. - MAE. In order: 1, 3, 4, 7, 15. The middle (third) one is 4.
- Huber (δ = 2). Suppose c is between 4 and 5. Then only 3 and 4 are within δ. 7 and 15 are cut to +2, and 1 is cut to −2. The pulls add up to
(3 − c) + (4 − c) + 2 + 2 − 2 = 9 − 2c, which is 0 atc = 4.5. - Move the outlier from 15 to 30. The mean becomes
45 ÷ 5 = 9, up by 3. The median is the middle of 1, 3, 4, 7, 30, still 4. Huber also stays at 4.5, because 15 and 30 are both outside δ and both pull with exactly 2.
| y | MSE pull: y − 6 | MAE pull: sign of y − 4 | Huber pull: y − 4.5 cut at ±2 |
|---|---|---|---|
| 3 | −3 | −1 | −1.5 |
| 7 | 1 | 1 | 2 (2.5 cut) |
| 15 | 9 | 1 | 2 (10.5 cut) |
| 1 | −5 | −1 | −2 (−3.5 cut) |
| 4 | −2 | 0 | −0.5 |
| Sum | 0 | 0 | 0 |
With MSE, the single outlier 15 pulls up by +9, and all four other points have to pull down together (−3 + 1 − 5 − 2 = −9) to balance it. That is why the answer sits 2 above the median.
4. Key properties
| Property | MSE | MAE | Huber |
|---|---|---|---|
| Fit one number | the mean | the median | toward the median if δ is small, toward the mean if δ is big |
| Pull (slope) | grows with the error, no limit | always the same size (only the sign) | grows with the error, then stops at δ |
| Outlier moves farther away | the answer follows (1÷n for every 1) | the answer stays | the answer stays while the outlier is outside δ |
| Near 0 | smooth | kinked | smooth |
| What it assumes about errors | the most likely answer if errors follow a normal distribution | the most likely answer if errors follow a Laplace distribution | a shape that is normal in the middle and Laplace-like in the tails |
How they train. The MSE slope shrinks near the answer, so the steps shrink on their own. But a point with a big error makes the slope jump. The MAE slope always has the same size, so with a fixed learning rate it keeps hopping back and forth around the answer and never settles. You have to lower the learning rate over time (gradient descent). Huber moves like MSE for small errors and like MAE for big ones. Also, with an even number of points every value between the two middle values is a valid MAE answer, so the answer is not unique.
5. Choosing δ
δ is the border between "normal error" and "treat it as an outlier". It is in the same unit as the error (if house prices are in dollars, δ is in dollars).
- A very small δ puts almost every point outside, so the loss is close to δ times MAE and the answer moves toward the median. In the lab, δ = 0.5 gives 4.
- A very big δ puts every point inside, so the loss is ½e², half of MSE, and the answer is the mean. In the lab, any δ of 9 or more gives exactly 6, because the point farthest from the mean 6 (the 15) is off by 9.
- Set it around the size of a normal error. If you standardize the target first, you can read δ as "this many standard deviations". PyTorch's HuberLoss uses δ = 1 by default. The right value depends on the data, so try a few and compare them on validation data.
6. When to use which
| Option | Good | Bad | Use it when |
|---|---|---|---|
| MSE | Smooth, and the slope shrinks near the answer, so training is stable. Fits the mean. For a straight line it is solved in one formula (least squares) | One outlier drags it. Squaring makes big errors bigger, so the slope can jump | Errors are evenly small and outliers are rare. Big mistakes are especially bad. The default |
| MAE | Barely moved by outliers. Fits the median. Same unit as the data, so it reads as "off by this much on average" | Kinked at 0. The slope always has the same size, so the learning rate must shrink. The answer is sometimes not unique | The outliers are real data you cannot drop. You want the median. As a report metric |
| Huber | Smooth like MSE for small errors, and the pull of big errors stops at δ | You must pick δ. δ is in the error's unit, so it has to be picked again when the data's scale changes | Mostly clean data with a few very wrong values mixed in. You want to stop the slope from suddenly jumping |
If the outlier is a measurement mistake, fix it or drop it before you change the loss. The loss is the tool for deciding how much to trust values that are real but rare.
Where it is used
- Everyday regression. For numbers whose errors are evenly spread, like house prices or demand, start with MSE. The same goes when the last layer of a neural network outputs one number.
- Data that is sometimes very wrong. If a sensor sometimes gives a nonsense value, or a delivery is sometimes very late, Huber or MAE weakens the pull of those outliers.
- Fitting object boxes. The PyTorch docs point to the Fast R-CNN paper as an example where Smooth L1 (almost the same as Huber) keeps the slope from suddenly blowing up.
- Report metrics. Report MAE to say "off by this much on average" and RMSE when big errors matter more. The loss you train with and the metric you report can be different.
Common questions
What is the difference between MSE and RMSE?
RMSE is the square root of MSE. Taking the square root does not move the lowest point, so training finds the same answer. RMSE is in the same unit as the data (dollars stay dollars), so it is easier to read in a report. It has the same unit as MAE but puts more weight on big errors.
MAE has a kink at 0. How can gradient descent use it?
At that one kink you just pick a slope somewhere between −1 and 1 and carry on. An error of exactly 0 is rare, so this is not the real problem. The real problem is that the slope always has size 1, so the steps do not shrink near the answer. You lower the learning rate over time, or use Huber.
Are Huber and Smooth L1 the same thing?
Almost. According to the PyTorch docs, Smooth L1 is Huber divided by δ (Smooth L1 calls δ beta), so with δ = 1 they are the same. The straight part has slope δ in Huber and always slope 1 in Smooth L1.
Words
| Plain words used here | Textbook term |
|---|---|
| wrongness as one number | loss function, cost function |
| error e = true value − prediction | residual, error |
| average squared error | mean squared error (MSE), L2 loss |
| average error size | mean absolute error (MAE), L1 loss |
| a loss that joins a square and a line | Huber loss. Almost the same: Smooth L1 |
| the seam δ | threshold, delta. Called beta in PyTorch's Smooth L1 |
| how hard a point pulls | gradient, derivative of the loss. In statistics, the psi function (ψ) |
| the value used as the slope at a kink | subgradient |
| barely moved by outliers | robust |
| a value far from the rest | outlier |
| the middle value | median |
| the line with the smallest squared error | least squares |
| the square root of MSE | RMSE (root mean squared error) |
Sources
- Peter J. Huber, Robust Estimation of a Location Parameter, The Annals of Mathematical Statistics. The paper that introduced the Huber loss as a way to find the center of data with outliers mixed in.
- PyTorch docs: HuberLoss (the formula, default δ = 1, relation to Smooth L1), SmoothL1Loss (Huber ÷ beta, line slope 1, the Fast R-CNN example).
- PyTorch docs: MSELoss, L1Loss.