第22章付録:深層学習のための数学
22.3.1. Differential Calculus22.3.1 微分法
Differential calculus is fundamentally the study of how functions behave under small changes. To see why this is so core to deep learning, let’s consider an example.
英語のヒント
微分法は、根本的には、関数が小さな変化のもとでどうふるまうかの研究である。これがなぜ深層学習の核心なのかを見るため、例を考えよう。
Suppose that we have a deep neural network where the weights are, for convenience, concatenated into a single vector . Given a training dataset, we consider the loss of our neural network on this dataset, which we will write as .
英語のヒント
深層ニューラルネットワークがあり、便宜のため、その重みが一つのベクトルに連結されているとする。訓練データセットが与えられたとき、このデータセットでのニューラルネットワークの損失を考え、それをと書く。
This function is extraordinarily complex, encoding the performance of all possible models of the given architecture on this dataset, so it is nearly impossible to tell what set of weights will minimize the loss. Thus, in practice, we often start by initializing our weights randomly, and then iteratively take small steps in the direction which makes the loss decrease as rapidly as possible.
英語のヒント
この関数は並外れて複雑で、与えられた構造のありうるすべてのモデルのこのデータセットでの性能を符号化しているので、どんな重みの組が損失を最小にするかを言い当てるのはほとんど不可能である。そのため実際には、重みをランダムに初期化することから始め、それから損失ができるだけ速く減る方向に小さな一歩を繰り返し進めることが多い。
The question then becomes something that on the surface is no easier: how do we find the direction which makes the weights decrease as quickly as possible? To dig into this, let’s first examine the case with only a single weight: for a single real value .
英語のヒント
すると問題は、表面上はちっとも簡単でないものになる。重みができるだけ速く減る方向をどう見つけるか。これを掘り下げるため、まず重みが一つだけの場合、つまりある一つの実数値についての場合を調べよう。
Let’s take and try to understand what happens when we change it by a small amount to . If you wish to be concrete, think a number like . To help us visualize what happens, let’s graph an example function, , over the .
英語のヒント
をとり、それを少しだけ変えてにしたときに何が起きるかを理解しよう。具体的にしたいなら、のような数を考えればよい。何が起きるかを視覚化するため、例の関数をの範囲でグラフにしよう。
%matplotlib inline
import torch
from IPython import display
from d2l import torch as d2l
torch.pi = torch.acos(torch.zeros(1)).item() * 2 # Define pi in torch
# Plot a function in a normal range
x_big = torch.arange(0.01, 3.01, 0.01)
ys = torch.sin(x_big**x_big)
d2l.plot(x_big, ys, 'x', 'f(x)')At this large scale, the function’s behavior is not simple. However, if we reduce our range to something smaller like , we see that the graph becomes much simpler.
英語のヒント
この大きな尺度では、関数のふるまいは単純ではない。しかし、範囲をのようにもっと小さく絞ると、グラフはずっと単純になることがわかる。
# Plot a the same function in a tiny range
x_med = torch.arange(1.75, 2.25, 0.001)
ys = torch.sin(x_med**x_med)
d2l.plot(x_med, ys, 'x', 'f(x)')Taking this to an extreme, if we zoom into a tiny segment, the behavior becomes far simpler: it is just a straight line.
英語のヒント
これを極端まで進めて、ごく小さな区間に拡大すると、ふるまいははるかに単純になる。ただの直線である。
# Plot a the same function in a tiny range
x_small = torch.arange(2.0, 2.01, 0.0001)
ys = torch.sin(x_small**x_small)
d2l.plot(x_small, ys, 'x', 'f(x)')This is the key observation of single variable calculus: the behavior of familiar functions can be modeled by a line in a small enough range. This means that for most functions, it is reasonable to expect that as we shift the value of the function by a little bit, the output will also be shifted by a little bit. The only question we need to answer is, “How large is the change in the output compared to the change in the input? Is it half as large? Twice as large?”
英語のヒント
これが1変数の微積分の鍵となる観察である。なじみのある関数のふるまいは、十分小さな範囲では直線でモデル化できる。これは、たいていの関数について、関数のの値を少しずらすと、出力も少しずれると期待するのがもっともだということを意味する。答える必要があるただ一つの問いは、「入力の変化と比べて、出力の変化はどれくらい大きいか。半分か。2倍か」である。
Thus, we can consider the ratio of the change in the output of a function for a small change in the input of the function. We can write this formally as
英語のヒント
そこで、関数の入力の小さな変化に対する、関数の出力の変化の比を考えられる。これは形式的には次のように書ける。
(22.3.1)
This is already enough to start to play around with in code. For instance, suppose that we know that , then we can see how large this value is at the point as follows.
英語のヒント
これだけで、コードでいろいろ試し始めるのに十分である。たとえば、だとわかっているとすると、点でこの値がどれくらいの大きさかを次のように見られる。
# Define our function
def L(x):
return x**2 + 1701*(x-4)**3
# Print the difference divided by epsilon for several epsilon
for epsilon in [0.1, 0.001, 0.0001, 0.00001]:
print(f'epsilon = {epsilon:.5f} -> {(L(4+epsilon) - L(4)) / epsilon:.5f}')epsilon = 0.10000 -> 25.11000
epsilon = 0.00100 -> 8.00270
epsilon = 0.00010 -> 8.00012
epsilon = 0.00001 -> 8.00001Now, if we are observant, we will notice that the output of this number is suspiciously close to . Indeed, if we decrease , we will see value becomes progressively closer to . Thus we may conclude, correctly, that the value we seek (the degree a change in the input changes the output) should be at the point . The way that a mathematician encodes this fact is
英語のヒント
さて、注意深く見ていれば、この数の出力が怪しいほどに近いことに気づくだろう。実際、を小さくしていくと、値がだんだんに近づくのがわかる。したがって、求めている値(入力の変化が出力をどの程度変えるか)は、点ではであるはずだと、正しく結論できる。数学者がこの事実を表す書き方は次のとおりである。
(22.3.2)
As a bit of a historical digression: in the first few decades of neural network research, scientists used this algorithm (the method of finite differences) to evaluate how a loss function changed under small perturbation: just change the weights and see how the loss changed. This is computationally inefficient, requiring two evaluations of the loss function to see how a single change of one variable influenced the loss. If we tried to do this with even a paltry few thousand parameters, it would require several thousand evaluations of the network over the entire dataset! It was not solved until 1986 that the backpropagation algorithm introduced in Rumelhart et al. (1988) provided a way to calculate how any change of the weights together would change the loss in the same computation time as a single prediction of the network over the dataset.
英語のヒント
少し歴史の脇道にそれよう。ニューラルネットワークの研究の最初の数十年間、科学者はこのアルゴリズム(有限差分法)を使って、小さな摂動のもとで損失関数がどう変わるかを評価していた。重みを変えて、損失がどう変わるかを見るだけである。これは計算の点で非効率で、一つの変数の一つの変化が損失にどう影響したかを見るのに、損失関数を2回評価する必要がある。わずか数千のパラメータでこれをしようとしただけでも、データセット全体でのネットワークの評価が数千回必要になる。この問題は1986年まで解決されず、Rumelhart et al.(1988)で紹介された誤差逆伝播のアルゴリズムが、重みのどんな変化の組み合わせが損失をどう変えるかを、データセットでのネットワークの1回の予測と同じ計算時間で計算する方法を与えた。
Back in our example, this value is different for different values of , so it makes sense to define it as a function of . More formally, this value dependent rate of change is referred to as the derivative which is written as
英語のヒント
例に戻ると、この値はの値によって異なるので、これをの関数として定義するのが理にかなっている。より形式的には、この値に依存する変化率は導関数と呼ばれ、次のように書かれる。
(22.3.3)
Different texts will use different notations for the derivative. For instance, all of the below notations indicate the same thing:
英語のヒント
本によって、導関数に異なる記法を使う。たとえば、次の記法はどれも同じものを表す。
(22.3.4)
Most authors will pick a single notation and stick with it, however even that is not guaranteed. It is best to be familiar with all of these. We will use the notation throughout this text, unless we want to take the derivative of a complex expression, in which case we will use to write expressions like
英語のヒント
たいていの著者は一つの記法を選んでそれを使い続けるが、それさえも保証されてはいない。これらすべてに慣れておくのが最善である。本書では全体を通じて記法を使う。ただし、複雑な式の導関数をとりたい場合は例外で、その場合はを使って次のような式を書く。
(22.3.5)
Oftentimes, it is intuitively useful to unravel the definition of derivative (22.3.3) again to see how a function changes when we make a small change of :
英語のヒント
を少し変えたときに関数がどう変わるかを見るために、導関数の定義(式(22.3.3))をもう一度ほどいてみると、直観的に役に立つことがよくある。
(22.3.6)
The last equation is worth explicitly calling out. It tells us that if you take any function and change the input by a small amount, the output would change by that small amount scaled by the derivative.
英語のヒント
最後の式ははっきりと強調しておく価値がある。これは、任意の関数をとって入力を少しだけ変えると、出力はその少しの量に導関数を掛けた分だけ変わることを教えてくれる。
In this way, we can understand the derivative as the scaling factor that tells us how large of change we get in the output from a change in the input.
英語のヒント
このようにして、導関数は、入力の変化から出力にどれだけの変化が生じるかを教えてくれる倍率として理解できる。