无法理解einsum计算

2024-05-14 11:15:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将一些代码从python移植到R(预先警告..我对python的了解远远少于我对R的了解)

无论如何,我有一个复杂的einsum命令,我需要很好地理解,以便翻译它,我有麻烦。基于对前面问题的回答:https://stackoverflow.com/a/59858877/2498193,我试图明确地编写计算代码,但遇到了一些问题

可复制示例:

A = np.random.rand(2, 5, 1)
B = np.random.rand(2, 4, 5, 3)

C = np.einsum('ikl,ijkl->ijl', A, B)

""" Attempt to breakdown einsum into its parts"""
D = np.zeros((A.shape[0], B.shape[1], B.shape[3]))
for i in range(A.shape[0]): 
    for j in range(B.shape[1]):
        for l in range(B.shape[3]):
            for k in range(A.shape[1]):
                D[i, j, l] += A[i, k, l] * B[i, j, k, l]

因此,C的计算运行良好,但当我尝试D时,我得到:

IndexError: index 1 is out of bounds for axis 2 with size 1

我哪里出错了

编辑: 我在索引中发现了一个错误并修复了它,但现在有了一个新的谜团

我的D和输出的固定代码:

for i in range(A.shape[0]): 
    for j in range(B.shape[1]):
        for l in range(A.shape[2]):
            for k in range(A.shape[1]):
                D[i, j, l] += A[i, k, l] * B[i, j, k, l]

C
Out[763]: 
array([[[1.12003067, 1.10913818, 0.6906052 ],
        [1.90393492, 0.95523242, 1.54739457],
        [0.94529917, 0.86866832, 1.50882582],
        [1.7744814 , 1.90689624, 1.55044583]],

       [[0.99766459, 0.81698549, 0.91783172],
        [1.05860284, 1.41360977, 0.84931137],
        [0.92352849, 1.40863288, 1.14309574],
        [0.95503075, 1.1450589 , 1.32160452]]])

D
Out[764]: 
array([[[1.12003067, 0.        , 0.        ],
        [1.90393492, 0.        , 0.        ],
        [0.94529917, 0.        , 0.        ],
        [1.7744814 , 0.        , 0.        ]],

       [[0.99766459, 0.        , 0.        ],
        [1.05860284, 0.        , 0.        ],
        [0.92352849, 0.        , 0.        ],
        [0.95503075, 0.        , 0.        ]]])

所以我得到了部分正确的答案,但我不清楚所有的零是从哪里来的


Tags: 代码inhttps命令fornprangerandom
1条回答
网友
1楼 · 发布于 2024-05-14 11:15:54

我最终发现,在我对einsum正在做的事情的分析中,我还需要考虑广播

解决方案:

A = np.random.rand(2, 5, 1)
B = np.random.rand(2, 4, 5, 3)
C = np.einsum('ikl,ijkl->ijl', A, B)

D = np.zeros((A.shape[0], B.shape[1], B.shape[3]))
AA = np.zeros((2,5,3))
AA[:,:, 0] = A[:,:,0]
AA[:,:, 1] = A[:,:,0]
AA[:,:, 2] = A[:,:,0]

for i in range(AA.shape[0]): 
    for j in range(B.shape[1]):
        for l in range(AA.shape[2]):
            for k in range(AA.shape[1]):
                D[i, j, l] += AA[i, k, l] * B[i, j, k, l]

Out[946]: 
array([[[1.34883962, 1.29672134, 2.40826835],
        [2.12198906, 1.57248206, 1.62157716],
        [1.95668114, 1.45167364, 1.1761399 ],
        [2.14619827, 1.62883231, 1.42584051]],

       [[1.01555102, 1.48221712, 0.67038112],
        [0.52555659, 0.74096584, 0.73631941],
        [0.76738584, 0.61414461, 1.22202416],
        [0.5116698 , 0.94099001, 1.01196491]]])

D
Out[947]: 
array([[[1.34883962, 1.29672134, 2.40826835],
        [2.12198906, 1.57248206, 1.62157716],
        [1.95668114, 1.45167364, 1.1761399 ],
        [2.14619827, 1.62883231, 1.42584051]],

       [[1.01555102, 1.48221712, 0.67038112],
        [0.52555659, 0.74096584, 0.73631941],
        [0.76738584, 0.61414461, 1.22202416],
        [0.5116698 , 0.94099001, 1.01196491]]])

羞耻r似乎没有一个numpy.einsum等价物,但至少这是我可以实现的

相关问题 更多 >

    热门问题