损失函数
损失函数(Loss Function)是用于衡量模型预测值与真实值之间差距的函数。它是机器学习和深度学习中的关键组成部分,帮助算法通过最小化损失值来优化模型参数。
-
评估模型表现:通过计算目标值和输出之间的差距
损失函数通过量化模型预测与真实结果的偏差,为模型的好坏提供客观标准。如果损失值较大,说明模型表现不佳,预测结果与真实值之间存在较大差距;如果损失值较小,则表明模型的预测较为准确。
-
优化模型:反向传播
损失函数是训练过程中模型优化的依据。机器学习算法(如梯度下降)通过计算损失函数的梯度,调整模型参数,使得损失函数值最小化。最小化损失函数意味着模型能够更好地拟合数据,提高预测精度。
不同类型的任务(如回归问题、分类问题)需要使用不同的损失函数。损失函数的设计影响模型训练的效果和收敛速度。例如,回归问题通常使用均方误差(MSE),分类问题则常使用交叉熵损失。通过选择合适的损失函数,能够提高算法的表现和训练效率。
- 常见的损失函数
torch.nn.L1Loss()
torch.nn.L1Loss(size_average=None, reduce=None, reduction='mean')
函数描述:
函数使用:
Input: (∗), where ∗means any number of dimensions.(任意的维度均可参与运算)
Target: (∗), same shape as the input.
import torch
from torch import nn as nn
input = torch.tensor([1, 3, 6], dtype=torch.float32)
target = torch.tensor([1, 4, 9], dtype=torch.float32)
loss = nn.L1Loss()
print(loss(input, target)) # [|(1 + 3 + 6) - (1 + 4 + 9)|]/3 = 4/3
torch.nn.MSELoss()
torch.nn.MSELoss(size_average=None, reduce=None, reduction='mean')
函数描述:
函数使用:
Input: (∗), where ∗means any number of dimensions.(任意的维度均可参与运算)
Target: (∗), same shape as the input.
import torch
from torch import nn as nn
input = torch.tensor([1, 3, 6], dtype=torch.float32)
target = torch.tensor([1, 4, 9], dtype=torch.float32)
loss = nn.MSELoss()
print(loss(input, target)) # [(6-9)^2 + (4-3)^2 + (1-1)^2]/3 = 10/3
torch.nn.CrossEntropyLoss()
对于交叉熵损失函数常常用于分类问题
torch.nn.CrossEntropyLoss(weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean', label_smoothing=0.0)
函数描述:
函数使用:
C=number of classes
N=batch size
- Input: Shape(C), (N,C) or (N,C,d1,d2,…,dK) with K≥1 in the case of K-dimensional loss.
输入可以是单一类别概率(C
)、多个样本的类别概率(N, C
)或带有多个维度的数据(N, C, d1, d2, ..., dK
)。
- Target: If containing class indices, shape (), (N) or (N,d1,d2,…,dK) with K≥1 in the case of K-dimensional loss where each value should be between [0,C). If containing class probabilities, same shape as the input and each value should be between [0,1]
目标可以是类索引(表示具体类别的整数,范围是 [0, C)
)或者类概率(表示每个类别的概率值,范围是 [0, 1]
,形状与输入相同)。
- 案例:计算CIFAR-10分类模型网络的各损失函数
import torchvision
from torch import nn as nn
from torch.utils.data import DataLoader
class CIFAR10_test(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2),
nn.MaxPool2d(kernel_size=2),
nn.Flatten(),
nn.Linear(in_features=1024, out_features=64),
nn.Linear(in_features=64, out_features=10)
)
def forward(self, input):
return self.model(input)
# 2.加载数据集
data = torchvision.datasets.CIFAR10('data-train', train=False, transform=torchvision.transforms.ToTensor(),
download=True)
data_loader = DataLoader(data, batch_size=1)
# 3.输出结果
testNet = CIFAR10_test()
loss = nn.CrossEntropyLoss()
for x in data_loader:
img, target = x
input = testNet(img)
print(loss(input, target))