DEEP LEARNING LESSON 5 LOSS FUNCTIONS

Mean Squared Error

Mean Squared Error, commonly called MSE, is a loss function that measures the average squared difference between the model's predictions and the actual values.

In simple words

MSE looks at how far each prediction is from the correct answer, squares each error, and then calculates the average.

What Does MSE Mean?

MSE stands for:

Mean Squared Error

Each word tells us something about the calculation.

Mean
  ↓
Average

Squared
  ↓
Square the error

Error
  ↓
Difference between prediction and actual value

So the basic idea is:

Prediction
     ↓
Find Error
     ↓
Square Error
     ↓
Repeat for all values
     ↓
Calculate Average
     ↓
MSE

When Is MSE Used?

MSE is commonly used for regression problems, where the model predicts numerical values.

Examples include:

House Price Prediction
Temperature Prediction
Sales Prediction
Revenue Prediction
Stock Price Prediction

For example, a neural network might predict:

House Price = $300,000

Here, the target is a continuous numerical value rather than a class such as "cat" or "dog".

MSE Formula

The formula for Mean Squared Error is:

MSE = (1 / n) × Σ(actual - prediction)²

You do not need to memorize the formula immediately. Understand what each part means first.

n
↓
Number of values

actual
↓
Correct answer

prediction
↓
Model's prediction

(actual - prediction)
↓
Prediction error

²
↓
Square the error

1 / n
↓
Calculate the average

Simple Example

Suppose our model predicts house prices for three houses.

Actual Prices:

100
200
300

The model predicts:

Predictions:

110
190
280

Now we calculate the error for each prediction.

House 1:
Actual = 100
Prediction = 110

Error = 100 - 110
      = -10


House 2:
Actual = 200
Prediction = 190

Error = 200 - 190
      = 10


House 3:
Actual = 300
Prediction = 280

Error = 300 - 280
      = 20

Step 1 — Calculate the Squared Errors

MSE does not simply average the raw errors. It squares each error first.

Error 1 = -10
(-10)² = 100

Error 2 = 10
(10)² = 100

Error 3 = 20
(20)² = 400

Now we have:

Squared Errors:

100
100
400

Why Do We Square the Errors?

There are two important reasons.

1. Negative and positive errors should not cancel

Look at our original errors:

-10
+10
+20

If we simply added them:

-10 + 10 + 20 = 20

The negative and positive errors partly cancel each other.

Squaring removes the sign:

(-10)² = 100
(+10)² = 100

Both errors now contribute positively to the loss.

2. Large errors become much more noticeable

Compare these errors:

Error = 2
Squared Error = 4


Error = 10
Squared Error = 100

A much larger error receives a disproportionately larger contribution to MSE.

Step 2 — Calculate the Mean

We now have the squared errors:

100
100
400

Add them:

100 + 100 + 400 = 600

There are three predictions, so divide by 3:

MSE = 600 / 3

MSE = 200

Therefore:

MSE = 200

Complete Calculation in One View

Actual:
[100, 200, 300]

Prediction:
[110, 190, 280]


Step 1: Calculate errors

[-10, 10, 20]


Step 2: Square errors

[100, 100, 400]


Step 3: Add squared errors

100 + 100 + 400 = 600


Step 4: Calculate mean

600 / 3 = 200


MSE = 200

Another Simple Example

Suppose the actual values are:

Actual = [10, 20]

And the model predicts:

Prediction = [12, 18]

Calculate the errors:

10 - 12 = -2
20 - 18 = 2

Square them:

(-2)² = 4
(2)² = 4

Calculate the mean:

MSE = (4 + 4) / 2

MSE = 8 / 2

MSE = 4

What If the Prediction Is Perfect?

Suppose the actual values are:

Actual = [10, 20, 30]

And the predictions are exactly the same:

Prediction = [10, 20, 30]

Every error is zero:

Errors = [0, 0, 0]

Therefore:

MSE = (0² + 0² + 0²) / 3

MSE = 0

So a perfect prediction produces an MSE of zero.

What Does a Smaller MSE Mean?

When comparing models on the same dataset using the same MSE definition, a smaller MSE generally means the predictions are closer to the actual values.

