我可以消除此计算中的所有 Python 循环吗?
result[i,j,k] = (x[i] * y[j] * z[k]).sum()
其中x[i]
, y[j]
,z[k]
是长度向量N
, x
,具有长度为 , 的第一维y
, st输出是形状并且每个元素是三重乘积的总和(元素方式)。z
A
B
C
(A,B,C)
我可以将其从 3 个循环减少到 1 个循环(下面的代码),但我一直试图消除最后一个循环。
如有必要,我可以制作A=B=C
(通过少量填充)。
# Example with 3 loops, 2 loops, 1 loop (testing omitted)
N = 100 # more like 100k in real problem
A = 2 # more like 20 in real problem
B = 3 # more like 20 in real problem
C = 4 # more like 20 in real problem
import numpy
x = numpy.random.rand(A, N)
y = numpy.random.rand(B, N)
z = numpy.random.rand(C, N)
# outputs of each variant
result_slow = numpy.empty((A,B,C))
result_vec_C = numpy.empty((A,B,C))
result_vec_CB = numpy.empty((A,B,C))
# 3 nested loops
for i in range(A):
for j in range(B):
for k in range(C):
result_slow[i,j,k] = (x[i] * y[j] * z[k]).sum()
# vectorize loop over C (2 nested loops)
for i in range(A):
for j in range(B):
result_vec_C[i,j,:] = (x[i] * y[j] * z).sum(axis=1)
# vectorize one C and B (one loop)
for i in range(A):
result_vec_CB[i,:,:] = numpy.dot(x[i] * y, z.transpose())
numpy.testing.assert_almost_equal(result_slow, result_vec_C)
numpy.testing.assert_almost_equal(result_slow, result_vec_CB)