Pytorch:运行时错误:零维张量(位置 0)无法连接

S.E*_*.EB 5 python-3.x pytorch tensor pytorch-geometric

我有两个张量

# losses_q
tensor(0.0870, device='cuda:0', grad_fn=<SumBackward0>)
# this_loss_q
tensor([0.0874], device='cuda:0', grad_fn=<AddBackward0>)
Run Code Online (Sandbox Code Playgroud)

当我尝试连接它们时,pytorch 会引发错误:

losses_q = torch.cat((losses_q, this_loss_q), dim=0)

RuntimeError: zero-dimensional tensor (at position 0) cannot be concatenated
Run Code Online (Sandbox Code Playgroud)

如何解决这个错误?

jod*_*dag 6

losses_q是零维的,所以不能与任何东西连接。您可以在连接之前将其重新整形为一维张量。

losses_q = torch.cat((losses_q.reshape(1), this_loss_q), dim=0)
Run Code Online (Sandbox Code Playgroud)