プリンキピア

第3章回帰のための線形ニューラルネットワーク

3.1.1. Basics3.1.1 基本

Linear regression is both the simplest and most popular among the standard tools for tackling regression problems. Dating back to the dawn of the 19th century (Gauss, 1809, Legendre, 1805), linear regression flows from a few simple assumptions. First, we assume that the relationship between features 𝐱 and target y is approximately linear, i.e., that the conditional mean E[Y∣X=𝐱] can be expressed as a weighted sum of the features 𝐱. This setup allows that the target value may still deviate from its expected value on account of observation noise. Next, we can impose the assumption that any such noise is well behaved, following a Gaussian distribution. Typically, we will use n to denote the number of examples in our dataset. We use superscripts to enumerate samples and targets, and subscripts to index coordinates. More concretely, 𝐱(i) denotes the ith sample and xj(i) denotes its jth coordinate.
英語のヒント
線形回帰は、回帰問題に取り組む標準的な道具のなかで、最も単純で最も人気がある。19世紀の初めにさかのぼり(Gauss, 1809、Legendre, 1805)、線形回帰はいくつかの単純な仮定から導かれる。まず、特徴量𝐱と目標値yの関係がほぼ線形である、つまり条件付き平均E[Y∣X=𝐱]が特徴量𝐱の重み付き和で表せると仮定する。この設定では、観測ノイズのせいで、目標値がその期待値からずれることもありうる。次に、そうしたノイズは行儀がよく、ガウス分布に従うという仮定を課すことができる。普通、データセットの事例の数を表すのにnを使う。サンプルと目標値の番号付けには上付き添え字を、座標のインデックスには下付き添え字を使う。より具体的には、𝐱(i)はith番目のサンプルを、xj(i)はそのjth番目の座標を表す。

3.1.1.1. Model3.1.1.1 モデル

