PiMathCLanguage
PiMathCLanguage

Reputation: 375

How to calculate the outer product of two matrices A and B per rows faster in python (numpy)?

Let say we have two matrices A and B.

A has the shape (r, k) and B has the shape (r, l).

Now I want to calculate the np.outer product of these two matrices per rows. After the outer product I then want to sum all values in axis 0. So my result matrix should have the shape (k, l).

E.g.: Form of A is (4, 2), of B is (4, 3).

import numpy as np

A = np.array([[0, 7], [4, 1], [0, 2], [0, 5]])
B = np.array([[9, 7, 7], [6, 7, 5], [2, 7, 9], [6, 9, 7]])

# This is the first outer product for the first values of A and B
print(np.outer(A[0], B[0])) # This will give me 

# First possibility is to use list comprehension and then
sum1 = np.sum((np.outer(x, y) for x, y in zip(A, B)), axis=0)

# Second possibility would be to use the reduce function
sum2 = reduce(lambda sum, (x, y): sum+np.outer(x, y), zip(A, B), np.zeros((A.shape[1], B.shape[1])))

# result for sum1 or sum2 looks like this:
# array([[ 175.,  156.,  133.], [ 133.,  131.,  137.]])

I'm asking my self, is there a better or faster solution? Because when I have e.g. two matrices with more than 10.000 rows this takes some time.

Only using the np.outer function is not the solution, because np.outer(A, B) will give me a matrix with shape (8, 12) (this is not what I want).

Need this for neural networks backpropagation.

Upvotes: 3

Views: 954

Answers (1)

Divakar
Divakar

Reputation: 221634

You could literally transfer the iterators as string notation to np.einsum -

np.einsum('rk,rl->kl',A,B)

Or with matrix-multiplication using np.dot -

A.T.dot(B)

Upvotes: 3

Related Questions