合計されたインデックスとそうでないインデックスが繰り返される場合、配列操作を行う最良の方法は何ですか? これらの操作に使用する必要があるようですが、整列されているが合計されていないディメンションのフラグを使用 einsum
する代替手段があればより良いでしょう。tensordot
一部の軸を合計せずに整列できることを除いて、テンソルドットのように動作する高速数値ルーチン(おそらくlapackで?)を知っている人はいますか?
==
必要な配列操作のタイプを示すコード例を次に示します。私が必要とする操作はmethod_sum
、 、method_einsum
、およびによって行われmethod_matmul
ます。同様の演算は、一致する j 軸で合計され、 および によって実行されmethod2_einsum
ますmethod2_tensordot
。
時間を比較すると、最初の問題tensordot
を打ち負かすことができるはずです。einsum
ただし、軸を合計せずに軸を揃える機能はありません。
#import scipy
import scipy as sp
# Shapes of arrays
I = 200
J = 50
K = 200
L = 100
a = sp.ones((I, J, L))
b = sp.ones((J, K, L))
# The desired product has a sum over the l-axis
## Use broadcasting to multiply and sum over the last dimension
def method_sum(a, b):
"Multiply arrays and sum over last dimension."
c = (a[:, :, None, :] * b[None, :, :, :]).sum(-1)
return c
## Use einsum to multiply arrays and sum over the l-axis
def method_einsum(a, b):
"Multiply arrays and sum over last dimension."
c = sp.einsum('ijl,jkl->ijk', a, b)
return c
## Use matmul to multiply arrays and sum over one of the axes
def method_matmul(a, b):
"Multiply arrays using the new matmul operation."
c = sp.matmul(a[:, :, None, None, :],
b[None, :, :, :, None])[:, :, :, 0, 0]
return c
# Compare einsum vs tensordot on summation over j and l
## Einsum takes about the same amount of time when j is not summed over)
def method2_einsum(a, b):
"Multiply arrays and sum over last dimension."
c = sp.einsum('ijl,jkl->ik', a, b)
return c
## Tensor dot can do this faster but it always sums over the aligned axes
def method2_tensordot(a, b):
"Multiply and sum over all overlapping dimensions."
c = sp.tensordot(a, b, axes=[(1, 2,), (0, 2,)])
return c
ここに、私のコンピューターでのさまざまなルーチンのいくつかの時間があります。method2
Tensordot は、複数のコアを使用するため、einsum を上回ることができます。tensordot
J 軸と L 軸の両方が揃っているが、L 軸のみが合計される計算のようなパフォーマンスを実現したいと考えています。
Time for method_sum:
1 loops, best of 3: 744 ms per loop
Time for method_einsum:
10 loops, best of 3: 95.1 ms per loop
Time for method_matmul:
10 loops, best of 3: 93.8 ms per loop
Time for method2_einsum:
10 loops, best of 3: 90.4 ms per loop
Time for method2_tensordot:
100 loops, best of 3: 10.9 ms per loop