Multiplying Tensors Containing Images In Numpy
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 wi
Solution 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.
Post a Comment for "Multiplying Tensors Containing Images In Numpy"