zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Pytorch Loss Functions总结

2023-03-14 22:40:36 时间

Pytorch Loss Functions总结:


文档链接:Loss Functions


L1Loss

用于测量输入中每个元素之间的平均绝对误差 (MAE)。

>>> loss = nn.L1Loss()
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randn(3, 5)
>>> output = loss(input, target)
>>> output.backward()


MSELoss

用于测量输入中每个元素之间的均方误差(L2 范数)

loss = nn.MSELoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5)
output = loss(input, target)
output.backward()


CROSSENTROPYLOSS

此标准计算输入和目标之间的交叉熵损失


The input is expected to contain raw, unnormalized scores for each class. input has to be a Tensor of size ©(C) for unbatched input,(minibatc**h,C) or (minibatch, C, d_1, d_2, …, d_K)(minibatc**h,C,d1,d2,…,d**K) with K geq 1K≥1 for the K-dimensional case. The last being useful for higher dimension inputs, such as computing cross entropy loss per-pixel for 2D images.

# Example of target with class indices
loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
output.backward()
# Example of target with class probabilities
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5).softmax(dim=1)
output = loss(input, target)
output.backward()


CTCLoss

CTC loss 理解_代码款款的博客-CSDN博客_ctc loss


CTC Loss原理 - 知乎 (zhihu.com)


计算连续(未分段)时间序列和目标序列之间的损失。CTCLoss 对输入与目标可能对齐的概率求和,生成一个相对于每个输入节点可微分的损失值。假定输入与目标的对齐方式为"多对一"


NLLLoss

详解torch.nn.NLLLOSS - 知乎 (zhihu.com)


log_softmax与softmax的区别在哪里? - 知乎 (zhihu.com)


PoissonNLLLoss

目标泊松分布的负对数似然损失。


BCELOSS

loss函数之BCELoss - 简书 (jianshu.com)


MARGINRANKINGLOSS

loss函数之MarginRankingLoss - 简书 (jianshu.com)


HingeEmbeddingLoss

COSINEEMBEDDINGLOSS

loss函数之CosineEmbeddingLoss,HingeEmbeddingLoss_ltochange的博客-CSDN博客_余弦相似度损失函数


MultiLabelMarginLoss

loss函数之MultiMarginLoss, MultiLabelMarginLoss_ltochange的博客-CSDN博客


HuberLoss

回归损失函数:Huber Loss_Peanut_范的博客-CSDN博客_huber loss


SmoothL1Loss

创建一个条件,如果绝对元素误差低于 beta,则使用平方项,否则使用 L1 项。它对异常值的敏感度低于torch.nn.MSELoss,并且在某些情况下可以防止梯度爆炸(例如,参见Ross Girshick的论文Fast R-CNN)。


SoftMarginLoss

loss函数之SoftMarginLoss - 简书 (jianshu.com)


MultiLabelSoftMarginLoss

MultiLabelSoftMarginLoss函数_Coding-Prince的博客-CSDN博客_multilabelsoftmarginloss


TripletMarginLoss

PyTorch TripletMarginLoss(三元损失)_zj134_的博客-CSDN博客_pytorch 三元组损失


TripletMarginWithDistanceLoss

loss函数之TripletMarginLoss与TripletMarginWithDistanceLoss_ltochange的博客-CSDN博客


nn.xx 与 nn.functional .xx区别:

我们经常看到,二者有很多相同的loss函数,他们使用时有什么区别呢?


两者的相同之处:


nn.Xxx和nn.functional.xxx的实际功能是相同的,即nn.Conv2d和nn.functional.conv2d 都是进行卷积,nn.Dropout 和nn.functional.dropout都是进行dropout,。。。。。;

运行效率也是近乎相同。

nn.functional.xxx是函数接口,而nn.Xxx是nn.functional.xxx的类封装,并且**nn.Xxx都继承于一个共同祖先nn.Module。**这一点导致nn.Xxx除了具有nn.functional.xxx功能之外,内部附带了nn.Module相关的属性和方法,例如train(), eval(),load_state_dict, state_dict 等。


什么时候使用nn.functional.xxx,什么时候使用nn.Xxx?


这个问题依赖于你要解决你问题的复杂度和个人风格喜好。在nn.Xxx不能满足你的功能需求时,nn.functional.xxx是更佳的选择,因为nn.functional.xxx更加的灵活(更加接近底层),你可以在其基础上定义出自己想要的功能。


个人偏向于在能使用nn.Xxx情况下尽量使用,不行再换nn.functional.xxx ,感觉这样更能显示出网络的层次关系,也更加的纯粹(所有layer和model本身都是Module,一种和谐统一的感觉)。