Matrix multiplication on matrix with dimension >= 4

I have two tensors a = torch.randn(2, 3, 10, 25, 25) # B x C x * and b = a.permute(0, 2, 1, 3, 4) # 2 x 10 x 3 x 25 x 25, I want to do matrix multiplication on a and b to get the output with size of 2 x 3 x 3 x 25 x 25,

a = torch.randn(2, 3, 10, 25, 25)
b = a.permute(0, 2, 1, 3, 4)
c = torch.matmul(a, b)

but I ran into this error,

RuntimeError: The size of tensor a (10) must match the size of tensor b (3) at non-singleton dimension 2

so I check the torch.matmul and found out that I should permute a = a.permute(0, 3, 4, 1, 2) # 2 x 25 x 25 x 3 x 10 and b and then do matrix multiplication. But in training, pytorch is in the default channel first so is swapping the channel to last, doing matmul and swapping channel back to first the only solution?

The matrix multiplication doesn’t “check” for the memory format, i.e. if the tensors are in channels-first or channels-last, but just uses both tensors in the provided shapes.
It’s on the user to make sure the result is expected, so I would recommend to create tensors with known values and compare the output to this reference.

1 Like