Division in batches of a 3D tensor (Pytorch)
I have a 3D tensor of size say 100x5x2 and mean of the tensor across axis=1 which gives shape 100x2.
100 here is the batch size. Normally without batch, the division of tensor of shape 5x2 and 2 works perfectly but in the case of the 3D tensor with batch, I’m receiving error.
a = torch.rand(5,2)
b = torch.rand(2)
z=a/b

gives me expected answer.
a = torch.rand(100,5,2)
b = torch.rand(100,2)
z=a/b

Gives me the following error.
The size of tensor a (5) must match the size of tensor b (100) at non-singleton dimension 1.
How to divide these tensors such that my output is of shape 100x5x2 ? Something like bmm for division?

Simply do:
z = a / b.unsqueeze(1)

This adds an extra dimension in b and makes it of shape (100, 1, 2) which is compatible for broadcasting with a.