At the heart of every solution is a model that describes how features can be transformed into an estimate of the target. The assumption of linearity means that the expected value of the target (price) can be expressed as a weighted sum of the features (area and age):
英語のヒント
どんな解決策の中心にも、特徴量を目標値の推定値へどう変換できるかを記述するモデルがある。線形性の仮定とは、目標値(価格)の期待値が、特徴量(面積と築年数)の重み付き和として表せることを意味する。
price=warea⋅area+wage⋅age+b.(3.1.1)
Here warea and wage are called weights, and b is called a bias (or offset or intercept). The weights determine the influence of each feature on our prediction. The bias determines the value of the estimate when all features are zero. Even though we will never see any newly-built homes with precisely zero area, we still need the bias because it allows us to express all linear functions of our features (rather than restricting us to lines that pass through the origin). Strictly speaking, (3.1.1) is an affine transformation of input features, which is characterized by a linear transformation of features via a weighted sum, combined with a translation via the added bias. Given a dataset, our goal is to choose the weights 𝐰 and the bias b that, on average, make our model’s predictions fit the true prices observed in the data as closely as possible.
英語のヒント
ここでwareaとwageは重みと呼ばれ、bはバイアス(またはオフセット、切片)と呼ばれる。重みは、各特徴量が予測に与える影響を決める。バイアスは、すべての特徴量がゼロのときの推定値を決める。面積がちょうどゼロの新築の家を見ることは決してないが、それでもバイアスは必要である。バイアスがあれば、(原点を通る直線に限られることなく)特徴量のすべての線形関数を表せるからである。厳密に言えば、(3.1.1)は入力の特徴量のアフィン変換であり、重み付き和による特徴量の線形変換に、加えたバイアスによる平行移動を組み合わせたものとして特徴づけられる。データセットが与えられたとき、目標は、平均してモデルの予測がデータで観測された本当の価格にできるだけよく合うような、重み𝐰とバイアスbを選ぶことである。
In disciplines where it is common to focus on datasets with just a few features, explicitly expressing models long-form, as in (3.1.1), is common. In machine learning, we usually work with high-dimensional datasets, where it is more convenient to employ compact linear algebra notation. When our inputs consist of d features, we can assign each an index (between 1 and d) and express our prediction yˆ (in general the “hat” symbol denotes an estimate) as
英語のヒント
特徴量がほんの少ししかないデータセットに焦点を当てるのが普通の分野では、(3.1.1)のようにモデルを長い形で明示的に書き表すのが普通である。機械学習では普通、高次元のデータセットを扱うので、簡潔な線形代数の記法を使うほうが便利である。入力がd個の特徴量からなるとき、それぞれに(1からdのあいだの)インデックスを割り当て、予測yˆ(一般に「ハット」記号は推定値を表す)を次のように表せる。
yˆ=w1x1+⋯+wdxd+b.(3.1.2)
Collecting all features into a vector 𝐱∈ℝd and all weights into a vector 𝐰∈ℝd, we can express our model compactly via the dot product between 𝐰 and 𝐱:
英語のヒント
すべての特徴量をベクトル𝐱∈ℝdに、すべての重みをベクトル𝐰∈ℝdにまとめると、𝐰と𝐱の内積を使ってモデルを簡潔に表せる。
yˆ=𝐰⊤𝐱+b.(3.1.3)
In (3.1.3), the vector 𝐱 corresponds to the features of a single example. We will often find it convenient to refer to features of our entire dataset of n examples via the design matrix 𝐗∈ℝn×d. Here, 𝐗 contains one row for every example and one column for every feature. For a collection of features 𝐗, the predictions 𝐲ˆ∈ℝn can be expressed via the matrix–vector product:
英語のヒント
(3.1.3)で、ベクトル𝐱は一つの事例の特徴量に対応する。n個の事例からなるデータセット全体の特徴量を、計画行列𝐗∈ℝn×dで参照すると便利なことがよくある。ここで𝐗は、事例ごとに1行、特徴量ごとに1列をもつ。特徴量の集まり𝐗について、予測𝐲ˆ∈ℝnは行列とベクトルの積で表せる。
𝐲ˆ=𝐗𝐰+b,(3.1.4)
where broadcasting (Section 2.1.4) is applied during the summation. Given features of a training dataset 𝐗 and corresponding (known) labels 𝐲, the goal of linear regression is to find the weight vector 𝐰 and the bias term b such that, given features of a new data example sampled from the same distribution as 𝐗, the new example’s label will (in expectation) be predicted with the smallest error.
英語のヒント
ここで和をとる際にはブロードキャスト(2.1.4節)が適用される。訓練データセットの特徴量𝐗と対応する(既知の)ラベル𝐲が与えられたとき、線形回帰の目標は、𝐗と同じ分布から抽出した新しいデータ事例の特徴量が与えられたときに、その新しい事例のラベルが(期待値の意味で)最小の誤差で予測されるような、重みベクトル𝐰とバイアス項bを見つけることである。
Even if we believe that the best model for predicting y given 𝐱 is linear, we would not expect to find a real-world dataset of n examples where y(i) exactly equals 𝐰⊤𝐱(i)+b for all 1≤i≤n. For example, whatever instruments we use to observe the features 𝐗 and labels 𝐲, there might be a small amount of measurement error. Thus, even when we are confident that the underlying relationship is linear, we will incorporate a noise term to account for such errors.
英語のヒント
𝐱が与えられたときのyを予測する最良のモデルが線形だと信じていても、n個の事例からなる現実のデータセットで、すべての1≤i≤nについてy(i)が𝐰⊤𝐱(i)+bにぴったり等しくなるとは期待しないだろう。たとえば、特徴量𝐗とラベル𝐲を観測するのにどんな器具を使っても、わずかな測定誤差があるかもしれない。したがって、元にある関係が線形だと確信していても、そうした誤差を説明するノイズ項を組み込む。
Before we can go about searching for the best parameters (or model parameters) 𝐰 and b, we will need two more things: (i) a measure of the quality of some given model; and (ii) a procedure for updating the model to improve its quality.
英語のヒント
最良のパラメータ(またはモデルパラメータ)𝐰とbを探しにかかる前に、あと二つのものが必要になる。(i)ある与えられたモデルの質の尺度。(ii)モデルの質を高めるためにモデルを更新する手順。

3.1.1.2. Loss Function3.1.1.2 損失関数

