gpt4 book ai didi

python - for 循环等效于 einsum 表达式

转载 作者:行者123 更新时间:2023-12-01 07:14:59 27 4
gpt4 key购买 nike

我有以下 Einstein Sum (einsum) 表达式,

import numpy as np 

x = np.random.rand(1,8,2,8,10)
y = np.random.rand(8,10,10)
z = np.einsum('nkctv,kvw->nctw', x, y)

print (z.shape)

输出为:

(1, 2, 8, 10)

我想做的是使用一组嵌套的 for 循环构建等效的功能,我遵循这个 tutorial它展示了如何使用嵌套 for 循环实现一些 einsum 表达式,但它并没有多大帮助。

有人知道怎么做吗?

最佳答案

感谢@hpaulj,

解决办法是:

R = np.zeros((1,2,8,13))

for n in range(1):
for c in range(2):
for t in range(8):
for w in range(13):
total = 0
# These are the variables to sum over
for v in range(13):
for k in range(8):
total += x[n,k,c,t,v]*y[k,v,w]
R[n,c,t,w] = total

关于python - for 循环等效于 einsum 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58021736/

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