プリンキピア

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

22.3.2. Rules of Calculus22.3.2 微積分の規則

We now turn to the task of understanding how to compute the derivative of an explicit function. A full formal treatment of calculus would derive everything from first principles. We will not indulge in this temptation here, but rather provide an understanding of the common rules encountered.
英語のヒント
次に、明示的な関数の導関数をどう計算するかを理解する作業に移る。微積分の完全で形式的な扱いなら、すべてを第一原理から導くだろう。ここではその誘惑にはふけらず、よく出会う規則の理解を与える。

22.3.2.1. Common Derivatives22.3.2.1 よく使う導関数

As was seen in Section 2.4, when computing derivatives one can oftentimes use a series of rules to reduce the computation to a few core functions. We repeat them here for ease of reference.
英語のヒント
2.4節で見たように、導関数を計算するときには、一連の規則を使って計算をいくつかの中核となる関数に帰着させられることが多い。参照しやすいように、ここでそれらを繰り返す。
  • Derivative of constants. ddxc=0.
    英語のヒント
    定数の導関数。ddxc=0。
  • Derivative of linear functions. ddx(ax)=a.
    英語のヒント
    1次関数の導関数。ddx(ax)=a。
  • Power rule. ddxxn=nxn−1.
    英語のヒント
    べき乗則。ddxxn=nxn−1。
  • Derivative of exponentials. ddxex=ex.
    英語のヒント
    指数関数の導関数。ddxex=ex。
  • Derivative of the logarithm. ddxlog⁡(x)=1x.
    英語のヒント
    対数の導関数。ddxlog⁡(x)=1x。

22.3.2.2. Derivative Rules22.3.2.2 導関数の規則

If every derivative needed to be separately computed and stored in a table, differential calculus would be near impossible. It is a gift of mathematics that we can generalize the above derivatives and compute more complex derivatives like finding the derivative of f(x)=log⁡(1+(x−1)10). As was mentioned in Section 2.4, the key to doing so is to codify what happens when we take functions and combine them in various ways, most importantly: sums, products, and compositions.
英語のヒント
すべての導関数を別々に計算して表に保存する必要があったら、微分法はほとんど不可能だろう。上の導関数を一般化して、f(x)=log⁡(1+(x−1)10)の導関数を求めるような、より複雑な導関数を計算できるのは、数学の恵みである。2.4節で述べたように、そのための鍵は、関数をとってさまざまなやり方で組み合わせたときに何が起きるかを体系化することである。最も重要なのは、和、積、合成である。
  • Sum rule. ddx(g(x)+h(x))=dgdx(x)+dhdx(x).
    英語のヒント
    和の規則。ddx(g(x)+h(x))=dgdx(x)+dhdx(x)。
  • Product rule. ddx(g(x)⋅h(x))=g(x)dhdx(x)+dgdx(x)h(x).
    英語のヒント
    積の規則。ddx(g(x)⋅h(x))=g(x)dhdx(x)+dgdx(x)h(x)。
  • Chain rule. ddxg(h(x))=dgdh(h(x))⋅dhdx(x).
    英語のヒント
    連鎖律。ddxg(h(x))=dgdh(h(x))⋅dhdx(x)。
