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?
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.
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))