プリンキピア

第2章準備

2.3.10. Matrix–Matrix Multiplication2.3.10 行列と行列の積

Once you have gotten the hang of dot products and matrix–vector products, then matrix–matrix multiplication should be straightforward.
英語のヒント
内積と、行列とベクトルの積のこつがつかめたら、行列どうしの積は簡単なはずである。
Say that we have two matrices 𝐀∈ℝn×k and 𝐁∈ℝk×m:
英語のヒント
二つの行列𝐀∈ℝn×kと𝐁∈ℝk×mがあるとしよう。
𝐀=[a11a12⋯a1ka21a22⋯a2k⋮⋮⋱⋮an1an2⋯ank],𝐁=[b11b12⋯b1mb21b22⋯b2m⋮⋮⋱⋮bk1bk2⋯bkm].(2.3.7)
Let 𝐚i⊤∈ℝk denote the row vector representing the ith row of the matrix 𝐀 and let 𝐛j∈ℝk denote the column vector from the jth column of the matrix 𝐁:
英語のヒント
𝐚i⊤∈ℝkが行列𝐀のith行目を表す行ベクトル、𝐛j∈ℝkが行列𝐁のjth列目からとった列ベクトルを表すとしよう。
𝐀=[𝐚1⊤𝐚2⊤⋮𝐚n⊤],𝐁=[𝐛1𝐛2⋯𝐛m].(2.3.8)
To form the matrix product 𝐂∈ℝn×m, we simply compute each element cij as the dot product between the ith row of 𝐀 and the jth column of 𝐁, i.e., 𝐚i⊤𝐛j:
英語のヒント
行列の積𝐂∈ℝn×mを作るには、各要素cijを、𝐀のith行目と𝐁のjth列目の内積、つまり𝐚i⊤𝐛jとして計算すればよい。
𝐂=𝐀𝐁=[𝐚1⊤𝐚2⊤⋮𝐚n⊤][𝐛1𝐛2⋯𝐛m]=[𝐚1⊤𝐛1𝐚1⊤𝐛2⋯𝐚1⊤𝐛m𝐚2⊤𝐛1𝐚2⊤𝐛2⋯𝐚2⊤𝐛m⋮⋮⋱⋮𝐚n⊤𝐛1𝐚n⊤𝐛2⋯𝐚n⊤𝐛m].(2.3.9)
We can think of the matrix–matrix multiplication 𝐀𝐁 as performing m matrix–vector products or m×n dot products and stitching the results together to form an n×m matrix. In the following snippet, we perform matrix multiplication on A and B. Here, A is a matrix with two rows and three columns, and B is a matrix with three rows and four columns. After multiplication, we obtain a matrix with two rows and four columns.
英語のヒント
行列どうしの積𝐀𝐁は、m回の行列とベクトルの積、あるいはm×n回の内積を行い、その結果をつなぎ合わせてn×mの行列を作ることと考えられる。次のコードの断片では、AとBの行列の積を計算する。ここでAは2行3列の行列、Bは3行4列の行列である。掛け合わせると、2行4列の行列が得られる。
B = torch.ones(3, 4)
torch.mm(A, B), A@B
(tensor([[ 3.,  3.,  3.,  3.],
         [12., 12., 12., 12.]]),
 tensor([[ 3.,  3.,  3.,  3.],
         [12., 12., 12., 12.]]))
The term matrix–matrix multiplication is often simplified to matrix multiplication, and should not be confused with the Hadamard product.
英語のヒント
行列どうしの積という言葉は、しばしば行列の積と簡略化される。アダマール積と混同してはならない。