Hi!
I want to code this matrix multiplication:
B=16, h=32, w=56, b*b=64, n=25
my code:
torch.mul(torch.rand([16, 32, 56, 64]),torch.rand([16, 32, 56, 64,25]))
but I encountered in with this error:
The size of tensor a (64) must match the size of tensor b (25) at non-singleton dimension 4
Would you mind helping me to fix this problem?
albanD
(Alban D)
May 15, 2020, 10:36pm
2
Hi,
I think the simplest is to write it as a matrix matrix multiplication. And so add a temporary dimension of size 1 on the first Tensor:
torch.matmul(torch.rand([16, 32, 56, 1, 64]),torch.rand([16, 32, 56, 64,25])).squeeze(-2)
1 Like
Thanks for your reply. The problem solved.
Would you mind explaining it? I do not understand it.
albanD
(Alban D)
May 15, 2020, 10:44pm
4
It consider Bxhxw
as batch dimensions. And 1x(bb)
and (bb)xn
for the other dimensions of each other dimensions.
Doing the matrix multiply between the two, will give you a Tensor with 1xn
and with the same batch size.
The squeeze removes the dimension of size 1.
1 Like
Thanks for your help dear @albanD