ValueError: only one element tensors can be converted to Python scalars when converting list to float Torch tensor
I have the following:
type of X is: <class 'list'>
X: [tensor([[1.3373, 0.5666, 0.2337, ..., 0.4899, 0.1876, 0.5892],
[0.0320, 0.0797, 0.0052, ..., 0.3405, 0.0000, 0.0390],
[0.1305, 0.1281, 0.0021, ..., 0.6454, 0.1964, 0.0493],
...,
[0.2635, 0.0237, 0.0000, ..., 0.6635, 0.1376, 0.2988],
[0.0241, 0.5464, 0.1263, ..., 0.5766, 0.2352, 0.0140],
[0.1740, 0.1664, 0.0057, ..., 0.6056, 0.1020, 1.1573]],
device='cuda:0')]
However, following the instructions in this video, I get this error:
X_tensor = torch.FloatTensor(X)
ValueError: only one element tensors can be converted to Python scalars
I have the following conversion code:
X_tensor = torch.FloatTensor(X)
How can I fix this problem?
$ python
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.__version__
'1.10.1+cu113'
X here is a list of torch.tensors.
Solution 1:
Use torch.stack()
:
X = torch.stack(X)