I have this code:
JavaScript
x
5
1
import torch
2
3
list_of_tensors = [ torch.randn(3), torch.randn(3), torch.randn(3)]
4
tensor_of_tensors = torch.tensor(list_of_tensors)
5
I am getting the error:
ValueError: only one element tensors can be converted to Python scalars
How can I convert the list of tensors to a tensor of tensors in pytorch?
Advertisement
Answer
Here is a solution:
JavaScript
1
3
1
tensor_of_tensors = torch.stack((list_of_tensors))
2
print(tensor_of_tensors) #shape (3,3)
3