RuntimeError: Expected tensor to have size 100 at dimension 1, but got size 128 for argument #2 'batch2' (while checking arguments for bmm)

I’m trying to implement an attention model, but fail to execute the matmul

torch.matmul(att, v)

The shape of att and v is:

att shape:torch.Size([20, 3, 128, 128])
v shape:torch.Size([20, 3, 128, 100])

i get such an error:

RuntimeError: Expected tensor to have size 100 at dimension 1, but got size 128 for argument #2 ‘batch2’ (while checking arguments for bmm)

I also tried generate two tensors with the same shape by torch.randn and repeat the same operation and no error ouccrred. I don’t know what makes such an error.

Your code seems to work:

att = torch.randn(20, 3, 128, 128)
v = torch.randn(20, 3, 128, 100)

out = torch.matmul(att, v)
print(out.shape)
> torch.Size([20, 3, 128, 100])

Yes, I found i accidently repeat matmul () in the same function. I didn’t pay attention to the line number so got it wrong. I have solved the problem now.
Thanks for your help!