Loss function - Mean Squared Error (Mean Squared Error, MSE)

Mean Squared Error (Mean Squared Error, MSE): MSE is a commonly used loss function in regression tasks, which measures the average squared error between the model's predicted value and the actual value.

Specifically, the calculation formula of MSE is as follows:

MSE = \frac{1}{n} \sum \left ( x_{i}-y_{i}\right )^{2}

Among them, n is the number of samples, xi is the true value of the i-th sample, and yi is the predicted value of the model for the i-th sample.

The smaller the value of MSE, the smaller the difference between the predicted value of the model and the real value, and the better the performance of the model. MSE can be regarded as the average value of the square of the error of the model to the predicted value, so it is more sensitive to outliers. If there are outliers in the sample, MSE may be affected by them and lead to model performance degradation.

MSE is widely used in tasks such as linear regression and multiple linear regression. In deep learning, MSE is also used to measure the performance of neural networks in regression tasks and optimized as a loss function. When using MSE as a loss function for optimization, optimization algorithms such as gradient descent are usually used to minimize the value of MSE, thereby improving the performance of the model.

In PyTorch, the mean squared error can be calculated using the built-in MSE loss function. The following is a sample code to implement the MSE loss function using PyTorch:

import torch
import torch.nn as nn

# 创建真实值和预测值的张量
y_true = torch.tensor([1, 2, 3, 4, 5], dtype=torch.float32)
y_pred = torch.tensor([1.5, 2.5, 2.8, 3.8, 4.5], dtype=torch.float32)

# 计算MSE损失函数
criterion = nn.MSELoss()
mse_loss = criterion(y_pred, y_true)

print("MSE损失值:", mse_loss.item())

In the code above, tensors of true and predicted values ​​are first created, and then nn.MSELoss()an instance of the MSE loss function is created using PyTorch's built-in functions. Finally, call forward()the method of the instance, pass in the predicted value and the real value tensor, and the MSE loss value can be calculated.

If you need to use MSE as the loss function for optimization when training the model, you can calculate the loss in the training loop and use the backpropagation algorithm to update the model parameters. The following is a sample code for model training using MSE as the loss function:

import torch
import torch.nn as nn
import torch.optim as optim

# 定义模型
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.linear = nn.Linear(1, 1)

    def forward(self, x):
        y_pred = self.linear(x)
        return y_pred

# 创建模型和优化器
model = MyModel()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# 创建真实值和输入张量
x = torch.tensor([1, 2, 3, 4, 5], dtype=torch.float32).unsqueeze(1)
y_true = torch.tensor([2, 4, 6, 8, 10], dtype=torch.float32).unsqueeze(1)

# 训练模型
num_epochs = 100
criterion = nn.MSELoss()

for epoch in range(num_epochs):
    # 前向传播
    y_pred = model(x)
    # 计算损失
    loss = criterion(y_pred, y_true)
    # 反向传播
    optimizer.zero_grad()
    loss.backward()
    # 更新参数
    optimizer.step()

    print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}")

In the above code, a simple linear model and a Stochastic Gradient Descent (SGD) optimizer are first defined. The ground truth and input tensors are then created and the model is trained using the MSE loss function. In each training iteration, the forward propagation is performed first, then the loss is calculated, and the model parameters are updated using the backpropagation algorithm.

Guess you like

Origin blog.csdn.net/weixin_50752408/article/details/129557320