arr1 = np.arange(8).reshape(4, 2)
arr2 = np.arange(4, 12).reshape(2, 4)
ans=np.tensordot(arr1,arr2,axes=([1],[0]))
ans2=np.tensordot(arr1,arr2,axes=([0],[1]))
ans3 = np.tensordot(arr1,arr2, axes=([1,0],[0,1]))
I am trying to understand how this tensordot function work . I know that it returns the tensordot product.
but axes part is a little bit difficult for me to comprehend. what i have observed that
for ans it is like the number of columns in array arr1 and the number of rows in arr2 makes the final matrix.
for ans2 it is the other way around number of columns in arr2 and number of rows in arr1
i dont understand axes=([1,0],[0,1]). let me know if my understanding for ans and ans2 is correct
You forgot to show the arrays:
ansis just the regular dot, matrix product:The
dotsum-of-products is performed on([1],[0])axis 1 ofarr1, and axis 0 ofarr2(the conventional across the columns, down the rows). With 2d 'sum across ...' phrase can be confusing. It's clearer when dealing with 1 or 3d arrays. Here the matching size 2 dimensions are summed, leaving the (4,4).ans2reverses them, summing on the 4's, producing a (2,2):tensordothas just transposed the 2 arrays and performed a regulardot:ans3is uses a transpose and reshape (ravel), to sum on both axes:In general,
tensordotuses a mix of transpose and reshape to reduce the problem to a 2dnp.dotproblem. It may then reshape and transpose the result.I find the dimensions control of
einsumto be clearer:With the development of
einsumandmatmul/@,tensordothas become less necessary. It's harder to understand, and doesn't have any speed or flexibility advantages. Don't worry about understanding it.ans3is the trace (sum of diagonal) of the other 2 ans: