Multiply 1D tensor by 2D tensor


I am trying to multiply a 1D tensor by a 2D tensor as shown in the picture above (whereby each element of the first tensor is multiplied by each element in the corresponding row of the second tensor). If I were to run a*b, I would get RuntimeError: inconsistent tensor size. I could of course iterate through each tensor and multiply each of the corresponding elements, but I assume that there exists a ‘better’ way than this?

2 Likes

The problem is that shapes (4,) and (4,3) are not broadcastable together. Broadcasting works by trying to align starting from the right end. So we want to make the first tensor a shape (4,1) one.

Therefore, tensor1d.unsqueeze(1) * tensor2d should give you desired result.

2 Likes

Thanks, but this doesn’t appear to work. Running Variable(torch.randn(4)).unsqueeze(1)*Variable(torch.randn(4, 3)) still gives the error RuntimeError: inconsistent tensor size

It works on my install. Are you on version 0.1.12? That might explain why since broadcasting is introduced in 0.2.0.

I’d recommend to update since 0.1.12 is quite old. However, this should work on your version: Variable(torch.randn(4)).unsqueeze(1).expand(4,3)*Variable(torch.randn(4, 3))

1 Like

I’ve had problems before due to having an old version of pytorch. Do you happen to know why the latest version isn’t available through Conda?

I’m pretty sure that 0.2.0 is available on conda.

That’s odd. When I run conda update pytorch , I get:
Fetching package metadata …
Solving package specifications: .

# All requested packages already installed.
# packages in environment at /home/username/miniconda3:
#
pytorch                   0.1.12          py36cuda8.0cudnn6.0_1

Answered my own question - turns out that’s not the correct command for updating packages

1 Like

Glad that you found out!