一、损失函数概念
损失函数:衡量模型输出与真实标签的差异
损失函数(Loss Function):
𝑳𝒐𝒔𝒔 = 𝒇(𝒚^, 𝒚) 一个样本真实值与预测值的差异
代价函数(Cost Function):
cos t = 1 N ∑ i N f ( y i ∧ , y i ) \cos t=\frac{1}{N} \sum_{i}^{N} f\left(y_{i}^{\wedge}, y_{i}\right) cost=N1i∑Nf(yi∧,yi)代价函数是整个样本loss的平均值
目标函数(Objective Function):
𝑶𝒃𝒋 = 𝑪𝒐𝒔𝒕 + Regularization 损失 + 正则项(对模型进行一定约束,怕过拟合)
pytorch中的loss继承Module类
size_average, reduce,将被舍弃,不用
class _Loss(Module):
def __init__(self, size_average=None, reduce=None, reduction='mean'):
super(_Loss, self).__init__()
if size_average is not None or reduce is not None:
self.reduction = _Reduction.legacy_get_string(size_average, reduce)
else:
self.reduction = reduction
二、交叉熵损失函数
交叉熵 = 信息熵 + 相对熵
交叉熵: H ( P , Q ) = − ∑ i = 1 N P ( x i ) log Q ( x i ) \mathrm{H}(\boldsymbol{P}, \boldsymbol{Q})=-\sum_{i=1}^{N} \boldsymbol{P}\left(\boldsymbol{x}_{i}\right) \log Q\left(\boldsymbol{x}_{i}\right) H(P,Q)=−i=1∑NP