ValueError:only one element tensors can be converted to Python scalars

分享
手机软件开发 2024-9-27 19:26:13 73 0 来自 中国
偶然间我们在使用pytorch将一个list转换成为tensor的时间大概会遇到这个标题:
报错内容:
ValueErrornly 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.
要只管制止如许的操纵。
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 20:29, Processed in 0.101015 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表