Model A
MSE = 25


Model B
MSE = 100

In this comparison, Model A has the smaller average squared error.

Therefore, Model A performed better according to MSE on this dataset.

Why MSE Is Sensitive to Large Errors

Because the error is squared, large mistakes can have a very large effect on the final loss.

Error = 2
Squared Error = 4


Error = 5
Squared Error = 25


Error = 20
Squared Error = 400

Notice how quickly the squared error grows.

This makes MSE useful when large prediction errors should be penalized strongly.

MSE Is a Loss Function

MSE is one specific type of loss function.

Loss Function
      │
      ├── Mean Squared Error
      ├── Binary Cross-Entropy
      └── Categorical Cross-Entropy
           ...

So when a neural network uses MSE, the training process uses the MSE value to measure the model's prediction error.

Calculate MSE With Python

We can calculate MSE manually using Python.

actual = [100, 200, 300]
predicted = [110, 190, 280]

squared_errors = []

for actual_value, predicted_value in zip(actual, predicted):

    error = actual_value - predicted_value

    squared_error = error ** 2

    squared_errors.append(squared_error)


mse = sum(squared_errors) / len(squared_errors)

print("MSE:", mse)

The result is:

MSE: 200.0

Understand the Python Code

1. Store the actual values

actual = [100, 200, 300]

These are the correct target values.

2. Store predictions

predicted = [110, 190, 280]

These are the values produced by the model.

3. Create an empty list

squared_errors = []

We will store the squared error for each prediction in this list.

4. Loop through the values

for actual_value, predicted_value in zip(
    actual,
    predicted
):

zip() pairs each actual value with its corresponding prediction.

100 → 110
200 → 190
300 → 280

5. Calculate the error

error = actual_value - predicted_value

This calculates the difference between the actual value and the prediction.

6. Square the error

squared_error = error ** 2

The ** 2 means "raise to the power of 2".

7. Store the result

squared_errors.append(squared_error)

The squared error is added to the list.

8. Calculate the mean

mse = sum(squared_errors) / len(squared_errors)

sum() adds all squared errors, while len() tells us how many errors there are.

Dividing the total by the number of values gives us the mean.

A Shorter Python Version

Once you understand the manual version, the same calculation can be written more compactly:

actual = [100, 200, 300]
predicted = [110, 190, 280]

mse = sum(
    (a - p) ** 2
    for a, p in zip(actual, predicted)
) / len(actual)

print(mse)

The calculation is exactly the same. The shorter version simply combines the steps.

MSE in Deep Learning Frameworks

In real neural-network projects, we usually do not calculate MSE manually.

For example, with Keras:

model.compile(
    optimizer="adam",
    loss="mse"
)

Here:

loss="mse"

tells the model to use Mean Squared Error as its loss function.

The framework handles the underlying calculations during training.

Important

MSE is not automatically the best loss function for every problem.

It is commonly useful for regression, but classification problems such as spam detection usually use classification-specific losses such as Binary Cross-Entropy.

MSE in One Picture

Actual Values
[100, 200, 300]
       +
Predictions
[110, 190, 280]
       ↓
Calculate Errors
[-10, 10, 20]
       ↓
Square Errors
[100, 100, 400]
       ↓
Average
600 / 3
       ↓
MSE = 200

The Key Idea

MSE takes every prediction error, squares it, and then calculates the average.

1. Find Error
       ↓
2. Square Error
       ↓
3. Average Squared Errors
       ↓
4. MSE
QUICK CHECK

Check Your Understanding

What does MSE stand for?
Mean Squared Error.

What does MSE calculate?
The average of the squared differences between actual values and predictions.

Why do we square the errors?
To prevent positive and negative errors from cancelling each other and to penalize larger errors more strongly.

What is MSE when every prediction is perfect?
Zero.

What type of problem commonly uses MSE?
Regression problems where the model predicts numerical values.

Does a smaller MSE generally mean better predictions?
Yes, when comparing models using the same MSE on the same or comparable data.

NEXT TOPIC

Binary Cross-Entropy

Next, we will learn how Binary Cross-Entropy measures error for binary classification problems such as spam detection and pass/fail prediction.