Naturally, fitting our model to the data requires that we agree on some measure of fitness (or, equivalently, of unfitness). Loss functions quantify the distance between the real and predicted values of the target. The loss will usually be a nonnegative number where smaller values are better and perfect predictions incur a loss of 0. For regression problems, the most common loss function is the squared error. When our prediction for an example i is yˆ(i) and the corresponding true label is y(i), the squared error is given by:
英語のヒント
当然、モデルをデータに当てはめるには、当てはまりの良さ(あるいは同じことだが、悪さ)の何らかの尺度について合意する必要がある。損失関数は、目標値の本当の値と予測値との距離を数値で表す。損失は普通、非負の数で、値が小さいほどよく、完璧な予測なら損失は0になる。回帰問題で最もよく使われる損失関数は二乗誤差である。事例iについての予測がyˆ(i)で、対応する本当のラベルがy(i)のとき、二乗誤差は次で与えられる。
l(i)(𝐰,b)=12(yˆ(i)−y(i))2.(3.1.5)
The constant 12 makes no real difference but proves to be notationally convenient, since it cancels out when we take the derivative of the loss. Because the training dataset is given to us, and thus is out of our control, the empirical error is only a function of the model parameters. In Fig. 3.1.1, we visualize the fit of a linear regression model in a problem with one-dimensional inputs.
英語のヒント
定数12は実質的な違いを生まないが、損失の導関数をとるときに打ち消し合うので、記法の上で便利だとわかる。訓練データセットは私たちに与えられたもので、こちらでは制御できないので、経験誤差はモデルのパラメータだけの関数である。図3.1.1では、1次元の入力をもつ問題で線形回帰モデルの当てはまりを可視化している。
Fig. 3.1.1図3.1.1
Fitting a linear regression model to one-dimensional data.
英語のヒント
1次元のデータに線形回帰モデルを当てはめる。
Note that large differences between estimates yˆ(i) and targets y(i) lead to even larger contributions to the loss, due to its quadratic form (this quadraticity can be a double-edge sword; while it encourages the model to avoid large errors it can also lead to excessive sensitivity to anomalous data). To measure the quality of a model on the entire dataset of n examples, we simply average (or equivalently, sum) the losses on the training set:
英語のヒント
推定値yˆ(i)と目標値y(i)の差が大きいと、損失が2次の形をしているために、損失への寄与はさらに大きくなることに注意してほしい(この2次性は諸刃の剣になりうる。モデルが大きな誤差を避けるよう促す一方で、異常なデータに過敏になることにもつながりうる)。n個の事例からなるデータセット全体でモデルの質を測るには、訓練集合での損失を平均する(あるいは同じことだが、和をとる)だけでよい。
L(𝐰,b)=1n∑i=1nl(i)(𝐰,b)=1n∑i=1n12(𝐰⊤𝐱(i)+b−y(i))2.(3.1.6)
When training the model, we seek parameters (𝐰∗,b∗) that minimize the total loss across all training examples:
英語のヒント
モデルを訓練するときは、すべての訓練事例にわたる総損失を最小にするパラメータ(𝐰∗,b∗)を求める。
𝐰∗,b∗=argmin𝐰,b L(𝐰,b).(3.1.7)

3.1.1.3. Analytic Solution3.1.1.3 解析解

