看起来您偶然发现了greedy
路径提供非最佳缩放的情况。
>>> path, desc = np.einsum_path('xyf,xtf,ytpf,fr->tpr', M, A, B, C, optimize="greedy");
>>> print(desc)
Complete contraction: xyf,xtf,ytpf,fr->tpr
Naive scaling: 6
Optimized scaling: 5
Naive FLOP count: 3.219e+10
Optimized FLOP count: 4.165e+08
Theoretical speedup: 77.299
Largest intermediate: 5.371e+06 elements
--------------------------------------------------------------------------
scaling current remaining
--------------------------------------------------------------------------
5 ytpf,xyf->xptf xtf,fr,xptf->tpr
4 xptf,xtf->ptf fr,ptf->tpr
4 ptf,fr->tpr tpr->tpr
>>> path, desc = np.einsum_path('xyf,xtf,ytpf,fr->tpr', M, A, B, C, optimize="optimal");
>>> print(desc)
Complete contraction: xyf,xtf,ytpf,fr->tpr
Naive scaling: 6
Optimized scaling: 4
Naive FLOP count: 3.219e+10
Optimized FLOP count: 2.744e+07
Theoretical speedup: 1173.425
Largest intermediate: 1.535e+05 elements
--------------------------------------------------------------------------
scaling current remaining
--------------------------------------------------------------------------
4 xtf,xyf->ytf ytpf,fr,ytf->tpr
4 ytf,ytpf->ptf fr,ptf->tpr
4 ptf,fr->tpr tpr->tpr
使用np.einsum('xyf,xtf,ytpf,fr->tpr', M, A, B, C, optimize="optimal")
应该让您以最佳性能运行。我可以看看这个边缘,看看贪婪是否可以抓住它。