Search code examples
pythonluatorchpytorch

Strange behaviour in PyTorch


I started learning pyTorch recently.I moved from Torch community as I like Python.

I encountered this strange behaviour in pyTorch.Any insights as why this happened would be appreciated.

x=torch.Tensor(10,1).fill_(1)
y=torch.Tensor(10).fill_(2)
print(x.size())     #output is torch.Size([10, 1])
print(y.size())     #output is  torch.Size([10])
z=x+y
z.size()
z.size()            #output is torch.Size([10, 10])

Output of z is 10 x 10 with value 3 which means it is clearly adding the Tensors(I also checked with other numbers) and then constructing a 10 x 10 Tensor with the values after addition.

Can someone explain me why is this happening . I tried it in Torch(Lua) it did a cumulative addition and returned the Tensor I expected.


Solution

  • When you're doing the sum between the torch Tensors, broadcasting is happening in the background. It's the same behaviour that you'd also see when you do the addition using NumPy. And, PyTorch simply follows the same broadcasting rules that is followed in NumPy.

    You can read and understand broadcasting here: NumPy Broadcasting