Let’s see how we may use (22.3.6) to understand these rules. For the sum rule, consider following chain of reasoning:
英語のヒント
これらの規則を理解するのに式(22.3.6)をどう使えるかを見よう。和の規則については、次の推論の流れを考えよう。
f(x+ϵ)=g(x+ϵ)+h(x+ϵ)≈g(x)+ϵdgdx(x)+h(x)+ϵdhdx(x)=g(x)+h(x)+ϵ(dgdx(x)+dhdx(x))=f(x)+ϵ(dgdx(x)+dhdx(x)).(22.3.7)
By comparing this result with the fact that f(x+ϵ)≈f(x)+ϵdfdx(x), we see that dfdx(x)=dgdx(x)+dhdx(x) as desired. The intuition here is: when we change the input x, g and h jointly contribute to the change of the output by dgdx(x) and dhdx(x).
英語のヒント
この結果をf(x+ϵ)≈f(x)+ϵdfdx(x)という事実と比べると、望みどおりdfdx(x)=dgdx(x)+dhdx(x)であることがわかる。ここでの直観は次のとおりである。入力xを変えると、gとhが、dgdx(x)とdhdx(x)によって出力の変化にともに寄与する。
The product is more subtle, and will require a new observation about how to work with these expressions. We will begin as before using (22.3.6):
英語のヒント
積はもっと微妙で、こうした式の扱い方について新しい観察が必要になる。前と同じように、式(22.3.6)を使って始める。
f(x+ϵ)=g(x+ϵ)⋅h(x+ϵ)≈(g(x)+ϵdgdx(x))⋅(h(x)+ϵdhdx(x))=g(x)⋅h(x)+ϵ(g(x)dhdx(x)+dgdx(x)h(x))+ϵ2dgdx(x)dhdx(x)=f(x)+ϵ(g(x)dhdx(x)+dgdx(x)h(x))+ϵ2dgdx(x)dhdx(x).(22.3.8)
This resembles the computation done above, and indeed we see our answer (dfdx(x)=g(x)dhdx(x)+dgdx(x)h(x)) sitting next to ϵ, but there is the issue of that term of size ϵ2. We will refer to this as a higher-order term, since the power of ϵ2 is higher than the power of ϵ1. We will see in a later section that we will sometimes want to keep track of these, however for now observe that if ϵ=0.0000001, then ϵ2=0.0000000000001, which is vastly smaller. As we send ϵ→0, we may safely ignore the higher order terms. As a general convention in this appendix, we will use “≈” to denote that the two terms are equal up to higher order terms. However, if we wish to be more formal we may examine the difference quotient
英語のヒント
これは上で行った計算に似ており、実際、答え(dfdx(x)=g(x)dhdx(x)+dgdx(x)h(x))がϵの隣にあるのがわかるが、大きさがϵ2の項という問題がある。ϵ2のべきはϵ1のべきより高いので、これを高次の項と呼ぶ。後の節で、これらを追いかけておきたい場合もあることを見るが、今のところは、ϵ=0.0000001ならϵ2=0.0000000000001であり、はるかに小さいことに注目しよう。ϵ→0とすれば、高次の項は安全に無視できる。この付録での一般的な約束として、「≈」を、二つの項が高次の項を除いて等しいことを表すのに使う。しかし、もっと形式的にしたいなら、次の差分商を調べればよい。
f(x+ϵ)−f(x)ϵ=g(x)dhdx(x)+dgdx(x)h(x)+ϵdgdx(x)dhdx(x),(22.3.9)
and see that as we send ϵ→0, the right hand term goes to zero as well.
英語のヒント
そして、ϵ→0とすると、右辺の項も0に向かうことがわかる。
Finally, with the chain rule, we can again progress as before using (22.3.6) and see that
英語のヒント
最後に、連鎖律についても、前と同じように式(22.3.6)を使って進めると、次がわかる。
f(x+ϵ)=g(h(x+ϵ))≈g(h(x)+ϵdhdx(x))≈g(h(x))+ϵdhdx(x)dgdh(h(x))=f(x)+ϵdgdh(h(x))dhdx(x),(22.3.10)
where in the second line we view the function g as having its input (h(x)) shifted by the tiny quantity ϵdhdx(x).
英語のヒント
ここで2行目では、関数gを、その入力(h(x))がごく小さな量ϵdhdx(x)だけずれたものとみなしている。
These rule provide us with a flexible set of tools to compute essentially any expression desired. For instance,
英語のヒント
これらの規則は、望むほとんどどんな式でも計算できる柔軟な道具一式を与える。たとえば、
ddx[log⁡(1+(x−1)10)]=(1+(x−1)10)−1ddx[1+(x−1)10]=(1+(x−1)10)−1(ddx[1]+ddx[(x−1)10])=(1+(x−1)10)−1(0+10(x−1)9ddx[x−1])=10(1+(x−1)10)−1(x−1)9=10(x−1)91+(x−1)10.(22.3.11)
Where each line has used the following rules:
英語のヒント
ここで、各行では次の規則を使った。
  1. The chain rule and derivative of logarithm.
    英語のヒント
    連鎖律と対数の導関数。
  2. The sum rule.
    英語のヒント
    和の規則。
  3. The derivative of constants, chain rule, and power rule.
    英語のヒント
    定数の導関数、連鎖律、べき乗則。
  4. The sum rule, derivative of linear functions, derivative of constants.
    英語のヒント
    和の規則、1次関数の導関数、定数の導関数。
Two things should be clear after doing this example:
英語のヒント
この例をやってみると、二つのことがはっきりするはずである。
  1. Any function we can write down using sums, products, constants, powers, exponentials, and logarithms can have its derivate computed mechanically by following these rules.
    英語のヒント
    和、積、定数、べき乗、指数関数、対数を使って書き下せる関数なら、どんな関数でも、これらの規則に従えば機械的に導関数を計算できる。
  2. Having a human follow these rules can be tedious and error prone!
    英語のヒント
    人間にこれらの規則に従わせると、退屈で間違いやすくなりうる。
