プリンキピア

第22章付録:深層学習のための数学

22.4.7. A Little Matrix Calculus22.4.7 少しの行列微積分

Derivatives of functions involving matrices turn out to be particularly nice. This section can become notationally heavy, so may be skipped in a first reading, but it is useful to know how derivatives of functions involving common matrix operations are often much cleaner than one might initially anticipate, particularly given how central matrix operations are to deep learning applications.
英語のヒント
行列を含む関数の導関数は、とくにきれいになることがわかる。この節は記法が重くなることがあるので、最初に読むときは飛ばしてもよいが、よくある行列演算を含む関数の導関数が、はじめに予想するよりもずっとすっきりしていることが多いことを知っておくと役に立つ。行列演算が深層学習の応用にとってどれほど中心的であるかを考えればなおさらである。
Let’s begin with an example. Suppose that we have some fixed column vector 𝜷, and we want to take the product function f(𝐱)=𝜷⊤𝐱, and understand how the dot product changes when we change 𝐱.
英語のヒント
例から始めよう。ある固定された列ベクトル𝜷があり、積の関数f(𝐱)=𝜷⊤𝐱をとって、𝐱を変えたときに内積がどう変わるかを理解したいとする。
A bit of notation that will be useful when working with matrix derivatives in ML is called the denominator layout matrix derivative where we assemble our partial derivatives into the shape of whatever vector, matrix, or tensor is in the denominator of the differential. In this case, we will write
英語のヒント
MLで行列の導関数を扱うときに役立つ記法の一つは、分母レイアウトの行列導関数と呼ばれるもので、偏導関数を、微分の分母にあるベクトル、行列、テンソルの形に合わせて並べる。この場合は次のように書く。
dfd𝐱=[dfdx1⋮dfdxn],(22.4.34)
where we matched the shape of the column vector 𝐱.
英語のヒント
ここで列ベクトル𝐱の形に合わせた。
If we write out our function into components this is
英語のヒント
関数を成分で書き出すと、次のようになる。
f(𝐱)=∑i=1nβixi=β1x1+⋯+βnxn.(22.4.35)
If we now take the partial derivative with respect to say β1, note that everything is zero but the first term, which is just x1 multiplied by β1, so we obtain that
英語のヒント
ここで、たとえばβ1について偏導関数をとると、最初の項以外はすべて0であり、最初の項はただx1にβ1を掛けたものなので、次が得られることに注意しよう。
dfdx1=β1,(22.4.36)
or more generally that
英語のヒント
あるいはもっと一般に、次が得られる。
dfdxi=βi.(22.4.37)
We can now reassemble this into a matrix to see
英語のヒント
これを行列にまとめ直すと、次がわかる。
dfd𝐱=[dfdx1⋮dfdxn]=[β1⋮βn]=𝜷.(22.4.38)
This illustrates a few factors about matrix calculus that we will often counter throughout this section:
英語のヒント
これは、この節を通じてよく出会う、行列微積分についてのいくつかの要素を示している。
  • First, The computations will get rather involved.
    英語のヒント
    第一に、計算はかなり込み入ったものになる。
  • Second, The final results are much cleaner than the intermediate process, and will always look similar to the single variable case. In this case, note that ddx(bx)=b and dd𝐱(𝜷⊤𝐱)=𝜷 are both similar.
    英語のヒント
    第二に、最終的な結果は途中の過程よりずっとすっきりしており、いつも1変数の場合に似て見える。この場合、ddx(bx)=bとdd𝐱(𝜷⊤𝐱)=𝜷がどちらも似ていることに注意しよう。
  • Third, transposes can often appear seemingly from nowhere. The core reason for this is the convention that we match the shape of the denominator, thus when we multiply matrices, we will need to take transposes to match back to the shape of the original term.
    英語のヒント
    第三に、転置がどこからともなく現れるように見えることがよくある。その中核の理由は、分母の形に合わせるという約束にあり、そのため行列を掛けるときには、もとの項の形に戻すために転置をとる必要がある。
