TypeError: can't convert np.ndarray of type numpy.uint16.

报错代码为:

label = torch.FloatTensor(label)

报错  TypeError: can't convert np.ndarray of type numpy.uint16. The only supported types are: float64, float32, float16, int64, int32, int16, int8, and uint8.

       我没有百度到一样的问题,但看着是数据类型不对——现在是int,需要的是float,解决方法请往下翻,在5.中。

       期间我试了很多int转float的方法,具体如下(由于接触python时间不长,可能有点……嗯……):

1. label = torch.FloatTensor(float(label))

    报错:TypeError: only size-1 arrays can be converted to Python scalars

2. label.astype(float)

    这是百度到的 1.中的报错找到的方案,1.的报错是解决了,但是并没有转化了数据类型,还是报错

    TypeError: can't convert np.ndarray of type numpy.uint16.……

3. 到此,我想,是不是得用pytorch中专用的类型转化函数呢?于是,改为:

     label = label.to(torch.float64)

     报错   AttributeError: 'numpy.ndarray' object has no attribute 'to'

      所以,这个方法只适用于转化前后都是tensor的情况

4. 那么,怎么将numpy转为tensor?

           label = torch.from_numpy(label)

    再加上3.中类型转化函数,得:

          label = torch.from_numpy(label)

          label = label.to(torch.float64)

    然而,并没有什么用,类型还是numpy.uint16

5. 那,还有什么方法呢?我想起了当初学C语言入门时的方法:

  label = label/1.0

    试了那么多方法,问题就这样解决了……

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

总结:

       我想不到3.中类型转化失败的原因。是不是我用的类型转化方法不对呢?针对这个问题,还有什么解决方法?

       我觉得自己的解决方法不够python,有没有python的解决方案呢?

       希望看到这个帖子的各位,不要吝啬,请说出你们的想法!!!

  • 39
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值