Thankfully, these two facts together hint towards a way forward: this is a perfect candidate for mechanization! Indeed backpropagation, which we will revisit later in this section, is exactly that.
英語のヒント
ありがたいことに、この二つの事実を合わせると、進むべき道が見えてくる。これは機械化のうってつけの候補である。実際、この節の後で再び取り上げる誤差逆伝播法がまさにそれである。

22.3.2.3. Linear Approximation22.3.2.3 線形近似

When working with derivatives, it is often useful to geometrically interpret the approximation used above. In particular, note that the equation
英語のヒント
導関数を扱うときには、上で使った近似を幾何学的に解釈すると役に立つことが多い。とくに、次の式は、
f(x+ϵ)≈f(x)+ϵdfdx(x),(22.3.12)
approximates the value of f by a line which passes through the point (x,f(x)) and has slope dfdx(x). In this way we say that the derivative gives a linear approximation to the function f, as illustrated below:
英語のヒント
fの値を、点(x,f(x))を通り傾きがdfdx(x)の直線で近似していることに注意しよう。このようにして、下に図示するように、導関数は関数fへの線形近似を与えるという。
# Compute sin
xs = torch.arange(-torch.pi, torch.pi, 0.01)
plots = [torch.sin(xs)]

# Compute some linear approximations. Use d(sin(x))/dx = cos(x)
for x0 in [-1.5, 0.0, 2.0]:
    plots.append(torch.sin(torch.tensor(x0)) + (xs - x0) *
                 torch.cos(torch.tensor(x0)))

d2l.plot(xs, plots, 'x', 'f(x)', ylim=[-1.5, 1.5])

22.3.2.4. Higher Order Derivatives22.3.2.4 高階導関数

