Coeff-wise multiplication of tensors with different dims

Hello,

After an update, some parts of my code don’t work anymore.
I have a tensor X of size 64 * 1 * 3 * 3 and a tensor Y of size 1 * 3 * 3, and I want to make the coeff-wise multiplication Z = X * Y. It would be equivalent to:

Z = X.new().resize_as_(X)
for i in range(64):
    Z[i] = X[i] * Y

Is the solution Z = X * Y.unsqueeze(0).expand_as(X) efficient? Is there a better way to do it?

Thank you

Which Pytorch version are you using?
Broadcasting should take care of it.

The following code snippet is equal to yours:

a = torch.randn(64, 1, 3, 3)
b = torch.zeros(1, 3, 3)
b[0, 0, 0] = 1
c = a * b

Your code returns an error:

Traceback (most recent call last):
  File "main.py", line 13, in <module> 
    c = a * b
  File ".../python3.6/site-packages/torch/tensor.py", line 283, in __mul__
    return self.mul(other)
RuntimeError: inconsistent tensor size at ...

I am working with the version 0.1.12_1.

Ah ok, broadcasting was introduced in version 0.2.
Try to update Pytorch to version 0.3.
Just select your config and follow the instructions here.

The problem was: I never updated anaconda and when I “updated” pytorch with conda install pytorch torchvision -c pytorch, it was downgraded
I just had to update anaconda and reinstall pytorch.