我在Numpy中有四个多维张量v[i,j,k]
,a[i,s,l]
,w[j,s,t,m]
,x[k,t,n]
,而我正在尝试计算由下式给出的张量z[l,m,n]
:
z[l,m,n] = sum_{i,j,k,s,t} v[i,j,k] * a[i,s,l] * w[j,s,t,m] * x[k,t,n]
所有张量相对较小(总共少于32k个元素),但我需要多次执行此计算,所以我希望函数尽可能少地开销。
我尝试使用numpy.einsum
这样实现它:
z = np.einsum('ijk,isl,jstm,ktn', v, a, w, x)
但是很慢。我还尝试了以下numpy.tensordot
调用序列:
z = np.zeros((a.shape[-1],w.shape[-1],x.shape[-1]))
for s in range(a.shape[1]):
for t in range(x.shape[1]):
res = np.tensordot(v, a[:,s,:], (0,0))
res = np.tensordot(res, w[:,s,t,:], (0,0))
z += np.tensordot(res, x[:,s,:], (0,0))
在for for循环中加总s
和t
(s
和t
都非常小,所以这不是太大的问题) 。这样做效果更好,但仍然没有我想象的那么快。我认为这可能是因为tensordot
在获取实际产品之前需要在内部执行的所有操作(例如,置换轴)。
我想知道是否有更有效的方法在Numpy中实现这种操作。我也不介意在Cython中实现这个部分,但我不确定什么是正确的算法。
答案 0 :(得分:4)
在部分中使用np.tensordot
,您可以对事物进行矢量化 -
# Perform "np.einsum('ijk,isl->jksl', v, a)"
p1 = np.tensordot(v,a,axes=([0],[0])) # shape = jksl
# Perform "np.einsum('jksl,jstm->kltm', p1, w)"
p2 = np.tensordot(p1,w,axes=([0,2],[0,1])) # shape = kltm
# Perform "np.einsum('kltm,ktn->lmn', p2, w)"
z = np.tensordot(p2,x,axes=([0,2],[0,1])) # shape = lmn
运行时测试并验证输出 -
In [15]: def einsum_based(v, a, w, x):
...: return np.einsum('ijk,isl,jstm,ktn', v, a, w, x) # (l,m,n)
...:
...: def vectorized_tdot(v, a, w, x):
...: p1 = np.tensordot(v,a,axes=([0],[0])) # shape = jksl
...: p2 = np.tensordot(p1,w,axes=([0,2],[0,1])) # shape = kltm
...: return np.tensordot(p2,x,axes=([0,2],[0,1])) # shape = lmn
...:
案例#1:
In [16]: # Input params
...: i,j,k,l,m,n = 10,10,10,10,10,10
...: s,t = 3,3 # As problem states : "both s and t are very small".
...:
...: # Input arrays
...: v = np.random.rand(i,j,k)
...: a = np.random.rand(i,s,l)
...: w = np.random.rand(j,s,t,m)
...: x = np.random.rand(k,t,n)
...:
In [17]: np.allclose(einsum_based(v, a, w, x),vectorized_tdot(v, a, w, x))
Out[17]: True
In [18]: %timeit einsum_based(v,a,w,x)
10 loops, best of 3: 129 ms per loop
In [19]: %timeit vectorized_tdot(v,a,w,x)
1000 loops, best of 3: 397 µs per loop
案例#2(更大的数据):
In [20]: # Input params
...: i,j,k,l,m,n = 15,15,15,15,15,15
...: s,t = 3,3 # As problem states : "both s and t are very small".
...:
...: # Input arrays
...: v = np.random.rand(i,j,k)
...: a = np.random.rand(i,s,l)
...: w = np.random.rand(j,s,t,m)
...: x = np.random.rand(k,t,n)
...:
In [21]: np.allclose(einsum_based(v, a, w, x),vectorized_tdot(v, a, w, x))
Out[21]: True
In [22]: %timeit einsum_based(v,a,w,x)
1 loops, best of 3: 1.35 s per loop
In [23]: %timeit vectorized_tdot(v,a,w,x)
1000 loops, best of 3: 1.52 ms per loop