I am relative new to pytorch. After doing a pretty exhaustive search online, I still couldn’t obtain the operation I want.
My question is How do do matrix multiplication (matmal) along certain axis?
For example, if I want to multiply a vector by a matrix, that would just be the following:
a = torch.rand(3,5)
b = torch.rand(3)
torch.matmul(b,a)
One can interpret this as each element in b scale each row of a, and summing those scaled row together.
What if we have the dimension of a and b as following:
a = torch.rand(3,5,10)
b = torch.rand(3,10)
and we want to do matrix multiplication along the first axis, basically, here’s what I want to do in a for loop form:
product = []
for i in range(10):
a_i = a[:,:,i]
b_i = b[:,i]
a_i_mul_b_i = torch.matmul(b_i,a_i)
product.append(a_i_mul_b_i)
Although product is not a tensor but a list of tensor, but if we ignore the datatype, this is the end result I want.