在https://numpy.org/doc/stable/reference/generated/numpy.einsum.html
optimize{False, True, 'greedy', 'optimal'}, optional 控制是否應該進行中間優化。如果 False 和 True 將默認為“貪婪”演算法,則不會發生優化。還接受來自 np.einsum_path 函式的顯式收縮串列。有關更多詳細資訊,請參閱 np.einsum_path。默認為假。
在我看來,optimize標志是在多個收縮中選擇順序。例如,
A B C -> D
對于 (AB)C 或 A(BC) 或 (AC)B,它更快,而不是二元收縮,例如, AB->C。
對于以下代碼 A[a,b] * B[b,c,d] = C[a,c,d]
import numpy as np
import time
import scipy.stats
# from https://stackoverflow.com/questions/15033511/compute-a-confidence-interval-from-sample-data
def mean_confidence_interval(data, var_name, unit, confidence=0.95):
a = 1.0 * np.array(data)
n = len(a)
m, se = np.mean(a), scipy.stats.sem(a)
h = se * scipy.stats.t.ppf((1 confidence) / 2., n-1)
print(var_name, round(m, 5), "\u00B1", round(h, 5), unit )
def einsum_greedy(A, B, na, nb, nc, nd):
#res = np.zeros((na,nc,nd))
res = np.einsum('ab,bcd->acd', A, B, optimize="greedy")
return res
def einsum_standard(A, B, na, nb, nc, nd):
# res = np.zeros((na,nc,nd))
res = np.einsum('ab,bcd->acd', A, B)
return res
def btime_ABC(name_def, name_out, A, B, C, na, nb, nc, nd, n_times):
global opt_path
list_time = []
for i in range(n_times):
start_time = time.time()
C = name_def(A, B, na, nb, nc, nd)
finish_time = time.time()
list_time.append(finish_time - start_time)
mean_confidence_interval(list_time, name_out, 's' )
# A[a,b] * B[b,c,d] = C[a,c,d]
na = nb = nc = nd = dim_comm = 90
n_times = 60
print('number of common dimension', dim_comm)
print('number of averaged time', n_times)
A = np.random.random((na,nb))
B = np.random.random((nb,nc,nd))
C1 = np.zeros((na,nc,nd))
C2 = np.zeros((na,nc,nd))
btime_ABC(einsum_standard, 'einsum_standard', A, B, C1, na, nb, nc, nd, n_times)
btime_ABC(einsum_greedy, 'einsum_greedy', A, B, C2, na, nb, nc, nd, n_times)
我有
number of common dimension 90
number of averaged time 60
einsum_standard 0.04799 ± 0.00312 s
einsum_greedy 0.00805 ± 0.00137 s
該optimize標志有助于二進制收縮A[a,b] * B[b,c,d] = C[a,c,d]。那么為什么?
uj5u.com熱心網友回復:
我的時間:
In [26]: A = np.random.random((90,80))
In [27]: B = np.random.random((80,81,82))
In [28]: timeit np.einsum('ab,bcd->acd',A,B,optimize=False)
39.2 ms ± 1.51 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [29]: timeit np.einsum('ab,bcd->acd',A,B,optimize=True)
9.06 ms ± 70.5 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
查看einsum代碼,我看到,早期:
# If no optimization, run pure einsum
if optimize is False:
return c_einsum(*operands, **kwargs)
如果不是,它會檢查各種引數,并執行
operands, contraction_list = einsum_path(*operands, optimize=optimize,
einsum_call=True)
...
# Call tensordot if still possible
if blas:
...
new_view = tensordot(*tmp_operands, axes=(tuple(left_pos), tuple(right_pos)))
由于我們只有 2 個引數并且path很簡單,我認為這種True情況只是:
In [30]: timeit np.tensordot(A,B,(1,0))
7.62 ms ± 609 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
也就是說,從過去的研究tensordot:
In [31]: timeit ([email protected](80,-1)).reshape(90,81,82)
6.44 ms ± 116 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
所以基本上時間差是運行一個編譯的“純 einsum”,和一個將其視為matmul問題的替代方案,它可以使用優化的BLAS例程。[29] 時間似乎是 [31] 時間加上一些開銷。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/392561.html
