ValueError:将列表转换为浮点 Torch 张量时,只能将一个元素张量转换为 Python 标量

Mon*_*lal 3 python pytorch 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')]
Run Code Online (Sandbox Code Playgroud)

但是,按照该视频中的说明操作,我收到此错误:

    X_tensor = torch.FloatTensor(X)
ValueError: only one element tensors can be converted to Python scalars
Run Code Online (Sandbox Code Playgroud)

我有以下转换代码:

X_tensor = torch.FloatTensor(X)
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

$ 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'
Run Code Online (Sandbox Code Playgroud)

这里的 X 是 torch.tensors 的列表。

joe*_*140 9

使用torch.stack()

X = torch.stack(X)
Run Code Online (Sandbox Code Playgroud)