Product between 2D matrix and 3D tensor

I’m tying to calculate the matrix-tensor product between a bxd matrix (a batch of d-dimensional vectors) and a bxdxn tensor (a batch of dxn matrices), so as the product is a bxn matrix, so as each row of the result is the product (a vector) between the corresponding vector of the matrix and the matrix of the tensor.

For instance:

import torch
X = torch.randn(32, 512)
Y = torch.randn(32, 512, 128)
# Z: needs to be of torch.Size([32, 128])

Thanks!

Hi,

I think you want to do this

import torch 

X = torch.randn(32, 1, 512)
Y = torch.randn(32, 512, 128)
Z = torch.matmul(X, Y)
1 Like