Unlike most of the models that we will cover, linear regression presents us with a surprisingly easy optimization problem. In particular, we can find the optimal parameters (as assessed on the training data) analytically by applying a simple formula as follows. First, we can subsume the bias b into the parameter 𝐰 by appending a column to the design matrix consisting of all 1s. Then our prediction problem is to minimize ∥𝐲−𝐗𝐰∥2. As long as the design matrix 𝐗 has full rank (no feature is linearly dependent on the others), then there will be just one critical point on the loss surface and it corresponds to the minimum of the loss over the entire domain. Taking the derivative of the loss with respect to 𝐰 and setting it equal to zero yields:
英語のヒント
これから扱うほとんどのモデルと違って、線形回帰は驚くほど簡単な最適化問題をもたらす。特に、次のように単純な公式を当てはめるだけで、(訓練データで評価した)最適なパラメータを解析的に求められる。まず、計画行列にすべて1からなる列を付け加えることで、バイアスbをパラメータ𝐰に組み込める。すると、予測問題は∥𝐲−𝐗𝐰∥2を最小にすることになる。計画行列𝐗が最大階数をもつ(どの特徴量もほかの特徴量に線形従属しない)かぎり、損失曲面の臨界点はただ一つで、それは定義域全体での損失の最小値に対応する。損失を𝐰について微分してゼロとおくと、次が得られる。
∂𝐰∥𝐲−𝐗𝐰∥2=2𝐗⊤(𝐗𝐰−𝐲)=0 and hence 𝐗⊤𝐲=𝐗⊤𝐗𝐰.(3.1.8)
Solving for 𝐰 provides us with the optimal solution for the optimization problem. Note that this solution
英語のヒント
𝐰について解くと、最適化問題の最適解が得られる。この解は
𝐰∗=(𝐗⊤𝐗)−1𝐗⊤𝐲(3.1.9)
will only be unique when the matrix 𝐗⊤𝐗 is invertible, i.e., when the columns of the design matrix are linearly independent (Golub and Van Loan, 1996).
英語のヒント
行列𝐗⊤𝐗が逆行列をもつとき、つまり計画行列の列が線形独立なときにだけ一意になることに注意してほしい(Golub and Van Loan, 1996)。
While simple problems like linear regression may admit analytic solutions, you should not get used to such good fortune. Although analytic solutions allow for nice mathematical analysis, the requirement of an analytic solution is so restrictive that it would exclude almost all exciting aspects of deep learning.
英語のヒント
線形回帰のような単純な問題には解析解があるかもしれないが、そんな幸運に慣れてはいけない。解析解があればきれいな数学的分析ができるが、解析解を要求するのはあまりに制約が強く、深層学習の刺激的な面のほとんどすべてを締め出してしまうだろう。

3.1.1.4. Minibatch Stochastic Gradient Descent3.1.1.4 ミニバッチ確率的勾配降下法

