Size not showing correctly

I have the following outputs.shape: tensor(-0.0162, device=‘cuda:0’, grad_fn=) torch.Size([])

The following is my error:
ValueError: Target size (torch.Size([1])) must be the same as input size (torch.Size([]))

How do I make ‘outputs’ to be of size 1?

Please help!

You could unsqueeze the scalar (0-dim) tensor:

x = torch.tensor(-0.0162)
print(x)
# tensor(-0.0162)
print(x.size())
# torch.Size([])

y = x.unsqueeze(0)
print(y)
# tensor([-0.0162])
print(y.size())
# torch.Size([1])

Note that the size is shown correctly and is expected to be empty for a 0-dim tensor.

1 Like