pytorch应用(五)Loss function

很多的 loss 函数都有 size_average reduce 两个布尔类型的参数,需要解释一下。因为一般损失函数都是直接计算 batch 的数据,因此返回的 loss 结果都是维度为 (batch_size, ) 的向量。

如果 reduce = False,返回维度为 (batch_size, ) 的向量,input和output的element wise求loss,此时size_average失效

如果 reduce = True,那么 loss 返回的是标量 tensor.shape = ()

  如果size_average = True,那么返回loss.mean();

  如果size_average = False,那么返回loss.sum();

查看官网这两个flag都Deprecated了,而且默认都是进行了降维与平均的

https://pytorch.org/docs/stable/nn.html?highlight=crossentropyloss#torch.nn.CrossEntropyLoss

nn.L1Loss

                   \text{loss}(\mathbf{x}_i, \mathbf{y}_i) = |\mathbf{x}_i - \mathbf{y}_i|

 

两个向量的差值

loss_fn = torch.nn.SmoothL1Loss(reduce=True, size_average=False)
input = torch.autograd.Variable(torch.randn(3,4))
target = torch.autograd.Variable(torch.randn(3,4))

loss = loss_fn(input, target)
print(input); print(target); print(loss)
print(input.size(), target.size(), loss.size())
#print(input.shape, target.shape, loss.shape)
#tensor.shape和tensor.size()都可以表示形状

>>>tensor([[-0.8321, -1.5957, -0.7866,  0.1292],
        [-0.1069,  0.3355, -2.4420, -2.0299],
        [ 0.0513,  0.1084, -0.7796, -0.8375]])
>>>tensor([[-0.6004, -0.6185, -1.1132,  1.0856],
        [ 0.3787, -0.9000,  1.7433, -1.6910],
        [ 0.6767,  0.3840, -1.3867, -0.6717]])
>>>tensor(6.0426)
#这种一维的tensor是可以直接当做一个数来计算的,例如loss/12可直接求平均
#但是在计算除法时“/”只取结果整数部分,“%”只取结果余数部分,“float(loss)/12”才是精确除法
>>>((3, 4), (3, 4), ())

nn.SmoothL1Loss

也叫作 Huber Loss,误差在 (-1,1) 上是平方损失,其他情况是 L1 损失。

             \text{loss}(\mathbf{x}_i, \mathbf{y}_i) = \left\{\begin{matrix} \frac12(\mathbf{x}_i -\mathbf{y}_i)^2 & \text{if } |\mathbf{x}_i -\mathbf{y}_i| < 1 \\ |\mathbf{x}_i -\mathbf{y}_i| - \frac12, & \text{otherwise} \end{matrix}\right.

nn.MSELoss

均方损失函数,用法和上面类似,这里 loss, x, y 的维度是一样的,可以是向量或者矩阵,ii 是下标。

                \text{loss}(\mathbf{x}_i, \mathbf{y}_i) = (\mathbf{x}_i - \mathbf{y}_i)^2

nn.CrossEntropyLoss

               \begin{align*} \text{loss}(\mathbf{x}, \text{label}) &= - \boldsymbol{w}_{\text{label}}\log \frac{e^{\mathbf{x}_{\text{label}}}}{\sum_{j=1}^N e^{\mathbf{x}_j}} \\ &= \boldsymbol{w}_{\text{label}} \left[ -\mathbf{x}_{\text{label}} + \log \sum_{j=1}^N e^{\mathbf{x}_j} \right] \end{align*}

weight = torch.Tensor([1,2,1,1,10])
loss_fn = torch.nn.CrossEntropyLoss(reduce=False, size_average=False, weight=weight)
input = Variable(torch.randn(3, 5)) # (batch_size, C)
target = Variable(torch.LongTensor(3).random_(5))
loss = loss_fn(input, target)
print(input); print(target); print(loss)

输入为三个数据,输出的loss为一个长度为3的一维tensor,这里reduce, size_average也可以使用

参考链接:https://blog.csdn.net/zhangxb35/article/details/72464152

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值