Let’s now do something that may on the surface seem strange. Take a function f and compute the derivative dfdx. This gives us the rate of change of f at any point.
英語のヒント
では、表面上は奇妙に見えるかもしれないことをしよう。関数fをとり、導関数dfdxを計算する。これは任意の点でのfの変化率を与える。
However, the derivative, dfdx, can be viewed as a function itself, so nothing stops us from computing the derivative of dfdx to get d2fdx2=dfdx(dfdx). We will call this the second derivative of f. This function is the rate of change of the rate of change of f, or in other words, how the rate of change is changing. We may apply the derivative any number of times to obtain what is called the n-th derivative. To keep the notation clean, we will denote the n-th derivative as
英語のヒント
しかし、導関数dfdxはそれ自体を関数とみなせるので、dfdxの導関数を計算してd2fdx2=dfdx(dfdx)を得るのを妨げるものは何もない。これをfの2階導関数と呼ぶ。この関数はfの変化率の変化率、言い換えれば、変化率がどう変わっているかである。導関数は何回でも適用でき、n階導関数と呼ばれるものが得られる。記法をすっきりさせるため、n階導関数を次のように表す。
f(n)(x)=dnfdxn=(ddx)nf.(22.3.13)
Let’s try to understand why this is a useful notion. Below, we visualize f(2)(x), f(1)(x), and f(x).
英語のヒント
これがなぜ役に立つ概念なのかを理解しよう。以下では、f(2)(x)、f(1)(x)、f(x)を視覚化する。
First, consider the case that the second derivative f(2)(x) is a positive constant. This means that the slope of the first derivative is positive. As a result, the first derivative f(1)(x) may start out negative, becomes zero at a point, and then becomes positive in the end. This tells us the slope of our original function f and therefore, the function f itself decreases, flattens out, then increases. In other words, the function f curves up, and has a single minimum as is shown in Fig. 22.3.1.
英語のヒント
まず、2階導関数f(2)(x)が正の定数である場合を考えよう。これは、1階導関数の傾きが正であることを意味する。その結果、1階導関数f(1)(x)は負から始まり、ある点で0になり、最後には正になりうる。これはもとの関数fの傾きを表しているので、関数fそのものは減少し、平らになり、それから増加する。言い換えれば、図22.3.1に示すように、関数fは上に曲がり、ただ一つの最小値をもつ。
Fig. 22.3.1図22.3.1
If we assume the second derivative is a positive constant, then the fist derivative in increasing, which implies the function itself has a minimum.
英語のヒント
2階導関数が正の定数だと仮定すると、1階導関数は増加しており、それは関数そのものが最小値をもつことを意味する。
Second, if the second derivative is a negative constant, that means that the first derivative is decreasing. This implies the first derivative may start out positive, becomes zero at a point, and then becomes negative. Hence, the function f itself increases, flattens out, then decreases. In other words, the function f curves down, and has a single maximum as is shown in Fig. 22.3.2.
英語のヒント
次に、2階導関数が負の定数なら、それは1階導関数が減少していることを意味する。これは、1階導関数が正から始まり、ある点で0になり、それから負になりうることを意味する。したがって、関数fそのものは増加し、平らになり、それから減少する。言い換えれば、図22.3.2に示すように、関数fは下に曲がり、ただ一つの最大値をもつ。
Fig. 22.3.2図22.3.2
If we assume the second derivative is a negative constant, then the fist derivative in decreasing, which implies the function itself has a maximum.
英語のヒント
2階導関数が負の定数だと仮定すると、1階導関数は減少しており、それは関数そのものが最大値をもつことを意味する。
Third, if the second derivative is a always zero, then the first derivative will never change—it is constant! This means that f increases (or decreases) at a fixed rate, and f is itself a straight line as is shown in Fig. 22.3.3.
英語のヒント
三つ目に、2階導関数がいつも0なら、1階導関数は決して変わらない。定数なのである。これは、fが一定の割合で増加(または減少)し、図22.3.3に示すようにfそのものが直線であることを意味する。
Fig. 22.3.3図22.3.3
If we assume the second derivative is zero, then the fist derivative is constant, which implies the function itself is a straight line.
英語のヒント
2階導関数が0だと仮定すると、1階導関数は定数であり、それは関数そのものが直線であることを意味する。
To summarize, the second derivative can be interpreted as describing the way that the function f curves. A positive second derivative leads to a upwards curve, while a negative second derivative means that f curves downwards, and a zero second derivative means that f does not curve at all.
英語のヒント
まとめると、2階導関数は、関数fがどう曲がるかを表すものとして解釈できる。正の2階導関数は上に曲がる曲線につながり、負の2階導関数はfが下に曲がることを意味し、0の2階導関数はfがまったく曲がらないことを意味する。
Let’s take this one step further. Consider the function g(x)=ax2+bx+c. We can then compute that
英語のヒント
これをもう一歩進めよう。関数g(x)=ax2+bx+cを考える。すると次のように計算できる。
dgdx(x)=2ax+bd2gdx2(x)=2a.(22.3.14)
If we have some original function f(x) in mind, we may compute the first two derivatives and find the values for a,b, and c that make them match this computation. Similarly to the previous section where we saw that the first derivative gave the best approximation with a straight line, this construction provides the best approximation by a quadratic. Let’s visualize this for f(x)=sin⁡(x).
英語のヒント
もとの関数f(x)を念頭に置いているなら、最初の二つの導関数を計算して、それらがこの計算に一致するようなa,bとcの値を求められる。1階導関数が直線による最良の近似を与えることを見た前の節と同じように、この構成は2次式による最良の近似を与える。f(x)=sin⁡(x)についてこれを視覚化しよう。
# Compute sin
xs = torch.arange(-torch.pi, torch.pi, 0.01)
plots = [torch.sin(xs)]

# Compute some quadratic approximations. Use d(sin(x)) / dx = cos(x)
for x0 in [-1.5, 0.0, 2.0]:
    plots.append(torch.sin(torch.tensor(x0)) + (xs - x0) *
                 torch.cos(torch.tensor(x0)) - (xs - x0)**2 *
                 torch.sin(torch.tensor(x0)) / 2)

d2l.plot(xs, plots, 'x', 'f(x)', ylim=[-1.5, 1.5])
We will extend this idea to the idea of a Taylor series in the next section.
英語のヒント
次の節で、この考えをテイラー級数の考えへと広げる。

22.3.2.5. Taylor Series22.3.2.5 テイラー級数

