PyTorch中的损失函数简介

创建日期:2025-03-12
更新日期:2025-03-12

示例1

import torch
import torch.nn as nn

x = torch.tensor([1, 2, 3], dtype=torch.float32)
y = torch.tensor([4, 5, 6], dtype=torch.float32)

criterion = nn.MSELoss()
loss = criterion(x, y)

print(loss.item())

示例2

import torch
import torch.nn as nn

x = torch.tensor([0.8, 2.1, 3.2], dtype=torch.float32)
y = torch.tensor([0, 1, 2], dtype=torch.float32)

criterion = nn.CrossEntropyLoss()
loss = criterion(x, y)

print(loss.item())

损失函数

损失函数说明
nn.L1LossCreates a criterion that measures the mean absolute error (MAE) between each element in the input x//x// and target y//y//.
nn.MSELossCreates a criterion that measures the mean squared error (squared L2 norm) between each element in the input x//x// and target y//y//.
nn.CrossEntropyLossThis criterion computes the cross entropy loss between input logits and target.
nn.CTCLossThe Connectionist Temporal Classification loss.
nn.NLLLossThe negative log likelihood loss.
nn.PoissonNLLLossNegative log likelihood loss with Poisson distribution of target.
nn.GaussianNLLLossGaussian negative log likelihood loss.
nn.KLDivLossThe Kullback-Leibler divergence loss.
nn.BCELossCreates a criterion that measures the Binary Cross Entropy between the target and the input probabilities:
nn.BCEWithLogitsLossThis loss combines a //Sigmoid// layer and the //BCELoss// in one single class.
nn.MarginRankingLossCreates a criterion that measures the loss given inputs x1//x//1, x2//x//2, two 1D mini-batch or 0D //Tensors//, and a label 1D mini-batch or 0D //Tensor// y//y// (containing 1 or -1).
nn.HingeEmbeddingLossMeasures the loss given an input tensor x//x// and a labels tensor y//y// (containing 1 or -1).
nn.MultiLabelMarginLossCreates a criterion that optimizes a multi-class multi-classification hinge loss (margin-based loss) between input x//x// (a 2D mini-batch //Tensor//) and output y//y// (which is a 2D //Tensor// of target class indices).
nn.HuberLossCreates a criterion that uses a squared term if the absolute element-wise error falls below delta and a delta-scaled L1 term otherwise.
nn.SmoothL1LossCreates a criterion that uses a squared term if the absolute element-wise error falls below beta and an L1 term otherwise.
nn.SoftMarginLossCreates a criterion that optimizes a two-class classification logistic loss between input tensor x//x// and target tensor y//y// (containing 1 or -1).
nn.MultiLabelSoftMarginLossCreates a criterion that optimizes a multi-label one-versus-all loss based on max-entropy, between input x//x// and target y//y// of size (N,C)(//N//,//C//).
nn.CosineEmbeddingLossCreates a criterion that measures the loss given input tensors x1//x//1​, x2//x//2​ and a //Tensor// label y//y// with values 1 or -1.
nn.MultiMarginLossCreates a criterion that optimizes a multi-class classification hinge loss (margin-based loss) between input x//x// (a 2D mini-batch //Tensor//) and output y//y// (which is a 1D tensor of target class indices, 0≤y≤x.size(1)−10≤//y//≤x.size(1)−1):
nn.TripletMarginLossCreates a criterion that measures the triplet loss given an input tensors x1//x//1, x2//x//2, x3//x//3 and a margin with a value greater than 00.
nn.TripletMarginWithDistanceLossCreates a criterion that measures the triplet loss given input tensors a//a//, p//p//, and n//n// (representing anchor, positive, and negative examples, respectively), and a nonnegative, real-valued function ("distance function") used to compute the relationship between the anchor and positive example ("positive distance") and the anchor and negative example ("negative distance").