To keep building intuition, let’s try a computation that is a little harder. Suppose that we have a column vector 𝐱, and a square matrix A and we want to compute
英語のヒント
直観を築き続けるため、もう少し難しい計算を試そう。列ベクトル𝐱と正方行列Aがあり、次を計算したいとする。
dd𝐱(𝐱⊤A𝐱).(22.4.39)
To drive towards easier to manipulate notation, let’s consider this problem using Einstein notation. In this case we can write the function as
英語のヒント
扱いやすい記法に向かうため、アインシュタインの縮約記法を使ってこの問題を考えよう。この場合、関数は次のように書ける。
𝐱⊤A𝐱=xiaijxj.(22.4.40)
To compute our derivative, we need to understand for every k, what is the value of
英語のヒント
導関数を計算するには、すべてのkについて、次の値が何かを理解する必要がある。
ddxk(𝐱⊤A𝐱)=ddxkxiaijxj.(22.4.41)
By the product rule, this is
英語のヒント
積の規則により、これは次のようになる。
ddxkxiaijxj=dxidxkaijxj+xiaijdxjdxk.(22.4.42)
For a term like dxidxk, it is not hard to see that this is one when i=k and zero otherwise. This means that every term where i and k are different vanish from this sum, so the only terms that remain in that first sum are the ones where i=k. The same reasoning holds for the second term where we need j=k. This gives
英語のヒント
dxidxkのような項については、これがi=kのとき1で、そうでなければ0であることは難しくなくわかる。これは、iとkが異なる項はすべてこの和から消えることを意味するので、最初の和に残る項はi=kのものだけである。同じ推論が、j=kが必要な2番目の項にも成り立つ。これにより次が得られる。
ddxkxiaijxj=akjxj+xiaik.(22.4.43)
Now, the names of the indices in Einstein notation are arbitrary—the fact that i and j are different is immaterial to this computation at this point, so we can re-index so that they both use i to see that
英語のヒント
さて、アインシュタインの縮約記法での添字の名前は任意である。iとjが異なるという事実は、この時点のこの計算には関係ないので、どちらもiを使うように添字をつけ直して、次がわかる。
ddxkxiaijxj=akixi+xiaik=(aki+aik)xi.(22.4.44)
Now, here is where we start to need some practice to go further. Let’s try and identify this outcome in terms of matrix operations. aki+aik is the k,i-th component of 𝐀+𝐀⊤. This gives
英語のヒント
ここからが、先へ進むのに練習が必要になり始めるところである。この結果を行列演算によって見分けてみよう。aki+aikは𝐀+𝐀⊤のk,i番目の成分である。これにより次が得られる。
ddxkxiaijxj=[𝐀+𝐀⊤]kixi.(22.4.45)
Similarly, this term is now the product of the matrix 𝐀+𝐀⊤ by the vector 𝐱, so we see that
英語のヒント
同様に、この項は今や行列𝐀+𝐀⊤とベクトル𝐱の積なので、次がわかる。
[dd𝐱(𝐱⊤A𝐱)]k=ddxkxiaijxj=[(𝐀+𝐀⊤)𝐱]k.(22.4.46)
Thus, we see that the k-th entry of the desired derivative from (22.4.39) is just the k-th entry of the vector on the right, and thus the two are the same. Thus yields
英語のヒント
したがって、式(22.4.39)で求める導関数のk番目の成分は、右辺のベクトルのk番目の成分にほかならず、両者は同じである。こうして次が得られる。
dd𝐱(𝐱⊤A𝐱)=(𝐀+𝐀⊤)𝐱.(22.4.47)
This required significantly more work than our last one, but the final result is small. More than that, consider the following computation for traditional single variable derivatives:
英語のヒント
これには前のものよりかなり多くの作業が必要だったが、最終的な結果は小さい。それだけでなく、従来の1変数の導関数について次の計算を考えよう。
ddx(xax)=dxdxax+xadxdx=(a+a)x.(22.4.48)
Equivalently ddx(ax2)=2ax=(a+a)x. Again, we get a result that looks rather like the single variable result but with a transpose tossed in.
英語のヒント
同じことだがddx(ax2)=2ax=(a+a)xである。ここでも、1変数の結果によく似ているが、転置が一つ投げ込まれた結果が得られる。
At this point, the pattern should be looking rather suspicious, so let’s try to figure out why. When we take matrix derivatives like this, let’s first assume that the expression we get will be another matrix expression: an expression we can write it in terms of products and sums of matrices and their transposes. If such an expression exists, it will need to be true for all matrices. In particular, it will need to be true of 1×1 matrices, in which case the matrix product is just the product of the numbers, the matrix sum is just the sum, and the transpose does nothing at all! In other words, whatever expression we get must match the single variable expression. This means that, with some practice, one can often guess matrix derivatives just by knowing what the associated single variable expression must look like!
英語のヒント
この時点で、このパターンはかなり怪しく見えてくるはずなので、その理由を突き止めてみよう。このように行列の導関数をとるときは、まず、得られる式がまた別の行列の式、つまり行列とその転置の積と和で書ける式になると仮定しよう。そのような式が存在するなら、それはすべての行列について成り立つ必要がある。とくに、1×1行列についても成り立つ必要があり、その場合、行列の積はただの数の積、行列の和はただの和であり、転置は何もしない。言い換えれば、得られる式は何であれ1変数の式に一致しなければならない。これは、少し練習すれば、対応する1変数の式がどう見えるはずかを知っているだけで、行列の導関数を推測できることが多いことを意味する。
Let’s try this out. Suppose that 𝐗 is a n×m matrix, 𝐔 is an n×r and 𝐕 is an r×m. Let’s try to compute
英語のヒント
試してみよう。𝐗がn×m行列、𝐔がn×r行列、𝐕がr×m行列だとする。次を計算してみよう。
dd𝐕∥𝐗−𝐔𝐕∥22=?(22.4.49)
This computation is important in an area called matrix factorization. For us, however, it is just a derivative to compute. Let’s try to imagine what this would be for 1×1 matrices. In that case, we get the expression
英語のヒント
この計算は、行列分解と呼ばれる分野で重要である。しかし、ここではただ計算すべき導関数にすぎない。1×1行列ならこれがどうなるかを想像してみよう。その場合、次の式が得られる。
ddv(x−uv)2=−2(x−uv)u,(22.4.50)
where, the derivative is rather standard. If we try to convert this back into a matrix expression we get
英語のヒント
ここで、導関数はごく標準的なものである。これを行列の式に戻そうとすると、次が得られる。
dd𝐕∥𝐗−𝐔𝐕∥22=−2(𝐗−𝐔𝐕)𝐔.(22.4.51)
However, if we look at this it does not quite work. Recall that 𝐗 is n×m, as is 𝐔𝐕, so the matrix 2(𝐗−𝐔𝐕) is n×m. On the other hand 𝐔 is n×r, and we cannot multiply a n×m and a n×r matrix since the dimensions do not match!
英語のヒント
しかし、これを見ると、うまくいっていないことがわかる。𝐗はn×mであり、𝐔𝐕も同じなので、行列2(𝐗−𝐔𝐕)はn×mであることを思い出そう。一方、𝐔はn×rであり、次元が合わないので、n×m行列とn×r行列は掛けられない。
We want to get dd𝐕, which is the same shape as 𝐕, which is r×m. So somehow we need to take a n×m matrix and a n×r matrix, multiply them together (perhaps with some transposes) to get a r×m. We can do this by multiplying U⊤ by (𝐗−𝐔𝐕). Thus, we can guess the solution to (22.4.49) is
英語のヒント
dd𝐕を得たいのであり、これは𝐕と同じ形、つまりr×mである。そこで、何とかしてn×m行列とn×r行列をとり、(おそらく転置を使って)掛け合わせてr×mを得る必要がある。これは、U⊤に(𝐗−𝐔𝐕)を掛ければできる。したがって、式(22.4.49)の解は次のようになると推測できる。
dd𝐕∥𝐗−𝐔𝐕∥22=−2𝐔⊤(𝐗−𝐔𝐕).(22.4.52)
To show that this works, we would be remiss to not provide a detailed computation. If we already believe that this rule-of-thumb works, feel free to skip past this derivation. To compute
英語のヒント
これがうまくいくことを示すには、詳しい計算を示さないのは怠慢というものだろう。この経験則がうまくいくとすでに信じているなら、この導出は気軽に飛ばしてよい。次を計算するには、
dd𝐕∥𝐗−𝐔𝐕∥22,(22.4.53)
we must find for every a, and b
英語のヒント
すべてのaとbについて、次を求める必要がある。
ddvab∥𝐗−𝐔𝐕∥22=ddvab∑i,j(xij−∑kuikvkj)2.(22.4.54)
Recalling that all entries of 𝐗 and 𝐔 are constants as far as ddvab is concerned, we may push the derivative inside the sum, and apply the chain rule to the square to get
英語のヒント
ddvabにとっては𝐗と𝐔のすべての成分が定数であることを思い出すと、導関数を和の内側に押し込み、2乗に連鎖律を適用して、次が得られる。
ddvab∥𝐗−𝐔𝐕∥22=∑i,j2(xij−∑kuikvkj)(−∑kuikdvkjdvab).(22.4.55)
As in the previous derivation, we may note that dvkjdvab is only non-zero if the k=a and j=b. If either of those conditions do not hold, the term in the sum is zero, and we may freely discard it. We see that
英語のヒント
前の導出と同じように、dvkjdvabが0でないのはk=aかつj=bのときだけであることに注意しよう。どちらかの条件が成り立たなければ、和の中の項は0なので、自由に捨てられる。次がわかる。
ddvab∥𝐗−𝐔𝐕∥22=−2∑i(xib−∑kuikvkb)uia.(22.4.56)
An important subtlety here is that the requirement that k=a does not occur inside the inner sum since that k is a dummy variable which we are summing over inside the inner term. For a notationally cleaner example, consider why
英語のヒント
ここでの重要な微妙な点は、k=aという条件が内側の和の中には現れないことである。そのkは内側の項の中で和をとっているダミー変数だからである。記法の上でもっとすっきりした例として、次がなぜ成り立つかを考えよう。
ddx1(∑ixi)2=2(∑ixi).(22.4.57)
From this point, we may start identifying components of the sum. First,
英語のヒント
ここから、和の構成要素を見分け始められる。まず、
∑kuikvkb=[𝐔𝐕]ib.(22.4.58)
So the entire expression in the inside of the sum is
英語のヒント
したがって、和の内側の式全体は次のようになる。
xib−∑kuikvkb=[𝐗−𝐔𝐕]ib.(22.4.59)
This means we may now write our derivative as
英語のヒント
これは、導関数を次のように書けることを意味する。
ddvab∥𝐗−𝐔𝐕∥22=−2∑i[𝐗−𝐔𝐕]ibuia.(22.4.60)
We want this to look like the a,b element of a matrix so we can use the technique as in the previous example to arrive at a matrix expression, which means that we need to exchange the order of the indices on uia. If we notice that uia=[𝐔⊤]ai, we can then write
英語のヒント
これが行列のa,b要素のように見えるようにして、前の例と同じ技法を使って行列の式にたどり着きたい。そのためには、uiaの添字の順序を入れ替える必要がある。uia=[𝐔⊤]aiであることに気づけば、次のように書ける。
ddvab∥𝐗−𝐔𝐕∥22=−2∑i[𝐔⊤]ai[𝐗−𝐔𝐕]ib.(22.4.61)
This is a matrix product, and thus we can conclude that
英語のヒント
これは行列の積なので、次のように結論できる。
ddvab∥𝐗−𝐔𝐕∥22=−2[𝐔⊤(𝐗−𝐔𝐕)]ab.(22.4.62)
and thus we may write the solution to (22.4.49)
英語のヒント
したがって、式(22.4.49)の解は次のように書ける。
dd𝐕∥𝐗−𝐔𝐕∥22=−2𝐔⊤(𝐗−𝐔𝐕).(22.4.63)
This matches the solution we guessed above!
英語のヒント
これは、上で推測した解と一致する。
It is reasonable to ask at this point, “Why can I not just write down matrix versions of all the calculus rules I have learned? It is clear this is still mechanical. Why do we not just get it over with!” And indeed there are such rules and (Petersen and Pedersen, 2008) provides an excellent summary. However, due to the plethora of ways matrix operations can be combined compared to single values, there are many more matrix derivative rules than single variable ones. It is often the case that it is best to work with the indices, or leave it up to automatic differentiation when appropriate.
英語のヒント
この時点で、「なぜ、学んだ微積分の規則をすべて行列版で書き下すだけではいけないのか。これがまだ機械的なのは明らかだ。さっさと片づけてしまえばいいではないか」と問うのはもっともである。そして実際、そのような規則は存在し、(Petersen and Pedersen, 2008)がすばらしいまとめを与えている。しかし、1つの値と比べて、行列演算を組み合わせるやり方はおびただしくあるため、行列の導関数の規則は1変数の規則よりはるかに多い。添字を使って作業するか、適切なら自動微分に任せるのが最善であることが多い。