I’ve encountered the following, and looking for an explanation
import torch
print(torch.__version__)
1.3.1
a = torch.tensor([[1]])
b = torch.tensor(1)
print(a.shape, b.shape)
torch.Size([1, 1]), torch.Size([])
print(a == b)
tensor([[True]])
I’ve encountered the following, and looking for an explanation
import torch
print(torch.__version__)
1.3.1
a = torch.tensor([[1]])
b = torch.tensor(1)
print(a.shape, b.shape)
torch.Size([1, 1]), torch.Size([])
print(a == b)
tensor([[True]])
If you want to detect equivalence of sizes and shape, torch.equal is the right thing to use.
In your example above, a == b
, we are broadcasting a
and b
and then performing the operation: https://pytorch.org/docs/stable/notes/broadcasting.html.
The steps look roughly like the following:
Understood. Is there a proper name for a torch.Size([])?
OK, it looks it’s called a scalar https://discuss.pytorch.org/t/what-does-torch-size-0-means.
thanks for answering