Search code examples
pythonarraysnumpytheano

Outer products using theano/numpy.tensordot


I have a NumPy array of n=3 vectors of length m=10, so input.shape = (n,m,1). I am trying to compute the outer products of each vector using theano/numpy.tensordot such that the output.shape = (n,m,m) and :

output[i] = np.dot(input[i],input[i].T) = np.outer(input[i],input[i])

I'm pretty sure this is possible, but am having a hard time figuring out what arguments to use for axes. This can be easily done iterating over the array, but I am trying to complete this step using tensordot as it is implemented the same in theano and numpy. This is important because I am using theano, but using numpy to debug currently.


Solution

  • You are not exactly using the sum reduction part of a dot product and just broadcasting elementwise multiiplication for each row with it's transposed version. This could be achieved in a vectorized manner for all rows again with broadcasting by creating singleton dimensions in two versions of the input array - One with singleton dim at axis=1 and another at axis=2. Since, the input array is already 3D shaped, so we already have the version #2 with us. To get the first version, just swap axes along the last two dims. Thus, multiplying these two versions would give us the desired output, like so -

    output = input*input.swapaxes(1,2)