Fortunately, even in cases where we cannot solve the models analytically, we can still often train models effectively in practice. Moreover, for many tasks, those hard-to-optimize models turn out to be so much better that figuring out how to train them ends up being well worth the trouble.
英語のヒント
幸い、モデルを解析的に解けない場合でも、実際には多くの場合モデルを効果的に訓練できる。しかも、多くの課題で、そうした最適化しにくいモデルのほうがはるかに優れていることがわかり、それをどう訓練するかを考え出すことは、十分に骨を折る価値があることになる。
The key technique for optimizing nearly every deep learning model, and which we will call upon throughout this book, consists of iteratively reducing the error by updating the parameters in the direction that incrementally lowers the loss function. This algorithm is called gradient descent.
英語のヒント
ほぼすべての深層学習モデルを最適化するための鍵となる技術、そしてこの本を通じて頼ることになる技術は、損失関数を少しずつ下げる方向にパラメータを更新することで、誤差を反復的に減らしていくことからなる。このアルゴリズムを勾配降下法という。
The most naive application of gradient descent consists of taking the derivative of the loss function, which is an average of the losses computed on every single example in the dataset. In practice, this can be extremely slow: we must pass over the entire dataset before making a single update, even if the update steps might be very powerful (Liu and Nocedal, 1989). Even worse, if there is a lot of redundancy in the training data, the benefit of a full update is limited.
英語のヒント
勾配降下法の最も素朴な使い方は、損失関数の導関数をとることである。損失関数は、データセットのすべての事例で計算した損失の平均である。実際には、これは極めて遅くなりうる。更新のステップが非常に強力だとしても(Liu and Nocedal, 1989)、1回の更新をする前にデータセット全体を通らなければならないからである。さらに悪いことに、訓練データに冗長さが多ければ、全体を使った更新の利点は限られる。
The other extreme is to consider only a single example at a time and to take update steps based on one observation at a time. The resulting algorithm, stochastic gradient descent (SGD) can be an effective strategy (Bottou, 2010), even for large datasets. Unfortunately, SGD has drawbacks, both computational and statistical. One problem arises from the fact that processors are a lot faster multiplying and adding numbers than they are at moving data from main memory to processor cache. It is up to an order of magnitude more efficient to perform a matrix–vector multiplication than a corresponding number of vector–vector operations. This means that it can take a lot longer to process one sample at a time compared to a full batch. A second problem is that some of the layers, such as batch normalization (to be described in Section 8.5), only work well when we have access to more than one observation at a time.
英語のヒント
もう一方の極端は、一度に一つの事例だけを考え、一度に一つの観測にもとづいて更新ステップをとることである。こうして得られるアルゴリズム、確率的勾配降下法(SGD)は、大きなデータセットでも効果的な戦略になりうる(Bottou, 2010)。残念ながら、SGDには計算上と統計上の両方の欠点がある。一つの問題は、プロセッサは、メインメモリからプロセッサのキャッシュへデータを移すよりも、数を掛けたり足したりするほうがずっと速いという事実から生じる。行列とベクトルの積を行うほうが、対応する数のベクトルどうしの演算を行うより、最大で1桁効率がよい。つまり、一度に一つのサンプルを処理すると、バッチ全体を処理するのに比べてずっと長い時間がかかりうる。第二の問題は、バッチ正規化(8.5節で説明する)のような一部の層は、一度に複数の観測を利用できるときにしかうまく働かないことである。
The solution to both problems is to pick an intermediate strategy: rather than taking a full batch or only a single sample at a time, we take a minibatch of observations (Li et al., 2014). The specific choice of the size of the said minibatch depends on many factors, such as the amount of memory, the number of accelerators, the choice of layers, and the total dataset size. Despite all that, a number between 32 and 256, preferably a multiple of a large power of 2, is a good start. This leads us to minibatch stochastic gradient descent.
英語のヒント
両方の問題の解決策は、中間の戦略をとることである。バッチ全体や一度に一つのサンプルではなく、観測のミニバッチをとるのである(Li et al., 2014)。そのミニバッチの大きさを具体的にどう選ぶかは、メモリの量、アクセラレータの数、層の選び方、データセット全体の大きさなど、多くの要因による。それでも、32から256のあいだの数、できれば2の大きなべき乗の倍数がよい出発点である。これがミニバッチ確率的勾配降下法につながる。
In its most basic form, in each iteration t, we first randomly sample a minibatch ℬt consisting of a fixed number |ℬ| of training examples. We then compute the derivative (gradient) of the average loss on the minibatch with respect to the model parameters. Finally, we multiply the gradient by a predetermined small positive value η, called the learning rate, and subtract the resulting term from the current parameter values. We can express the update as follows:
英語のヒント
最も基本的な形では、各反復tで、まず決まった数|ℬ|の訓練事例からなるミニバッチℬtを無作為に抽出する。次に、ミニバッチでの平均損失の、モデルパラメータについての導関数(勾配)を計算する。最後に、学習率と呼ばれる、あらかじめ決めた小さな正の値ηを勾配に掛け、得られた項を今のパラメータの値から引く。更新は次のように表せる。
(𝐰,b)←(𝐰,b)−η|ℬ|∑i∈ℬt∂(𝐰,b)l(i)(𝐰,b).(3.1.10)
In summary, minibatch SGD proceeds as follows: (i) initialize the values of the model parameters, typically at random; (ii) iteratively sample random minibatches from the data, updating the parameters in the direction of the negative gradient. For quadratic losses and affine transformations, this has a closed-form expansion:
英語のヒント
まとめると、ミニバッチSGDは次のように進む。(i)モデルパラメータの値を、普通は無作為に初期化する。(ii)データから無作為なミニバッチを繰り返し抽出し、負の勾配の方向にパラメータを更新する。2次の損失とアフィン変換では、これは閉じた形の展開をもつ。
𝐰←𝐰−η|ℬ|∑i∈ℬt∂𝐰l(i)(𝐰,b)=𝐰−η|ℬ|∑i∈ℬt𝐱(i)(𝐰⊤𝐱(i)+b−y(i))b←b−η|ℬ|∑i∈ℬt∂bl(i)(𝐰,b)=b−η|ℬ|∑i∈ℬt(𝐰⊤𝐱(i)+b−y(i)).(3.1.11)
Since we pick a minibatch ℬ we need to normalize by its size |ℬ|. Frequently minibatch size and learning rate are user-defined. Such tunable parameters that are not updated in the training loop are called hyperparameters. They can be tuned automatically by a number of techniques, such as Bayesian optimization (Frazier, 2018). In the end, the quality of the solution is typically assessed on a separate validation dataset (or validation set).
英語のヒント
ミニバッチℬを選ぶので、その大きさ|ℬ|で正規化する必要がある。ミニバッチの大きさと学習率は、利用者が決めることが多い。訓練のループで更新されない、こうした調整可能なパラメータをハイパーパラメータという。これらは、ベイズ最適化(Frazier, 2018)のような、いくつもの技術で自動的に調整できる。最終的には、解の質は普通、別の検証データセット(または検証集合)で評価される。
After training for some predetermined number of iterations (or until some other stopping criterion is met), we record the estimated model parameters, denoted 𝐰ˆ,bˆ. Note that even if our function is truly linear and noiseless, these parameters will not be the exact minimizers of the loss, nor even deterministic. Although the algorithm converges slowly towards the minimizers it typically will not find them exactly in a finite number of steps. Moreover, the minibatches ℬ used for updating the parameters are chosen at random. This breaks determinism.
英語のヒント
あらかじめ決めた回数だけ反復して訓練した後(あるいはほかの停止基準が満たされるまで訓練した後)、𝐰ˆ,bˆと表される推定されたモデルパラメータを記録する。関数が本当に線形でノイズがなくても、これらのパラメータは損失を厳密に最小にするものではなく、決定論的ですらないことに注意してほしい。アルゴリズムは最小点に向かってゆっくり収束するが、普通は有限のステップで厳密に見つけることはない。しかも、パラメータの更新に使うミニバッチℬは無作為に選ばれる。これが決定論性を崩す。
Linear regression happens to be a learning problem with a global minimum (whenever 𝐗 is full rank, or equivalently, whenever 𝐗⊤𝐗 is invertible). However, the loss surfaces for deep networks contain many saddle points and minima. Fortunately, we typically do not care about finding an exact set of parameters but merely any set of parameters that leads to accurate predictions (and thus low loss). In practice, deep learning practitioners seldom struggle to find parameters that minimize the loss on training sets (Frankle and Carbin, 2018, Izmailov et al., 2018). The more formidable task is to find parameters that lead to accurate predictions on previously unseen data, a challenge called generalization. We return to these topics throughout the book.
英語のヒント
線形回帰は、たまたま大域的な最小値をもつ学習問題である(𝐗が最大階数のとき、あるいは同じことだが𝐗⊤𝐗が逆行列をもつとき)。しかし、深いネットワークの損失曲面には、多くの鞍点と極小値がある。幸い、普通は厳密なパラメータの組を見つけることは気にせず、正確な予測(したがって低い損失)につながるパラメータの組なら何でもよい。実際、深層学習の実践者が、訓練集合での損失を最小にするパラメータを見つけるのに苦労することはめったにない(Frankle and Carbin, 2018、Izmailov et al., 2018)。もっと手ごわいのは、未知のデータで正確な予測につながるパラメータを見つけることで、この課題を汎化という。これらの話題には本全体を通じて立ち戻る。

