I have to execute the below operation several thousand times and it is slowing down my code substantially:
T = 50
D = 10
K = 20
x = np.random.randn(T, D)
y = np.random.randn(T, K)
result = np.zeros((K, D))
for k in range(K):
for t in range(T):
result[k] += y[t, k] * x[t] # Multiply scalar element in y with row in x
Basically i'm trying to add up each element in column k
of matrix y
with the corresponding row in x
and sum them up. I tried using np.einsum()
to solve this:
result = np.einsum("ij,ik->jk", y, x)
which at least gives me result.shape == (K, D)
, but the results don't match! How can i efficiently perform this operation? Is this even possible with np.einsum()
?