Ken*_*ith 9 python python-3.x pytorch
我有这个代码:
import torch
list_of_tensors = [ torch.randn(3), torch.randn(3), torch.randn(3)]
tensor_of_tensors = torch.tensor(list_of_tensors)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
ValueError:只有一个元素张量可以转换为Python标量
如何将张量列表转换为pytorch中的张量张量?
Pra*_*kar 12
这是一个解决方案:
tensor_of_tensors = torch.stack((list_of_tensors))
print(tensor_of_tensors) #shape (3,3)
Run Code Online (Sandbox Code Playgroud)