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.

ei=yi−y^i

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ᵢ.

MSE=1n∑i=1nei2

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).

MAE=1n∑i=1n|ei|

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 δ.

Lδ(e)=12e2(|e|≤δ)

Lδ(e)=δ(|e|−δ2)(|e|>δ)

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.

NameLoss for one pointCombine
MSEe²(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.

  1. 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.
  2. 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 seam e = δ. The slope joins up, but the values do not. At the seam the parabola is ½δ² and the line is δ², so the line floats δ²÷2 too high.
  3. 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.
  4. 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.

Work it out by hand: the data 3, 7, 15, 1, 4

  1. MSE. The mean is (3 + 7 + 15 + 1 + 4) ÷ 5 = 30 ÷ 5 = 6.
  2. MAE. In order: 1, 3, 4, 7, 15. The middle (third) one is 4.
  3. 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 at c = 4.5.
  4. 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.
yMSE pull: y − 6MAE pull: sign of y − 4Huber pull: y − 4.5 cut at ±2
3−3−1−1.5
7112 (2.5 cut)
15912 (10.5 cut)
1−5−1−2 (−3.5 cut)
4−20−0.5
Sum000

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

PropertyMSEMAEHuber
Fit one numberthe meanthe mediantoward the median if δ is small, toward the mean if δ is big
Pull (slope)grows with the error, no limitalways the same size (only the sign)grows with the error, then stops at δ
Outlier moves farther awaythe answer follows (1÷n for every 1)the answer staysthe answer stays while the outlier is outside δ
Near 0smoothkinkedsmooth
What it assumes about errorsthe most likely answer if errors follow a normal distributionthe most likely answer if errors follow a Laplace distributiona 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).

6. When to use which

OptionGoodBadUse it when
MSESmooth, 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 jumpErrors are evenly small and outliers are rare. Big mistakes are especially bad. The default
MAEBarely 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 uniqueThe outliers are real data you cannot drop. You want the median. As a report metric
HuberSmooth 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 changesMostly 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

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 hereTextbook term
wrongness as one numberloss function, cost function
error e = true value − predictionresidual, error
average squared errormean squared error (MSE), L2 loss
average error sizemean absolute error (MAE), L1 loss
a loss that joins a square and a lineHuber loss. Almost the same: Smooth L1
the seam δthreshold, delta. Called beta in PyTorch's Smooth L1
how hard a point pullsgradient, derivative of the loss. In statistics, the psi function (ψ)
the value used as the slope at a kinksubgradient
barely moved by outliersrobust
a value far from the restoutlier
the middle valuemedian
the line with the smallest squared errorleast squares
the square root of MSERMSE (root mean squared error)

Sources

← Label formatsClass imbalance →