pythonpytorchnlpmatrix-multiplicationallennlp

RuntimeError: Expected 3-dimensional tensor, but got 2-dimensional tensor for argument


I have two tensors names: wy and x, both of them with size 8:

import torch

wy = torch.tensor([[7.2, -2.9, 5.2, -8.4, -3.8, -6.9, 7.4, -8.1]])

x = torch.tensor([[70., 77., 101., 75., 40., 83., 48., 73.]])

Now, I want to do bmm to multiply x * wy as follow:

xWy = x.bmm(wy.unsqueeze(2)).squeeze(3)

I got an error:

RuntimeError: Expected 3-dimensional tensor, but got 2-dimensional tensor for argument #1 'batch1' (while checking arguments for bmm)

8*8 should be possible. but I don't know why I got this error every time.

any help, please!


Solution

  • bmm stands for batch matrix-matrix product. So it expects both tensors with a batch dimension (i.e., 3D as the error says).

    For single tensors, you want to use mm instead. Note that Tensor.mm() also exists with the same behaviour.

    x.mm(wy.transpose(0, 1))
    
    tensor([[-5051.9199]])
    

    Or better, for two 1D tensor you can use dot for dot product.

    # Or simply do not initialise them with an additional dimension. Not needed.
    x.squeeze().dot(wy.squeeze())
    
    tensor(-5051.9199)