Hi, I want to multiply a vector by a matrix (batch, c, h,w).
I can do it using a for loop like this :
In [68]: x=torch.rand(2,3,2,2)
In [69]: y=torch.rand(3)
In [70]: for i in range(x.shape[1]):
...: x[:,i,:,:] = x[:,i,:,:]*y[i]
I know I can simply do :
In [101]: for i in range(x.shape[1]):
...: var=y[i]
...: x = x* var
but I want to remove the loop and make it a full vectorized multiplication.
I cant use mm, or other matrix to vector methods because it complains about the dimensions having mismatch. so Iām clueless about how I can make it vectorized.
by the way, is using mul_()
(as in x[:,i,:,:].mul_(y[i]
) in this case has any significance against the normal way of doing it like what I did in the for loop?
Thanks in advance