In PyTorch/Numpy, how to multiply rows of a matrix with "matrices" in a 3-D tensor?
For example, a = torch.Tensor([[1,2],[3,4]]) (for numpy it is just a = np.array([[1,2],[3,4]])), and b = torch.ones((2,2,2)),

I would like to product every row of a with the two 2x2 matrices, and get a new matrix [[3,3],[7,7]] (i.e. [1,2]*[[1,1],[1,1]]=[3,3], [3,4]*[[1,1],[1,1]]=[7,7]). Is it possible to achieve this? Thanks!

I consider this an ugly solution, but perhaps this is what you want to achieve:

a = torch.Tensor([[1,2],[3,4]])
b = torch.ones((2,2,2))

A = torch.mm(a[0].view(-1, 2), b[0])
B = torch.mm(a[1].view(-1, 2), b[1])
res = torch.cat([A, B], dim=0)
print(res)


output:

tensor([[3., 3.],
        [7., 7.]])

