0

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

1 Answers1

0

Maybe Discrepancy of align on memory and shape.If you use view method, you can handle tensor shape as you want. But array on memory is not reshaped. This sometimes causes unclear problem. So contiguous() or reshape() may help you.

ref: https://pytorch.org/docs/master/tensors.html?highlight=view#torch.Tensor.view What's the difference between reshape and view in pytorch?