I have two vectors v
and w
and I want to make a matrix m
out of them such that:
m[i, j] = v[i] * w[j]
In other words I want to calculate the outer product of them. I can do it either by using theano.tensor.outer
or by adding new indexes to v
and v
and using the dot
product.
m = T.dot(v[:,numpy.newaxis], w[numpy.newaxis,:])
Now, I try to solve a bit more general problem. Instead of two vectors v
and w
I have two matrices (I call them v
and w
again) and I would like to calculate an outer product of each row from matrix v
with the correspondent row of matrix w
(i_th row in the first matrix should be multiplied with the i_th row of the second matrix). So, I would like to do something like that:
m1 = T.tensordot(v[:,:, numpy.newaxis], w[:,:,numpy.newaxis], axes = [[2],[2]])
m[i, j, k] = m1[i, k, j, k]
In other words, m[:,:,k]
is the matrix corresponding to outer product of k_th
row from the matrix v
and k_th
row of the matrix w
.
I see two problems with the above "solution". First, it is not really a solution, since the second line of the code is not a proper theano code. So, my first question is how to do this "advanced slicing" by forcing some indexes to be equal. For example m[i, k] = a[i, k, i, i, k]
. Second, I do not like the fact that I first create a 4D tesnor (m1
) from two 2D tensors and then I reduce it back to a 3D tensor. It can be very memory consuming. I guess one can avoid it.