1

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

wy= tensor([[ 74.2090, -92.9444,  45.2677, -38.4132, -39.8641,  -6.9193,  67.4830,
     -80.1534]], grad_fn=<AddmmBackward>)

x= 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!

kaloon
  • 157
  • 4
  • 15

1 Answers1

1

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)
user2246849
  • 4,217
  • 1
  • 12
  • 16