MSE & MAE for Regression
Penalizing distance from the target.
MSE & MAE for Regression is a free Deep Learning Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Regression Needs Its Own Loss
When your model predicts a number like a price or temperature, you need a regression loss that measures how far each guess lands from the true value. 📏
Meet Mean Squared Error
MSE squares every prediction error, then averages them. Squaring keeps all errors positive and punishes big misses much harder than small ones.
MSE in One Line
PyTorch gives you nn.MSELoss, so you never write the formula by hand. You pass predictions and targets and get a single scalar back.
import torch.nn as nn
loss_fn = nn.MSELoss()
loss = loss_fn(pred, target)Why Squaring Matters
Because MSE squares errors, an error of 4 counts 16x more than an error of 1. That makes MSE very sensitive to large mistakes and outliers.
Meet Mean Absolute Error
MAE averages the absolute size of each error instead of squaring it. Every mistake counts in proportion to how big it actually is.
MAE in PyTorch
Use nn.L1Loss for MAE. The name comes from the L1 norm, which simply sums absolute differences before averaging.
import torch.nn as nn
loss_fn = nn.L1Loss()
loss = loss_fn(pred, target)MAE Shrugs Off Outliers
Since MAE never squares anything, one wild outlier does not blow up the loss. That makes it the calmer, more robust choice for messy data.
MSE vs MAE: The Tradeoff
Pick MSE when big errors are truly worse and your data is clean. Pick MAE when outliers are noise you do not want to chase.
What the Number Means
A regression loss is in your target's units (squared for MSE). Lower is better, but compare it across models, not as an absolute score.
Huber: The Best of Both
Want robustness and smooth gradients? Huber loss acts like MSE for small errors and like MAE for large ones, blending both behaviors.
import torch.nn as nn
loss_fn = nn.SmoothL1Loss()
loss = loss_fn(pred, target)Match Loss to Output Shape
For regression your final layer outputs raw numbers with no activation. The loss compares those values straight to the targets.
Quick Check
One outlier is dominating your training. Which loss is most robust to it?
Recap: Distance, Measured
You now read regression error two ways: MSE punishes big misses by squaring, while MAE stays steady against outliers. Match the loss to your data and you are set. 🎯
Frequently asked questions
Is the “MSE & MAE for Regression” lesson free?
Yes — the full text of “MSE & MAE for Regression” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “MSE & MAE for Regression”?
Penalizing distance from the target. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “MSE & MAE for Regression” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- MSE & MAE for Regression
- Binary Cross-Entropy with Logits
- Cross-Entropy for Multiclass
- Class Weights for Imbalanced Data