当前位置:   article > 正文

bert系列第二篇:几个损失函数_bert的损失函数

bert的损失函数

L1-Loss(MAE)

L o s s ( x , y ) = 1 N ∑ i ( ∣ x i − y i ∣ ) Loss(x,y)=\frac{1}{N} \sum_{i}( |x_i-y_i|) Loss(x,y)=N1i(xiyi)

简单而言,就是两个向量的绝对值的误差。 默认求平均,可以设置为sum。
也是mean absolute error(MAE)

举例而言:

 loss = nn.L1Loss()
    '''
    L1 Loss
    '''
    predict_ar = [0,2,3]
    target_ar = [2,2,3]
    input = torch.FloatTensor(predict_ar)
    input.requires_grad = True
    target = torch.FloatTensor(target_ar)
    output = loss(input, target)
    print(output)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

结果则是0.667

MSE(L2 Loss)

这个也容易理解,则是平方误差。 mean squared error。

 loss = nn.MSELoss()
    '''
    L1 Loss
    '''
    predict_ar = [0, 2, 3]
    target_ar = [2, 2, 3]

    input = torch.FloatTensor(predict_ar)
    input.requires_grad = True
    target = torch.FloatTensor(target_ar)

    output 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号