在PyTorch库的torch.nn模块中,提供了许多预定义的损失函数,用于不同的机器学习任务。以下是一些常见的损失函数:
MSELoss(均方误差损失): 常用于回归问题,计算预测值与真实值之间的平均平方误差。
import torch.nn as nn
criterion = nn.MSELoss()
output = ... # your network's output, shape should be the same as target
loss = criterion(output, target)
CrossEntropyLoss(交叉熵损失): 常用于分类问题,计算预测概率[logits]与真实标签之间的交叉熵。
import torch.nn as nn
criterion = nn.CrossEntropyLoss()
output = ... # your network's output, shape should be (batch_size, num_classes)
target = ... # target labels, shape should be (batch_size)
loss = criterion(output, target)
BCELoss(二分类交叉熵损失): 用于二分类问题,计算预测概率与真实标签之间的二进制交叉熵。
import torch.nn as nn
criterion = nn.BCELoss()
output = ... # your network's output, shape should be (batch_size, 1)
target = ... # target labels, shape should be (batch_size)
loss = criterion(output, target)
NLLLoss(负对数似然损失): 常用于多分类问题,计算预测概率与真实标签之间的负对数似然。
import torch.nn as nn
criterion = nn.NLLLoss()
output = ... # your network's output, shape should be (batch_size, num_classes)
target = ... # target labels, shape should be (batch_size)
loss = criterion(output, target)
HingeEmbeddingLoss(铰链嵌入损失): 用于铰链损失问题,计算预测值与真实值之间的铰链嵌入损失。
import torch.nn as nn
criterion = nn.HingeEmbeddingLoss()
output = ... # your network's output, shape should be the same as target
target = ... # target labels, shape should be the same as output
loss = criterion(output, target)
MultiMarginLoss(多边损失): 常用于多分类问题,计算预测标签与真实标签之间的多边损失。
import torch.nn as nn
criterion = nn.MultiMarginLoss()
output = ... # your network's output, shape should be (batch_size, num_classes)
target = ... # target labels, shape should be (batch_size)
loss = criterion(output, target)