pytocrh学习记录0507。

一。toch.nn.CELoss
最近在使用该loss的时候遇到了如下三个问题。

  1. One-hot问题。
    RuntimeError: multi-target not supported at /opt/conda/conda-bld/pytorch_1587428398394/work/aten/src/THCUNN/generic/ClassNLLCriterion.cu:18
    参考此篇博客.这是因为CEloss期望的真值输入不能是one-hot形式的,打印一下原本的预测值和真值维度、部分数据如下:
pred shape:  torch.Size([128, 10])
target shape:  torch.Size([128, 10])
pred:  tensor([ -1.8647,  11.9629,  -8.9586,  -4.3062,  -2.7667,   2.8557,  -1.5226,-12.8641,   3.2789,   0.8572], device='cuda:0', grad_fn=<SelectBackward>)
target:  tensor([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], device='cuda:0')

CELoss在内部运算过程中会自动进行one-hot编码,所以,这里target中对应维度应该是【128, 1】, 具体到上述打印中的例子, 应该就是单维张量tensor(4, device='cuda:0')
2. 数据类型问题

RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target' in call to _thnn_nll_loss_forward

nn.CELoss(pred, target)其中的第二参数target,其数据类型必须是long类型,如果自己构造的Dataset中的__getitem__()返回的是其他类型比如float,就会报错。
3. 数据归一化问题。和softmax相关
nn.CELoss(pred, target)内涵了对pred的softmax操作,因此输入的pred,不需要也不应该再传入softmax层。再补充一下softmax的作用,就是将pred中(- ∞ \infty , + ∞ \infty )的数据归一化到(0, 1),再配合torch.argmax就可以获取概率最大值对应的类别。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值