我有这个代码:import torchlist_of_tensors = [ torch.randn(3), torch.randn(3), torch.randn(3)]tensor_of_tensors = torch.tensor(list_of_tensors)我收到错误消息:ValueError:只有一个元素张量可以转换为 Python 标量如何将张量列表转换为 pytorch 中的张量张量?
2 回答

互换的青春
TA贡献1797条经验 获得超6个赞
您还可以将火炬张量类型转换为 NumPy 数组,然后将它们转换为张量
list_of_tensors = [torch.randn(3).numpy(),torch.randn(3).numpy(),torch.randn(3).numpy()]
tensor_of_tensors = torch.tensor(list_of_tensors)

慕村225694
TA贡献1880条经验 获得超4个赞
这是一个解决方案:
tensor_of_tensors = torch.stack((list_of_tensors)) print(tensor_of_tensors) #shape (3,3)
添加回答
举报
0/150
提交
取消