How to reproduce the results of a specific application of numpy.tensordot

Viewed 16

I am currently trying to understand how Tensordot works in Python. Consider the following specific example:

import numpy as np
A = [0,1,1,0,1,1,0,1,1,1,1,1]
A = np.reshape(A, (2,3,2))
print('A is ', A)
B = [1,0,0,0,1,0,1,1,0,0,1,1]
B = np.reshape(B,(3,2,2))
print('B is ', B)
C = np.tensordot(A,B,axes = (0,1))
print('C is ', C)

Based on the axes I used, I see the that the resulting matrix will have shape 3 x 2 x 3 x 2. In other words, C will be a 3 x 2 matrix whose elements are 3 x 2 matrices. I also got this result which confirms this, and so I believe I understand how a choice of axes affects the size of the resulting matrix.

print(C.shape)
>Output: (3, 2, 3, 2)

However, I do not understand how the computer computed matrix C. Here are matrices A and B:

A is  [[[0 1]
        [1 0]
        [1 1]]

      [[0 1]
       [1 1]
       [1 1]]]
B is  [[[1 0]
        [0 0]]

       [[1 0]
        [1 1]]

       [[0 0]
        [1 1]]]

Based on the axes I chose, it seems that the first element of C should be determined from the two matrices in A and the first column of the first matrix in B. In other words, it should be

First element (a matrix) of A * 1 + second element (a matrix) of B * 0

However, the first element of C is a 3 x 2 matrix of 0s. Here is the matric C. Why did Python give this output?

C is  [[[[0 0]
         [0 0]
         [0 0]]

        [[1 0]
         [2 1]
         [1 1]]]


       [[[1 0]
         [2 1]
         [1 1]]

        [[0 0]
         [1 1]
         [1 1]]]


       [[[1 0]
         [2 1]
         [1 1]]

        [[1 0]
         [2 1]
         [1 1]]]]
0 Answers
Related