How to multiply matrices of 4-dimensions?

Hi, now, I have two matrics A and B, suppose A is a matrix with size(10, 3, 4,5), and 10 is batch size. B is a matrix with size(10, 5,6,7), and 10 is batch size. how can I multiply the two matrics with batch multiply and get the result matrix with size (10, 3,4,6,7)? I tried iterate over the batchsize and use torch.mm, but failed. Is there any good solution?
my codes like this:
for i in range(A.size(0)):
torch.mm(A[i,:], B[i,:]);
but failed, because the function only supports two-dimensional matrices multiplication.

Is there any good solution?

1 Like

You can use torch.matmul to get batch multiplication and possibly .view your 3,4 and 6,7 dimensions as one each before the multiplication and back into two in the result.
torch.matmul(A.view (10,3*4,5),B.view (10,5,6*7)).view (10,3,4,6,7)
Replace the hardcoded sizes appropriately.

Best regards

Thomas

2 Likes

Now with PyTorch 0.4 you can use einsum, too. :slight_smile:

4 Likes