Difference in shape of tensor torch.Size([]) and torch.Size([1]) in pytorch
I am new to pytorch. While playing around with tensors I observed 2 types of tensors-

tensor(58)
tensor([57.3895])


I printed their shape and the output was respectively -

torch.Size([])
torch.Size([1])


What is the difference between the two?

You can play with tensors having the single scalar value like this:

import torch

t = torch.tensor(1)
print(t, t.shape) # tensor(1) torch.Size([])

t = torch.tensor([1])
print(t, t.shape) # tensor([1]) torch.Size([1])

t = torch.tensor([[1]])
print(t, t.shape) # tensor([[1]]) torch.Size([1, 1])

t = torch.tensor([[[1]]])
print(t, t.shape) # tensor([[[1]]]) torch.Size([1, 1, 1])

t = torch.unsqueeze(t, 0)
print(t, t.shape) # tensor([[[[1]]]]) torch.Size([1, 1, 1, 1])

t = torch.unsqueeze(t, 0)
print(t, t.shape) # tensor([[[[[1]]]]]) torch.Size([1, 1, 1, 1, 1])

t = torch.unsqueeze(t, 0)
print(t, t.shape) # tensor([[[[[[1]]]]]]) torch.Size([1, 1, 1, 1, 1, 1])

#squize dimension with id 0
t = torch.squeeze(t,dim=0)
print(t, t.shape) # tensor([[[[[1]]]]]) torch.Size([1, 1, 1, 1, 1])

#back to beginning.
t = torch.squeeze(t)
print(t, t.shape) # tensor(1) torch.Size([])

print(type(t)) # &lt;class 'torch.Tensor'&gt;
print(type(t.data)) # &lt;class 'torch.Tensor'&gt;


Tensors, do have a size or shape. Which is the same. Which is actually a class torch.Size.
You can write help(torch.Size) to get more info.
Any time you write t.shape, or t.size() you will get that size info.

The idea of tensors is they can have different compatible size dimension for the data inside it including torch.Size([]).

Any time you unsqueeze a tensor it will add another dimension of 1.
Any time you squeeze a tensor it will remove dimensions of 1, or in general case all dimensions of one.
