I have an (m, n)
matrix where each row is an example with n
features. I want to expand it to an (m, n, n)
matrix, i.e. for each example create an outer product of its features. I've looked into tensordot
but haven't figured out a way to do so - it seems to only contract the the tensors, not expand it.
a = np.arange(5*3).reshape(5, 3, 1)
b = np.arange(5*3).reshape(1, 3, 5)
c = np.tensordot(a, b, axes=([1,2],[1,0])) # gives a (5,5) matrix
c = np.tensordot(a, b, axes=([1,2],[0,1])) # throws a shape-mismatch error
I'll give a simple example for one row. Say you have the col vector a = [1, 2, 3]
what I want to get is the a * a.T
i.e.:
1, 2, 3
2, 4, 6
3, 6, 9