美文网首页
ValueError:only one element tens

ValueError:only one element tens

作者: 小黄不头秃 | 来源:发表于2022-09-19 00:19 被阅读0次

有时候我们在使用pytorch将一个list转换成为tensor的时候可能会遇到这个问题:

报错内容:
ValueError:only one element tensors can be converted to Python scalars
或者:
TypeError: only integer tensors of a single element can be converted to an index

x = torch.tensor([1,2,3])
a = [x,x]
print(torch.tensor(a))

修改为:

x = torch.tensor([1,2,3])
a = [x.tolist(),x.tolist()]
print(torch.tensor(a))

或者:

x = torch.tensor([1,2,3])
a = [x,x]
print(torch.tensor([item.numpy() for item in a]))

当然会带来一些性能问题。因为这样子转换是非常耗时间的。
他可能会发出警告:
UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor.

要尽量避免这样的操作。

相关文章

  • ValueError:only one element tens

    有时候我们在使用pytorch将一个list转换成为tensor的时候可能会遇到这个问题: 报错内容:ValueE...

  • pytoch tensor常见坑

    ValueError: only one element tensors can be converted to ...

  • python : array str to float

    如果遇到: ValueError: Can only create a chararray from string...

  • logging ValueError: …… element #

    错误 解决

  • 塞戈维亚音阶路书-b小调,E大调

    安德列斯·塞戈维亚"I had only one teacher,myself,and only one stud...

  • only one

    发了365条“我爱你” 收到364句“呵呵” 以及一句“妈妈也爱你” 《Only one》 It was a fo...

  • One & Only

    One & Onlyby Bour “除你之外别无他爱。” 故事发生在一个小镇,咱们主人公出生的地方。 朴灿烈...

  • Only One

    万朵桃花,我只愿摘其中一朵,而我心中开的最美的,就是你了。^_^

  • Only   one

    像珍珠,像花瓣,像飞龙,像大雁,像高楼,像车辆。每一块千姿百态的岩石,大小不一的分布在地球上的每一个角落中。 水连...

  • Only one

    你大概玩过这样的游戏:在夏天炎热的某一天,把放大镜拿出来放在报纸上,中间隔一小段距离。很快你就会发现,如果放大镜不...

网友评论

      本文标题:ValueError:only one element tens

      本文链接:https://www.haomeiwen.com/subject/cfjanrtx.html