1
votes

I'm trying to compute the row-wise outer-product between two matrices in theano, without using scan. I can do this in numpy by using einsum, which isn't available in theano.

A = np.array([[1,1,1],[2,2,2]])
B = np.array([[3,3,3,3],[4,4,4,4]])
print np.einsum('xi,xj->xij', A, B)
[[[3 3 3 3]
  [3 3 3 3]
  [3 3 3 3]]

 [[8 8 8 8]
  [8 8 8 8]
  [8 8 8 8]]]
1
I thought this was a simple question; but I must say the answer eludes me so far. - Eelco Hoogendoorn

1 Answers

6
votes

This should be doable using some reshaping: Many of the simple einsum operations boil down to that. The complicated ones don't.

import theano
import theano.tensor as T
import numpy as np

a = np.array([[1,1,1],[2,2,2]]).astype('float32')
b = np.array([[3,3,3,3],[4,4,4,4]]).astype('float32')

A = T.fmatrix()
B = T.fmatrix()

C = A[:, :, np.newaxis] * B[:, np.newaxis, :]

print C.eval({A:a, B:b})

results in

[[[ 3.  3.  3., 3.]
  [ 3.  3.  3., 3.]
  [ 3.  3.  3.. 3.]]

 [[ 8.  8.  8., 8.]
  [ 8.  8.  8., 8.]
  [ 8.  8.  8., 8.]]]