Tensors with no content but size

torch.zeros(0,3,3), when printed, yields, tensor([], size=(0, 3, 3)), as opposed to torch.zeros(3,3), which yields tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) ]

Can anyone please explain the differences between the two? What is the first tensor? It doesn’t seem to have any values (zeros) inside it, but is of size (0,3,3)… I looked up the documentations on torch.zeros(), but didn’t see anything related to this…

Thank you in advance for your help!

The first has literally 0 = 0*3*3 elements. The second has 9 = 3*3 elements.

You didn’t ask, but as background: Zero-sized tensors are not useful per se (and the earliest PyTorch versions didn’t support them), but in many applications it turned out that it is good to have them to more gracefully deal with corner cases.

Best regards

Thomas

1 Like

thank you for the answer! Could you please explain what “having 0=033 elements” mean?

It means “Thomas needs to use backticks more”. :stuck_out_tongue: I edited the post above to show the *.

1 Like

Ahh I see :slight_smile: ! Could you please explain, then, the size=(0,3,3) part of the output? When I print out print(torch.zeros(0,3,3).shape) it yields torch.Size([0, 3, 3]). Does this mean that the tensor has no elements (0=0*3*3) , but has an attribute Size that is [0,3,3]?? Shouldn’t the .shape yield zero as the tensor has no elements?

All tensors have some metadata, size is one of them. Also, a 0x3x3 tensor is different to a 0x3 one, but you could not tell the difference if it didn’t print it.

Best regards

Thomas

I see! Thank you :slight_smile: it was really helpful!