💚
Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! 🚀
Loss and Error
Loss measures how wrong the network's predictions are. The goal of training is to reduce the loss as much as possible.
10 min•By Priygop Team•Updated 2026
What is Loss?
After the network makes a prediction, we compare it to the correct answer. The difference is the error. Loss is a mathematical formula that summarizes this error into a single number.
High loss: the network is making big mistakes
Low loss: the network is close to correct
The goal of training is to minimize (reduce) the loss.
Diagram
Loading diagram…
Deep Learning ⊂ Machine Learning ⊂ Artificial Intelligence
Common Loss Functions
- Mean Squared Error (MSE): used for regression tasks. Calculates the average squared difference between predictions and actual values
- Binary Cross-Entropy: used for yes/no classification (spam or not spam)
- Categorical Cross-Entropy: used for multi-class classification (cat, dog, or bird)
- The choice of loss function depends on what type of output the network produces
Loss Calculation Example
Loss Calculation Example
# Understanding loss with a simple example
# Example: predicting house prices (regression)
# Mean Squared Error (MSE)
actual_prices = [200000, 350000, 150000, 500000]
predicted_prices = [210000, 320000, 160000, 480000]
def mse_loss(actual, predicted):
"""Calculate Mean Squared Error loss."""
n = len(actual)
squared_errors = [(a - p) ** 2 for a, p in zip(actual, predicted)]
mse = sum(squared_errors) / n
return mse
loss = mse_loss(actual_prices, predicted_prices)
print("House Price Predictions:")
print()
for actual, predicted in zip(actual_prices, predicted_prices):
error = abs(actual - predicted)
print(f" Actual: ${actual:,} Predicted: ${predicted:,} Error: ${error:,}")
print()
print(f"MSE Loss: {loss:,.0f}")
print(f"Root MSE: ${loss**0.5:,.0f} (average error in dollars)")
print()
# Better predictions -> lower loss
better_predictions = [202000, 348000, 153000, 498000]
better_loss = mse_loss(actual_prices, better_predictions)
print(f"Better predictions MSE Loss: {better_loss:,.0f}")
print(f"Better Root MSE: ${better_loss**0.5:,.0f}")
print()
print("Lower loss = better predictions.")
print("Training minimizes this loss by adjusting weights.")Key Takeaways
- Loss measures how wrong the network's predictions are.
- Mean Squared Error (MSE): used for regression tasks. Calculates the average squared difference between predictions and actual values
- Binary Cross-Entropy: used for yes/no classification (spam or not spam)
- Categorical Cross-Entropy: used for multi-class classification (cat, dog, or bird)