如何重现numpy.tensordot的特定应用的结果

发布于 2025-02-11 06:37:57 字数 1218 浏览 0 评论 0原文

我目前正在尝试了解Tensordot在Python中的工作方式。考虑以下特定示例:

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)

根据我使用的轴,我看到所得矩阵将具有3 x 2 x 3 x 2的形状。换句话说,c将是一个3 x 2矩阵,其元素为3 x 2矩阵。我还得到了确认这一点的结果,因此我相信我了解轴的选择如何影响所得矩阵的大小。

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

但是,我不了解计算机计算的矩阵C。这是矩阵A和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]]]

基于我选择的轴,看来C的第一个元素应从A和A和该列的第一列中确定换句话说,换句话说

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

,C的第一个元素是C的第一个元素是0s的3 x 2矩阵。这是矩阵C。为什么Python给出了此输出?

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]]]]

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]]]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文