Pytorch损失函数

定义

损失函数(Loss Function)
Loss=f(a^,y)Loss=f( \hat{a} ,y)

代价函数(Cost Function)
Cost=1NiNf(yi^,yi)Cost=\frac{1}{N}\sum_{i}^{N}f(\hat{y_{i}},y_{i})

目标函数(Objective Function)
Obj=Cost+RegularizationObj=Cost+Regularization

在Pytorch中如何定义

损失函数要继承于nn.Moudle
参数size_average和reduce已经弃用,现在统一使用reduction参数

几种损失函数

1.CrossEntropyLoss
Pytorch损失函数
Pytorch损失函数
具体推导过程:
Pytorch损失函数
H(P,Q)=DKL(P,Q)+H(P)交叉熵:H(P,Q)=D_{KL}(P,Q)+H(P)
所以优化交叉熵是在优化相对熵

对于nn.CrossEntropyLoss来说,损失函数内部自带softmax,所以不需要人为额外在上一步执行softmax操作

2.nn.NLLLoss
Pytorch损失函数
仅仅实现负号的功能

3.nn.BCELoss
二分类
Pytorch损失函数
因为是一个概率分布,在数据输入损失函数之前呢,需要经过一下Sigmoid来把输出值变成0~1之间

4.nn.BCEWithLogitsLoss
Pytorch损失函数
对于第三个损失函数的补足,不需要再模型中加入Sigmoid函数
并且多了一个参数pos_weight

5.nn.L1Loss和nn.MSELoss
Pytorch损失函数
前面为第一范数,后面的为第二范数

7.SmoothL1Loss
Pytorch损失函数

Pytorch损失函数
8.PoissonNLLLoss
Pytorch损失函数
9.nn.KLDivLoss
Pytorch损失函数
10.nn.MarginRankingLoss
Pytorch损失函数
11.nn.MultiLabelMarginLoss
多标签是指,一个样本对应多个类别
Pytorch损失函数
12nn.SoftMarginLoss
Pytorch损失函数
13.nn.MutiLabelSoftMarginLoss
Pytorch损失函数

14.Pytorch损失函数

15 .nn.TripletMarginLoss
Pytorch损失函数
16.Pytorch损失函数
17.Pytorch损失函数
18.nn.CTCLoss
Pytorch损失函数