丢失因变量(loss function)或价格因变量(cost function)是将随机事变或其相关随机变量的取值映照为非负实数以表白该随机事变的“危害”或“丢失”的因变量。在运用中,丢失因变量常常动作进修规则与优化题目相接洽,即经过最小化丢失因变量求解和评价模子。比方在统计学和呆板进修中被用来模子的参数估量(parametric estimation),在直观财经学中被用来危害处置(risk mangement)和计划 ,在遏制表面中被运用于最优遏制表面(optimal control theory) 。
Pytorch十九种丢失因变量的运用详解
丢失因变量经过torch.nn包实行,
1 基础用法
1
2
criterion = LossCriterion() #结构因变量有本人的参数
loss = criterion(x, y) #挪用标及时也有参数
2 丢失因变量
2-1 L1范数丢失 L1Loss
计划 output 和 target 之差的一致值。
1
torch.nn.L1Loss(reduction='mean')
参数:
reduction-三个值,none: 不运用约简;mean:归来loss和的平衡值; sum:归来loss的和。默许:mean。
2-2 均方缺点丢失 MSELoss
计划 output 和 target 之差的均方差。
1
torch.nn.MSELoss(reduction='mean')
参数:
reduction-三个值,none: 不运用约简;mean:归来loss和的平衡值; sum:归来loss的和。默许:mean。
2-3 穿插熵丢失 CrossEntropyLoss
当演练有 C 个类型的分门别类题目时很灵验. 可选参数 weight 必需是一个1维 Tensor, 权重将被调配给各个类型. 对于不平稳的演练集特殊灵验。
在多分门别类工作中,常常沿用 softmax 激活因变量+穿插熵丢失因变量,由于穿插熵刻画了两个几率散布的分别,但是神经搜集输入的是向量,并不是几率散布的情势。以是须要 softmax激活因变量将一个向量举行“归一化”成几率散布的情势,再沿用穿插熵丢失因变量计划 loss。
1
torch.nn.CrossEntropyLoss(weight=None, ignore_index=-100, reduction='mean')
参数:
weight (Tensor, optional) – 自设置的每个类型的权重. 必需是一个长度为 C 的 Tensor
ignore_index (int, optional) – 树立一个目的值, 该目的值会被忽视, 进而不会感化到 输出的梯度。
reduction-三个值,none: 不运用约简;mean:归来loss和的平衡值; sum:归来loss的和。默许:mean。
2-4 KL 散度丢失 KLDivLoss
计划 input 和 target 之间的 KL 散度。KL 散度可用来测量各别的贯串散布之间的隔绝, 在贯串的输入散布的空间上(分割采集样品)长进行径直回归时 很灵验.
1
torch.nn.KLDivLoss(reduction='mean')
参数:
reduction-三个值,none: 不运用约简;mean:归来loss和的平衡值; sum:归来loss的和。默许:mean。
2-5 二进制穿插熵丢失 BCELoss
二分门别类工作时的穿插熵计划因变量。用来丈量重构的缺点, 比方机动源代码机. 提防目的的值 t[i] 的范畴为0到1之间.
1
torch.nn.BCELoss(weight=None, reduction='mean')
参数:
weight (Tensor, optional) – 自设置的每个 batch 元素的 loss 的权重. 必需是一个长度为 “nbatch” 的 的 Tensor
pos_weight(Tensor, optional) – 自设置的每个正样品的 loss 的权重. 必需是一个长度 为 “classes” 的 Tensor
2-6 BCEWithLogitsLoss
BCEWithLogitsLoss丢失因变量把 Sigmoid 层集成到了 BCELoss 类中. 该版比用一个大略的 Sigmoid 层和 BCELoss 在数值上更宁静, 由于把这两个操纵兼并为一个层之后, 不妨运用 log-sum-exp 的 本领来实行数值宁静.
1
torch.nn.BCEWithLogitsLoss(weight=None, reduction='mean', pos_weight=None)
参数:
weight (Tensor, optional) – 自设置的每个 batch 元素的 loss 的权重. 必需是一个长度 为 “nbatch” 的 Tensor
pos_weight(Tensor, optional) – 自设置的每个正样品的 loss 的权重. 必需是一个长度 为 “classes” 的 Tensor
2-7 MarginRankingLoss
1
torch.nn.MarginRankingLoss(margin=0.0, reduction='mean')
对于 mini-batch(小批量) 中每个范例的丢失因变量如次:
参数:
margin:默许值0
2-8 HingeEmbeddingLoss
1
torch.nn.HingeEmbeddingLoss(margin=1.0, reduction='mean')
对于 mini-batch(小批量) 中每个范例的丢失因变量如次:
参数:
margin:默许值1
2-9 多标签分门别类丢失 MultiLabelMarginLoss
1
torch.nn.MultiLabelMarginLoss(reduction='mean')
对于mini-batch(小批量) 中的每个样品按如次公式计划丢失:
2-10 光滑版L1丢失 SmoothL1Loss
也被称为 Huber 丢失因变量。
1
torch.nn.SmoothL1Loss(reduction='mean')
个中
2-11 2分门别类的logistic丢失 SoftMarginLoss
1
torch.nn.SoftMarginLoss(reduction='mean')
2-12 多标签 one-versus-all 丢失 MultiLabelSoftMarginLoss
1
torch.nn.MultiLabelSoftMarginLoss(weight=None, reduction='mean')
2-13 cosine 丢失 CosineEmbeddingLoss
1
torch.nn.CosineEmbeddingLoss(margin=0.0, reduction='mean')
参数:
margin:默许值0
2-14 多类型分门别类的hinge丢失 MultiMarginLoss
1
torch.nn.MultiMarginLoss(p=1, margin=1.0, weight=None, reduction='mean')
参数:
p=1大概2 默许值:1
margin:默许值1
2-15 三元组丢失 TripletMarginLoss
1
torch.nn.TripletMarginLoss(margin=1.0, p=2.0, eps=1e-06, swap=False, reduction='mean')
个中:
2-16 贯穿时序分门别类丢失 CTCLoss
CTC贯穿时序分门别类丢失,不妨对没有对齐的数据举行机动对齐,重要用在没有事前对齐的序列化数据演练上。比方语音辨别、ocr辨别之类。
1
torch.nn.CTCLoss(blank=0, reduction='mean')
参数:
reduction-三个值,none: 不运用约简;mean:归来loss和的平衡值; sum:归来loss的和。默许:mean。
2-17 负对数似然丢失 NLLLoss
负对数似然丢失. 用来演练 C 个类型的分门别类题目.
1
torch.nn.NLLLoss(weight=None, ignore_index=-100, reduction='mean')
参数:
weight (Tensor, optional) – 自设置的每个类型的权重. 必需是一个长度为 C 的 Tensor
ignore_index (int, optional) – 树立一个目的值, 该目的值会被忽视, 进而不会感化到 输出的梯度.
2-18 NLLLoss2d
对于图片输出的负对数似然丢失. 它计划每个像素的负对数似然丢失.
1
torch.nn.NLLLoss2d(weight=None, ignore_index=-100, reduction='mean')
参数:
weight (Tensor, optional) – 自设置的每个类型的权重. 必需是一个长度为 C 的 Tensor
reduction-三个值,none: 不运用约简;mean:归来loss和的平衡值; sum:归来loss的和。默许:mean。
2-19 PoissonNLLLoss
目的值为泊松散布的负对数似然丢失
1
torch.nn.PoissonNLLLoss(log_input=True, full=False, eps=1e-08, reduction='mean')
参数:
log_input (bool, optional) – 即使树立为 True , loss 将会依照公 式 exp(input) – target * input 来计划, 即使树立为 False , loss 将会依照 input – target * log(input+eps) 计划.
full (bool, optional) – 能否计划十足的 loss, i. e. 加上 Stirling 好像项 target * log(target) – target + 0.5 * log(2 * pi * target).
eps (float, optional) – 默许值: 1e-8