I have two matrices X and V with respective shape (K, M) and (K,L) The first dimension represents the batch_size (each row is a singular element from the batch). I wish to do the outer product only on dimensions 1 on both of these matrices such that the output has the shape (K, M, L).
I have of course tried to use the NumPy np.outer function but it flattens the arrays and the result is of final shape (K * M, K * L)
Is there a function in one of Python's standard libraries that can do the operation I want to do ?
An example of what I want, but it is not efficient :
W = np.zeros(K,M,L)
for i in range(X.shape[0]):
W[i,:,:] = np.outer(X[i,:], V[i,:])
Edit: Found an answer already on StackOverflow!
CodePudding user response:
For my specific problem, adding an extra dimension and letting Python do the broadcasting was sufficient.
My working code:
W = X[:,:,None]*V[:,None,:]
Thanks for this answer for helping me out.