まずnumpy.einsumはijk
などの「縮約記法」にて行列演算する関数です。
特殊な演算をするというわけではなく、簡単な表記で行列演算するための関数です。
参考:【einsum】アインシュタインの縮約記法のように使えるnumpyの関数。性能と使い方を解説。
次に...i,...i
の...
は省略(ellipsis
)記号です。これについては公式では以下のように説明されています。
To enable and control broadcasting, use an ellipsis. Default NumPy-style broadcasting is done by adding an ellipsis to the left of each term, like np.einsum('...ii->...i', a). To take the trace along the first and last axes, you can do np.einsum('i...i', a), or to do a matrix-matrix product with the left-most indices instead of rightmost, you can do np.einsum('ij...,jk...->ik...', a, b).
いわばnumpyにおけるブロードキャストを実現するための記号であり、いちいちijklm
みたいに記載しなくてもij...
のように添え字を自動で対応させることができるそうです。(といいながらよく分かっていません)
Python
1import numpy as np
2a = np.array([1,2,3])
3b = np.array([4,5,6])
4
5ret = np.einsum('i,i',a,b) # a,bのi番目同士をかけた結果の総和
6print(ret)
7ret = np.einsum('...i,...i',a,b) # 同じ結果。...のうまい例が思いつかない…
8print(ret)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/06 17:10