gpt4 book ai didi

python - 传递给 numpy.einsum() 的下标是什么意思?

转载 作者:太空宇宙 更新时间:2023-11-04 01:58:45 47 4
gpt4 key购买 nike

我试图理解一个 python 代码,它使用 numpy.einsum() 将 4 维 numpy 数组 A 转换为 2- 或 3-维数组。传递给numpy.einsum()的下标如下:

Mat1 = np.einsum('aabb->ab', A) 

Mat2 = np.einsum('abab->ab', A)

Mat3 = np.einsum('abba->ab', A)

T1 = np.einsum('abcb->abc' A)

T2 = np.einsum('abbc->abc', A)

等根据(Understanding NumPy's einsum)和(Python - Sum 4D Array)的回答,我尝试使用numpy.sum()来理解上面下标的含义,例如,Mat1 = np. sum(A, axis=(0,3)) 但我无法重现我用 numpy.einsum() 得到的结果。有人可以解释一下如何在 numpy.einsum() 中解释这些下标吗?

最佳答案

我建议你阅读 Einstein notation on Wikipedia .

以下是对您问题的简短回答:

np.einsum('aabb->ab', A)

意思是:

res = np.empty((max_a, max_b), dtype=A.dtype)
for a in range(max_a):
for b in range(max_b):
res[a, b] = A[a, a, b, b]
return res

简短说明:
aabb 表示索引及其相等性(参见A[a, a, b, b]);
->ab 表示形状是 (max_a, max_b) 并且您不需要对这两个索引求和。 (如果它们也是 c,那么您应该用 c 对所有内容求和,因为它不会出现在 -> 之后)


其他你的例子:

np.einsum('abab->ab', A)

# Same as (by logic, not by actual code)

res = np.empty((max_a, max_b), dtype=A.dtype)
for a in range(max_a):
for b in range(max_b):
res[a, b] = A[a, b, a, b]
return res
np.einsum('abba->ab', A) 

# Same as (by logic, not by actual code)

res = np.empty((max_a, max_b), dtype=A.dtype)
for a in range(max_a):
for b in range(max_b):
res[a, b] = A[a, b, b, a]
return res
np.einsum('abcb->abc', A)

# Same as (by logic, not by actual code)

res = np.empty((max_a, max_b, max_c), dtype=A.dtype)
for a in range(max_a):
for b in range(max_b):
for c in range(max_c):
res[a, b, c] = A[a, b, c, b]
return res
np.einsum('abbc->abc', A)

# Same as (by logic, not by actual code)

res = np.empty((max_a, max_b, max_c), dtype=A.dtype)
for a in range(max_a):
for b in range(max_b):
for c in range(max_c):
res[a, b, c] = A[a, b, b, c]
return res

一些代码来检查它是否真的是真的:

import numpy as np


max_a = 2
max_b = 3
max_c = 5

shape_1 = (max_a, max_b, max_c, max_b)
A = np.arange(1, np.prod(shape_1) + 1).reshape(shape_1)

print(A)
print()
print(np.einsum('abcb->abc', A))
print()

res = np.empty((max_a, max_b, max_c), dtype=A.dtype)
for a in range(max_a):
for b in range(max_b):
for c in range(max_c):
res[a, b, c] = A[a, b, c, b]

print(res)
print()

关于python - 传递给 numpy.einsum() 的下标是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56193116/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com