Given an array a of shape (n, k1) and an array b of shape (n, k2), I would like to compute all outer products of a[i] and b[i]. This
import numpy
n = 3
a = numpy.random.rand(n, 7)
b = numpy.random.rand(n, 11)
out = numpy.array([
numpy.outer(a[i], b[i]) for i in range(n)
])
print(out.shape) # (n, 7, 11)
does the trick, but it contains a Python loop which slows things down for large n.
Can the outer products be computed at once?