Multiplying tensors containing images in numpy
Asked Answered
N

1

1

I have the following 3rd order tensors. Both tensors matrices the first tensor containing 100 10x9 matrices and the second containing 100 3x10 matrices (which I have just filled with ones for this example).

My aim is to multiply the matrices as the line up one to one correspondance wise which would result in a tensor with shape: (100, 3, 9) This can be done with a for loop that just zips up both tensors and then takes the dot of each but I am looking to do this just with numpy operators. So far here are some failed attempts

Attempt 1:

import numpy as np
T1 = np.ones((100, 10, 9))
T2 = np.ones((100, 3, 10))
print T2.dot(T1).shape

Ouput of attempt 1 :

(100, 3, 100, 9)

Which means it tried all possible combinations ... which is not what I am after.

Actually non of the other attempts even compile. I tried using np.tensordot , np.einsum (read here https://jameshensman.wordpress.com/2010/06/14/multiple-matrix-multiplication-in-numpy that it is supposed to do the job but I did not get Einsteins indices correct) also in the same link there is some crazy tensor cube reshaping method that I did not manage to visualize. Any suggestions / ideas-explanations on how to tackle this ?

November answered 30/12, 2015 at 1:52 Comment(0)
S
1

Did you try?

In [96]: np.einsum('ijk,ilj->ilk',T1,T2).shape
Out[96]: (100, 3, 9)

The way I figure this out is look at the shapes:

(100, 10, 9))  (i, j, k)
(100, 3, 10)   (i, l, j)
-------------
(100, 3, 9)    (i, l, k)

the two j sum and cancel out. The others carry to the output.


For 4d arrays, with dimensions like (100,3,2,24 ) there are several options:

Reshape to 3d, T1.reshape(300,2,24), and after reshape back R.reshape(100,3,...). Reshape is virtually costless, and a good numpy tool.

Add an index to einsum: np.einsum('hijk,hilj->hilk',T1,T2), just a parallel usage to that of i.

Or use elipsis: np.einsum('...jk,...lj->...lk',T1,T2). This expression works with 3d, 4d, and up.

Southwestwards answered 30/12, 2015 at 2:11 Comment(3)
Yup I did and it worked ! just before I saw this haha. But I have no idea how this works. For the problem I am dealing with I actually have 4th order tensors (convolutional neural networks...) so I am trying to get a grasp of what is happneing ?November
The 4th dimension - is like i that carries through, or like j that sums? Or is it like k and unique to one of the inputs.Southwestwards
hmm ok so the example would be: (100,3,2,24 ) and (100,3,24,28) so its quite tricky to explain bassically the idea is that there are 100 cubes each cube containing 3 images of size 24x24 or 28x28 I would like to match them element wise outpting something of the form (100, 3, 2, 28)November

© 2022 - 2024 — McMap. All rights reserved.