简介
- 损失函数(loss function)或代价函数(cost function)是将随机事件或其有关随机变量的取值映射为非负实数以表示该随机事件的“风险”或“损失”的函数。
- 在应用中,损失函数通常作为学习准则与优化问题相联系,即通过最小化损失函数求解和评估模型。例如在统计学和机器学习中被用于模型的参数估计(parametric estimation),在宏观经济学中被用于风险管理(risk mangement)和决策 ,在控制理论中被应用于最优控制理论(optimal control theory)
常用loss
BCELoss
用于二分类任务,二值交叉熵(Binary Cross Entropy)。
公式如下,其中y是真实值, y ^ \hat{y} y^是预测值:
loss = -ylog\ y ^ \hat{y} y^ - (1-y)log(1- y ^ \hat{y} y^ )
class torch.nn.BCELoss
Examples::
>>> m = nn.Sigmoid()
>>> loss = nn.BCELoss()
>>> input = torch.randn(3, requires_grad=True) # 从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的3个随机数
>>> target = torch.empty(3).random_(2) # 生成3个值,值为0 或者 1
>>> output = loss(m(input), target) # m(input)激活,生成概率p,区间为(0, 1)
>>> output.backward()
CELoss
用于多分类任务,交叉熵(Cross Entropy)。
公式如下,其中y是真实值, y ^ \hat{y} y^是预测值:
loss = -ylog y ^ \hat{y} y^
class torch.nn.CrossEntropyLoss
It is useful when training a classification problem with `C` classes.
The `input` is expected to contain scores for each class.
Examples::
>>> loss = nn.CrossEntropyLoss()
>>> input = torch.randn(3, 5, requires_grad=True) # 3行5列,每一行代码当前数据特征
>>> target = torch.empty(3, dtype=torch.long).random_(5) # 3个数,值范围是0,1...,4
>>> output = loss(input, target)
>>> output.backward()
支持多目标分割、分类
def forward(self, predictive, target):
"""
:param predictive: (N,C)or (N,C,H,W)
:param target: (N,)or (N,C,H,W,C)
:return:
"""
c = predictive.size()[1]
if len(predictive.shape) == 4: # 语义分割任务 # [b,c,h,w]-> [b,h,w,c] -> [b*h*w, c]
predictive = predictive.transpose(1, 2).transpose(2, 3).contiguous().view(-1, c)
target = torch.argmax(target, dim=1) # 输入是one hot,但是loss不支持 [b,c,h,w]
else: # 分类任务
predictive = predictive.view(-1, c) # [b*h*w, c]
target = target.view(-1).long() # [b*h*w, c]
return F.cross_entropy(predictive, target, weight=self.weight, reduction=self.reduction)
MSELoss
计算均方误差 Mean Squared Error (squared L2 Norm)。
公式如下,其中y是真实值, y ^ \hat{y} y^是预测值:
class torch.nn.MSELoss
Creates a criterion that measures the mean squared error (squared L2 norm) between
each element in the input `x` and target `y`.
Examples::
>>> loss = nn.MSELoss()
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randn(3, 5)
>>> output = loss(input, target)
>>> output.backward()