The Taylor series provides a method to approximate the function f(x) if we are given values for the first n derivatives at a point x0, i.e., {f(x0),f(1)(x0),f(2)(x0),…,f(n)(x0)}. The idea will be to find a degree n polynomial that matches all the given derivatives at x0.
英語のヒント
テイラー級数は、点x0での最初のn個の導関数の値、つまり{f(x0),f(1)(x0),f(2)(x0),…,f(n)(x0)}が与えられたときに、関数f(x)を近似する方法を与える。考え方は、x0で与えられたすべての導関数に一致するn次の多項式を見つけることである。
We saw the case of n=2 in the previous section and a little algebra shows this is
英語のヒント
前の節でn=2の場合を見たが、少し代数計算をすると、これは次のようになることがわかる。
f(x)≈12d2fdx2(x0)(x−x0)2+dfdx(x0)(x−x0)+f(x0).(22.3.15)
As we can see above, the denominator of 2 is there to cancel out the 2 we get when we take two derivatives of x2, while the other terms are all zero. Same logic applies for the first derivative and the value itself.
英語のヒント
上でわかるように、分母の2は、x2を2回微分したときに出てくる2を打ち消すためにあり、ほかの項はすべて0である。1階導関数と値そのものにも同じ論理が当てはまる。
If we push the logic further to n=3, we will conclude that
英語のヒント
この論理をさらにn=3まで進めると、次が結論される。
f(x)≈d3fdx3(x0)6(x−x0)3+d2fdx2(x0)2(x−x0)2+dfdx(x0)(x−x0)+f(x0).(22.3.16)
where the 6=3×2=3! comes from the constant we get in front if we take three derivatives of x3.
英語のヒント
ここで6=3×2=3!は、x3を3回微分したときに前に出てくる定数から来ている。
Furthermore, we can get a degree n polynomial by
英語のヒント
さらに、次の式でn次の多項式が得られる。
Pn(x)=∑i=0nf(i)(x0)i!(x−x0)i.(22.3.17)
where the notation
英語のヒント
ここで記法は次のとおりである。
f(n)(x)=dnfdxn=(ddx)nf.(22.3.18)
Indeed, Pn(x) can be viewed as the best n-th degree polynomial approximation to our function f(x).
英語のヒント
実際、Pn(x)は、関数f(x)に対する最良のn次多項式近似とみなせる。
While we are not going to dive all the way into the error of the above approximations, it is worth mentioning the infinite limit. In this case, for well behaved functions (known as real analytic functions) like cos⁡(x) or ex, we can write out the infinite number of terms and approximate the exactly same function
英語のヒント
上の近似の誤差に深く入り込むことはしないが、無限の極限については述べておく価値がある。この場合、cos⁡(x)やexのような性質のよい関数(実解析関数として知られる)では、無限個の項を書き出して、まったく同じ関数を近似できる。
f(x)=∑n=0∞f(n)(x0)n!(x−x0)n.(22.3.19)
Take f(x)=ex as am example. Since ex is its own derivative, we know that f(n)(x)=ex. Therefore, ex can be reconstructed by taking the Taylor series at x0=0, i.e.,
英語のヒント
f(x)=exを例にとろう。exはそれ自身の導関数なので、f(n)(x)=exであることがわかる。したがって、exはx0=0でのテイラー級数をとることで再構成できる。つまり、
ex=∑n=0∞xnn!=1+x+x22+x36+⋯.(22.3.20)
Let’s see how this works in code and observe how increasing the degree of the Taylor approximation brings us closer to the desired function ex.
英語のヒント
これがコードでどう働くかを見て、テイラー近似の次数を上げると、望む関数exにどう近づくかを観察しよう。
# Compute the exponential function
xs = torch.arange(0, 3, 0.01)
ys = torch.exp(xs)

# Compute a few Taylor series approximations
P1 = 1 + xs
P2 = 1 + xs + xs**2 / 2
P5 = 1 + xs + xs**2 / 2 + xs**3 / 6 + xs**4 / 24 + xs**5 / 120

d2l.plot(xs, [ys, P1, P2, P5], 'x', 'f(x)', legend=[
    "Exponential", "Degree 1 Taylor Series", "Degree 2 Taylor Series",
    "Degree 5 Taylor Series"])
Taylor series have two primary applications:
英語のヒント
テイラー級数には主に二つの応用がある。
  1. Theoretical applications: Often when we try to understand a too complex function, using Taylor series enables us to turn it into a polynomial that we can work with directly.
    英語のヒント
    理論的な応用:複雑すぎる関数を理解しようとするとき、テイラー級数を使うと、それを直接扱える多項式に変えられることが多い。
  2. Numerical applications: Some functions like ex or cos⁡(x) are difficult for machines to compute. They can store tables of values at a fixed precision (and this is often done), but it still leaves open questions like “What is the 1000-th digit of cos⁡(1)?” Taylor series are often helpful to answer such questions.
    英語のヒント
    数値的な応用:exやcos⁡(x)のような関数は、機械にとって計算が難しい。決まった精度で値の表を保存しておくことはできる(そしてよくそうされる)が、それでも「cos⁡(1)の1000桁目は何か」のような問いは未解決のまま残る。こうした問いに答えるのに、テイラー級数が役立つことが多い。