torch将label转为OneHot遇到的问题

方法同: https://www.jianshu.com/p/15b1b809074c

遇到问题报错:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
    execfile(filename, namespace)
  File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 88, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
  File "/media/muya/32512c1a-a13e-4164-99e7-8dd2e82d0358/UDAimage/RES18/train.py", line 182, in <module>
    train(args)
  File "/media/muya/32512c1a-a13e-4164-99e7-8dd2e82d0358/UDAimage/RES18/train.py", line 98, in train
    OneHotLabel = torch.zeros(args.batch_size, args.classnum).scatter_(1, label, 1)
RuntimeError: invalid argument 3: Index tensor must either be empty or have same dimensions as output tensor at /pytorch/aten/src/TH/generic/THTensorEvenMoreMath.cpp:533

原因: 我的标签是一维的

解决方法: 将label 转为二维 

label = label.unsqueeze(-1)

 

Tips:

完整的将label转换onehot参考: https://blog.csdn.net/longshaonihaoa/article/details/105640239

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值