Maverick Meerkat
Maverick Meerkat

Reputation: 6404

Expanding a matrix to a tensor

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

Upvotes: 1

Views: 319

Answers (1)

hpaulj
hpaulj

Reputation: 231385

In [220]: a = np.arange(15).reshape(5,3)                                        
In [221]: a                                                                     
Out[221]: 
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])

Using standard numpy broadcasting:

In [222]: a[:,:,None]*a[:,None,:]                                               
Out[222]: 
array([[[  0,   0,   0],
        [  0,   1,   2],
        [  0,   2,   4]],

       [[  9,  12,  15],
        [ 12,  16,  20],
        [ 15,  20,  25]],

       [[ 36,  42,  48],
        [ 42,  49,  56],
        [ 48,  56,  64]],

       [[ 81,  90,  99],
        [ 90, 100, 110],
        [ 99, 110, 121]],

       [[144, 156, 168],
        [156, 169, 182],
        [168, 182, 196]]])
In [223]: _.shape                                                               
Out[223]: (5, 3, 3)

einsum was mentioned:

In [224]: np.einsum('ij,ik->ijk',a,a).shape                                     
Out[224]: (5, 3, 3)

The broadcasting works by:

(5,3) => (5,3,1) and (5,1,3) => (5,3,3)

Noneindexing is like your reshape(5,3,1), adding a dimension. In broadcasting size 1 dimensions match the corresponding dimension of the other array(s). reshape is cheap; use it freely.

tensordot is not well named; the 'tensor' means it can work with larger than 2d (but then all of numpy can do that). 'dot' refers to the dot product, the contraction. With einsum and matmul/@ tensordot isn't needed. And never worked for creating higher dimensional arrays.

Upvotes: 2

Related Questions