pythonpython-3.xpytorchtensorpytorch-geometric

PyTorch: RuntimeError: zero-dimensional tensor (at position 0) cannot be concatenated


I have two tensors:

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

When I am trying to concat them, PyTorch raises an error:

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

RuntimeError: zero-dimensional tensor (at position 0) cannot be concatenated

How to resolve this error?


Solution

  • losses_q is zero dimensional so can't be concatenated with anything. You can reshape it into a one dimensional tensor before concatenation.

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