3.1.1.5. Predictions3.1.1.5 予測

Given the model 𝐰ˆ⊤𝐱+bˆ, we can now make predictions for a new example, e.g., predicting the sales price of a previously unseen house given its area x1 and age x2. Deep learning practitioners have taken to calling the prediction phase inference but this is a bit of a misnomer—inference refers broadly to any conclusion reached on the basis of evidence, including both the values of the parameters and the likely label for an unseen instance. If anything, in the statistics literature inference more often denotes parameter inference and this overloading of terminology creates unnecessary confusion when deep learning practitioners talk to statisticians. In the following we will stick to prediction whenever possible.
英語のヒント
モデル𝐰ˆ⊤𝐱+bˆが与えられれば、新しい事例について予測できる。たとえば、未知の家の面積x1と築年数x2から販売価格を予測する。深層学習の実践者は、予測の段階を推論と呼ぶようになったが、これは少々誤った名前である。推論とは広く、証拠にもとづいて到達するあらゆる結論を指し、パラメータの値も、未知のインスタンスのありそうなラベルも含む。どちらかと言えば、統計学の文献では推論はパラメータの推論を指すことのほうが多く、この用語の使い回しは、深層学習の実践者が統計学者と話すときに無用な混乱を生む。以下では、できるかぎり予測という言葉を使う。