第4章分類のための線形ニューラルネットワーク
4.1.1. Classification4.1.1 分類
To get our feet wet, let’s start with a simple image classification problem. Here, each input consists of a grayscale image. We can represent each pixel value with a single scalar, giving us four features . Further, let’s assume that each image belongs to one among the categories “cat”, “chicken”, and “dog”.
英語のヒント
まず手始めに、単純な画像分類の問題から始めよう。ここでは各入力はのグレースケール画像からなる。各画素の値を一つのスカラーで表せば、四つの特徴量が得られる。さらに、各画像は「猫」「ニワトリ」「犬」のカテゴリーのどれか一つに属すると仮定しよう。
Next, we have to choose how to represent the labels. We have two obvious choices. Perhaps the most natural impulse would be to choose , where the integers represent respectively. This is a great way of storing such information on a computer. If the categories had some natural ordering among them, say if we were trying to predict , then it might even make sense to cast this as an ordinal regression problem and keep the labels in this format. See Moon et al. (2010) for an overview of different types of ranking loss functions and Beutel et al. (2014) for a Bayesian approach that addresses responses with more than one mode.
英語のヒント
In general, classification problems do not come with natural orderings among the classes. Fortunately, statisticians long ago invented a simple way to represent categorical data: the one-hot encoding. A one-hot encoding is a vector with as many components as we have categories. The component corresponding to a particular instance’s category is set to 1 and all other components are set to 0. In our case, a label would be a three-dimensional vector, with corresponding to “cat”, to “chicken”, and to “dog”:
英語のヒント
一般に、分類問題のクラスのあいだには自然な順序はない。幸い、統計学者はずっと昔に、カテゴリーのデータを表す単純な方法を発明した。ワンホット符号化である。ワンホット符号化とは、カテゴリーの数と同じだけの成分をもつベクトルである。ある特定のインスタンスのカテゴリーに対応する成分は1に、ほかの成分はすべて0に設定する。この例では、ラベルは3次元のベクトルになり、が「猫」に、が「ニワトリ」に、が「犬」に対応する。
(4.1.1)
4.1.1.1. Linear Model4.1.1.1 線形モデル
In order to estimate the conditional probabilities associated with all the possible classes, we need a model with multiple outputs, one per class. To address classification with linear models, we will need as many affine functions as we have outputs. Strictly speaking, we only need one fewer, since the final category has to be the difference between and the sum of the other categories, but for reasons of symmetry we use a slightly redundant parametrization. Each output corresponds to its own affine function. In our case, since we have 4 features and 3 possible output categories, we need 12 scalars to represent the weights ( with subscripts), and 3 scalars to represent the biases ( with subscripts). This yields:
英語のヒント
起こりうるすべてのクラスに対応する条件付き確率を推定するには、クラスごとに一つずつ、複数の出力をもつモデルが必要である。線形モデルで分類を扱うには、出力の数と同じだけのアフィン関数が必要になる。厳密に言えば、一つ少なくてよい。最後のカテゴリーは、とほかのカテゴリーの和との差でなければならないからである。しかし対称性のために、少し冗長なパラメータ化を使う。各出力は、それ自身のアフィン関数に対応する。この例では、特徴量が4個、出力カテゴリーが3個なので、重みを表すのに12個のスカラー(添え字付きの)、バイアスを表すのに3個のスカラー(添え字付きの)が必要である。これにより、次が得られる。
(4.1.2)
The corresponding neural network diagram is shown in Fig. 4.1.1. Just as in linear regression, we use a single-layer neural network. And since the calculation of each output, , and , depends on every input, , , , and , the output layer can also be described as a fully connected layer.
英語のヒント
対応するニューラルネットワークの図を図4.1.1に示す。線形回帰と同じく、単層のニューラルネットワークを使う。そして、各出力、の計算はすべての入力、、、に依存するので、出力層は全結合層としても記述できる。
Fig. 4.1.1図4.1.1
Softmax regression is a single-layer neural network.
英語のヒント
ソフトマックス回帰は単層のニューラルネットワークである。
For a more concise notation we use vectors and matrices: is much better suited for mathematics and code. Note that we have gathered all of our weights into a matrix and all biases in a vector.
英語のヒント
より簡潔な記法のために、ベクトルと行列を使う。のほうが、数学にもコードにもずっと向いている。重みはすべての行列に、バイアスはすべてベクトルにまとめたことに注意してほしい。
4.1.1.2. The Softmax4.1.1.2 ソフトマックス
Assuming a suitable loss function, we could try, directly, to minimize the difference between and the labels . While it turns out that treating classification as a vector-valued regression problem works surprisingly well, it is nonetheless unsatisfactory in the following ways:
英語のヒント
適切な損失関数を仮定すれば、とラベルの差を直接最小にしようとすることもできる。分類をベクトル値の回帰問題として扱うことは驚くほどうまくいくことがわかるが、それでも次の点で満足のいくものではない。
- There is no guarantee that the outputs sum up to in the way we expect probabilities to behave.
英語のヒント
出力の和が、確率に期待される振る舞いのようにになる保証はない。 - There is no guarantee that the outputs are even nonnegative, even if their outputs sum up to , or that they do not exceed .
英語のヒント
出力の和がになったとしても、出力が非負である保証すらないし、を超えない保証もない。
Both aspects render the estimation problem difficult to solve and the solution very brittle to outliers. For instance, if we assume that there is a positive linear dependency between the number of bedrooms and the likelihood that someone will buy a house, the probability might exceed when it comes to buying a mansion! As such, we need a mechanism to “squish” the outputs.
英語のヒント
どちらの面も、推定問題を解きにくくし、解を外れ値に対して非常にもろくする。たとえば、寝室の数と誰かが家を買う可能性とのあいだに正の線形の依存関係があると仮定すると、大邸宅を買うとなれば確率がを超えてしまうかもしれない。そこで、出力を「押しつぶす」仕組みが必要になる。
There are many ways we might accomplish this goal. For instance, we could assume that the outputs are corrupted versions of , where the corruption occurs by means of adding noise drawn from a normal distribution. In other words, , where . This is the so-called probit model, first introduced by Fechner (1860). While appealing, it does not work quite as well nor lead to a particularly nice optimization problem, when compared to the softmax.
英語のヒント
Another way to accomplish this goal (and to ensure nonnegativity) is to use an exponential function . This does indeed satisfy the requirement that the conditional class probability increases with increasing , it is monotonic, and all probabilities are nonnegative. We can then transform these values so that they add up to by dividing each by their sum. This process is called normalization. Putting these two pieces together gives us the softmax function:
英語のヒント
この目標を達成する(そして非負性を保証する)もう一つの方法は、指数関数を使うことである。これは確かに、が増えるにつれて条件付きのクラスの確率が増えるという要件を満たし、単調で、すべての確率が非負である。そのうえで、各値をそれらの和で割ることで、和がになるようにこれらの値を変換できる。この過程を正規化という。この二つの部品を組み合わせると、ソフトマックス関数が得られる。
(4.1.3)
Note that the largest coordinate of corresponds to the most likely class according to . Moreover, because the softmax operation preserves the ordering among its arguments, we do not need to compute the softmax to determine which class has been assigned the highest probability. Thus,
英語のヒント
の最大の座標は、によれば最も可能性の高いクラスに対応することに注意してほしい。さらに、ソフトマックスの演算は引数のあいだの順序を保つので、どのクラスに最も高い確率が割り当てられたかを決めるのにソフトマックスを計算する必要はない。したがって、
(4.1.4)
The idea of a softmax dates back to Gibbs (1902), who adapted ideas from physics. Dating even further back, Boltzmann, the father of modern statistical physics, used this trick to model a distribution over energy states in gas molecules. In particular, he discovered that the prevalence of a state of energy in a thermodynamic ensemble, such as the molecules in a gas, is proportional to . Here, is the energy of a state, is the temperature, and is the Boltzmann constant. When statisticians talk about increasing or decreasing the “temperature” of a statistical system, they refer to changing in order to favor lower or higher energy states. Following Gibbs’ idea, energy equates to error. Energy-based models (Ranzato et al., 2007) use this point of view when describing problems in deep learning.
英語のヒント
ソフトマックスという考え方は、物理学の考えを取り入れたGibbs (1902)にさかのぼる。さらにさかのぼると、現代統計物理学の父ボルツマンは、この技を使って、気体分子のエネルギー状態の分布をモデル化した。特に彼は、気体の分子のような熱力学的なアンサンブルにおけるあるエネルギー状態の出現率が、に比例することを発見した。ここでは状態のエネルギー、は温度、はボルツマン定数である。統計学者が統計的なシステムの「温度」を上げたり下げたりすると言うとき、それは、より低いか、より高いエネルギー状態を優遇するためにを変えることを指している。ギブズの考えに従えば、エネルギーは誤差に相当する。エネルギーにもとづくモデル(Ranzato et al., 2007)は、深層学習の問題を記述するとき、この見方を使う。
4.1.1.3. Vectorization4.1.1.3 ベクトル化
To improve computational efficiency, we vectorize calculations in minibatches of data. Assume that we are given a minibatch of examples with dimensionality (number of inputs) . Moreover, assume that we have categories in the output. Then the weights satisfy and the bias satisfies .
英語のヒント
計算効率を上げるため、データのミニバッチで計算をベクトル化する。次元数(入力の数)がの個の事例からなるミニバッチが与えられたとしよう。さらに、出力に個のカテゴリーがあるとしよう。すると重みはを満たし、バイアスはを満たす。
(4.1.5)
This accelerates the dominant operation into a matrix–matrix product . Moreover, since each row in represents a data example, the softmax operation itself can be computed rowwise: for each row of , exponentiate all entries and then normalize them by the sum. Note, though, that care must be taken to avoid exponentiating and taking logarithms of large numbers, since this can cause numerical overflow or underflow. Deep learning frameworks take care of this automatically.
英語のヒント
これにより、支配的な演算は行列どうしの積へと高速化される。さらに、の各行は一つのデータ事例を表すので、ソフトマックスの演算そのものは行ごとに計算できる。の各行について、すべての成分を指数関数に通し、それからその和で正規化する。ただし、大きな数を指数関数に通したり対数をとったりすることは避けるよう注意しなければならない。数値のオーバーフローやアンダーフローを引き起こしうるからである。深層学習フレームワークはこれを自動で処理してくれる。