3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

numpy行列積 メモ

Posted at

行列積周りで混乱したところをメモ

#画像の畳み込み計算
imgのテンソルとkernelのテンソルの積にnp.tensordotを使う。引数のaxisは1つの画像が格納されている次元(1,2)を指定する。

img_tensor = [[[0, 0],
               [0, 0]],
              [[1, 1],
               [1, 1]],
              [[3, 3],
               [3, 3]]]
    kernel = [[[0, 0],
               [0, 0]],
              [[1, 1],
               [1, 1]],
              [[2, 2],
               [2, 2]]]
    out = np.tensordot(img_tensor, kernel, axes=((1, 2), (1, 2)))
    print(out)
    print(np.shape(img_tensor), np.shape(kernel), '=>', np.shape(out))

-----------------
[[ 0  0  0]
 [ 0  4  8]
 [ 0 12 24]]
((3L, 2L, 2L), (3L, 2L, 2L), '=>', (3L, 3L))

#データ間の内積をまとめてやりたい

$({\bf i,j})$と$({\bf k,l})$から$({\bf i}\cdot{\bf k},~{\bf j}\cdot{\bf l})$を計算したい場合、アダマール積とnp.sumで各要素の内積を計算する。(もっといいやり方ある?)

a = np.arange(4).reshape((2,2))
    b = a[::-1,::-1]
    c = np.sum(a*b,axis=1)
    print("---a---")
    print(a)
    print("---b---")
    print(b)
    print("---c---")
    print(c)

---a---
[[0 1]
 [2 3]]
---b---
[[3 2]
 [1 0]]
---c---
[2 2]
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?