python 列表Tensor:如何将一个表转化为Tensor?

r6l8ljro  于 2023-01-24  发布在  Python
关注(0)|答案(1)|浏览(188)

我有一个这样的Tensor

tensor([[4.],[1.]], device='cuda:0')

我想更改为以下内容:

tensor([4.,1.], device='cuda:0')

如何做到这一点?
我不确定这是否是错误的原因:

loss = nn.MSELoss(y_tilde,y)

RuntimeError: Boolean value of Tensor with more than one value is ambiguous
ycl3bljg

ycl3bljg1#

使用

torch.flatten()

示例:

t = torch.tensor([[[1, 2],
                   [3, 4]],
                  [[5, 6],
                 [7, 8]]])
out: tensor([[[1, 2],
         [3, 4]],

        [[5, 6],
         [7, 8]]])
torch.flatten(t)
out: tensor([1, 2, 3, 4, 5, 6, 7, 8])

相关问题