ValueError: only one element tensors can be converted to Python scalars when converting list to float Torch tensor
ValueError: only one element tensors can be converted to Python scalars when converting list to float Torch tensor
我有以下内容:
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')]
但是,按照 this video 中的说明,我收到此错误:
X_tensor = torch.FloatTensor(X)
ValueError: only one element tensors can be converted to Python scalars
我有以下转换代码:
X_tensor = torch.FloatTensor(X)
我该如何解决这个问题?
$ 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 这里是 torch.tensors.
的列表
使用torch.stack()
:
X = torch.stack(X)
我有以下内容:
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')]
但是,按照 this video 中的说明,我收到此错误:
X_tensor = torch.FloatTensor(X)
ValueError: only one element tensors can be converted to Python scalars
我有以下转换代码:
X_tensor = torch.FloatTensor(X)
我该如何解决这个问题?
$ 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 这里是 torch.tensors.
的列表使用torch.stack()
:
X = torch.stack(X)