3週目の記事もあるので良かったら読んでみてください。
Neural Networks
単純な線形回帰やロジスティック回帰に、2次や 3次のフィーチャーを加えた複雑(非線形)な仮説を学習させることは、まったく適していないです。 それは、フィーチャーが多くなりすぎ、計算コストが高くなるからです。その時に使うのがニューラルネットワークと言われるアルゴリズムです。この章では、ニューラルネットワークについて説明します。
Model Representation
この節では、簡単なニューラルネットワークの仕組みについて説明します。ニューラルネットワークとは、人間の脳細胞を真似してできたアルゴリズムです。人間の脳細胞は、あるインプットを受け取って、計算した後にそれをアウトプットします。そのアウトプットを更に別の脳細胞のインプットとして受け取り、計算してアウトプットします。これを、フォワードプロパゲーションと言います。このように、一つの仮説(モデル)だけで処理せずに、複数回に計算を分けて、インプットとアウトプットを繰り返すことがニューラルネットワークの特徴です。
機械学習のニューラルネットワークでは、最初のインプットと最後のアウトプットを、Input layer と Output layer と言います。その中間の層は、総じて Hiden layer と言われます。また、Hiden layer のそれぞれのユニット(フューチャー)のことを、Activation node と言います。それぞれの仮説により必要なパラメーターとユニットは下記のように記述します。
また、j 層と j+1 層のユニット数を、a, bとすると、θj の行列は b x (a+1) の行列になります。列数が a+1 になるのは、各 x0(バイアスユニット)が計算時に考慮されるからです。通常、バイアスユニットは 1 です。
このニューラルネットワークがやる事はロジスティック回帰や線形回帰のようなことです。しかし、もともとのフィーチャーである x1、x2、x3を使う代わりにこれらの新しいフィーチャーa1、a2、a3を使う点で大きく異なります。具体的には、レイヤー1からレイヤー2へとマッピングするパラメータの組である θ1 で定義されます。つまりニューラルネットワークはロジスティック回帰や線形回帰への 入力のフィーチャーを x1、x2、x3 に制限する代わりに独自のフィーチャー a1,a2,a3を学習して、それをロジスティック回帰や線形回帰に食わせられるような物です。そして、θ1に何を選ぶかによって とても興味深く複雑なフィーチャーを学習できます。それゆえに、結果として単に生のフィーチャーx1、x2、x3に食わせるフィーチャーを限定したり、 その多項式、 x1x2、x2x3などの多項式から選ぶよりより良い仮説が 得られます。
最後にこの節のサマリーを載せておきます。
Let’s examine how we will represent a hypothesis function using neural networks. At a very simple level, neurons are basically computational units that take inputs (dendrites) as electrical inputs (called “spikes”) that are channeled to outputs (axons). In our model, our dendrites are like the input features x1⋯ xn, and the output is the result of our hypothesis function. In this model our x0 input node is sometimes called the “bias unit.” It is always equal to 1. In neural networks, we use the same logistic function as in classification, 1+e−θTx1, yet we sometimes call it a sigmoid (logistic) activation function. In this situation, our “theta” parameters are sometimes called “weights”.
Our input nodes (layer 1), also known as the “input layer”, go into another node (layer 2), which finally outputs the hypothesis function, known as the “output layer”.
We can have intermediate layers of nodes between the input and output layers called the “hidden layers.”
In this example, we label these intermediate or “hidden” layer nodes a²_0 ⋯ a²_n and call them “activation units.”
If we had one hidden layer, it would look like:
The values for each of the “activation” nodes is obtained as follows:
This is saying that we compute our activation nodes by using a 3×4 matrix of parameters. We apply each row of the parameters to our inputs to obtain the value for one activation node. Our hypothesis output is the logistic function applied to the sum of the values of our activation nodes, which have been multiplied by yet another parameter matrix Θ(2) containing the weights for our second layer of nodes.
Each layer gets its own matrix of weights, Θ(j).
The dimensions of these matrices of weights is determined as follows:
The +1 comes from the addition in Θ(j) of the “bias nodes,” x0 and Θ0(j). In other words the output nodes will not include the bias nodes while the inputs will. The following image summarizes our model representation:
To re-iterate, the following is an example of a neural network:
In this section we’ll do a vectorized implementation of the above functions. We’re going to define a new variable zk(j) that encompasses the parameters inside our g function. In our previous example if we replaced by the variable z for all the parameters we would get:
In other words, for layer j=2 and node k, the variable z will be:
The vector representation of x and z^{j}zj is:
Setting x=a(1), we can rewrite the equation as:
We are multiplying our matrix Θ(j−1) with dimensions (n+1)sj×(n+1) (where sj is the number of our activation nodes) by our vector a(j−1) with height (n+1). This gives us our vector z(j) with height sj. Now we can get a vector of our activation nodes for layer j as follows:
Where our function g can be applied element-wise to our vector z(j).
We can then add a bias unit (equal to 1) to layer j after we have computed a(j). This will be element a0(j) and will be equal to 1. To compute our final hypothesis, let’s first compute another z vector:
We get this final z vector by multiplying the next theta matrix after Θ(j−1) with the values of all the activation nodes we just got. This last theta matrix Θ(j) will have only one row which is multiplied by one column a(j) so that our result is a single number. We then get our final result with:
Notice that in this last step, between layer j and layer j+1, we are doing exactly the same thing as we did in logistic regression. Adding all these intermediate layers in neural networks allows us to more elegantly produce interesting and more complex non-linear hypotheses.
Applications
この章では、実際にニューラルネットワークを用いたアルゴリズムをどのように計算するのかを説明します。
Examples and Intuitions
ロジスティック回帰を用いたニューラルネットワークの説明をします。ロジスティック回帰の sigmoid 関数を、ニューラルネットワークでは activation 関数とも呼びます。その sigmoid 関数の見た目は下記です。
グラフから、おおよそ絶対値が4.6以上になると、1 または 0 と近似できます。この特性を活かし、θ のパラメーターをうまく選ぶとインプットとアウトプットの関係を、論理式で表すことができます。論理式の一覧を下記に示します。
ここで下記のような分類問題を解きたいとします。これをロジスティック回帰を用いて解くと、青線のような境界線が欲しくなり、非線形なモデルができることが想像できます。ただ、これをニューラルネットワークを使って解くと、簡単な線形の組み合わせであることが分かります。
上記のグラフを単純化して考えると、下記のように捉えることができます。
これは、論理式で表すと、 x1 XNOR x2 で有ることが分かります。後は、これを満たすような Θ の行列を見つけてあげれば良いことになります。また、sigmoid 関数の特性を使うと、否定を行うには 基本的には負の大きなウェイト(パラメーター)を否定したい変数の前に おけば良いです。下記の場合は-20を、x1に掛けています。 これがx1を否定するやり方の基本的な考え方です。
このように論理式の NOT を使いたい場合は、そのユニットに大きな負のパラメーターを掛け合わせることを応用し、XNOR の論理式を表現します。具体的には、a1 を x1 AND x2 とし、a2 を (NOT x1) AND (NOT x2) とします。そして、a1 OR a2 とすると、x1 XNOR x2 と表現できます。その対応表を、下記に示します。
このように、各 Hiden layer にあるパラメーターを調整し、論理式と組み合わせることで、複雑な非線形の計算をせずに、目的のモデルを取ることができます。最後に、ニューラルネットワークを用いた手書きの文字入力の例を示します。この動画では手書きの文字を3段階に分割し、学習させています。
最後にこの節のサマリーを載せておきます。
A simple example of applying neural networks is by predicting x1 AND x2, which is the logical ‘and’ operator and is only true if both x1 and x2 are 1.
The graph of our functions will look like:
Remember that x_0x0 is our bias variable and is always 1.
Let’s set our first theta matrix as:
This will cause the output of our hypothesis to only be positive if both x1 and x2 are 1. In other words:
So we have constructed one of the fundamental operations in computers by using a small neural network rather than using an actual AND gate. Neural networks can also be used to simulate all the other logical gates. The following is an example of the logical operator ‘OR’, meaning either x1 is true or x2 is true, or both:
Where g(z) is the following:
The Θ(1) matrices for AND, NOR, and OR are:
We can combine these to get the XNOR logical operator (which gives 1 if x1 and x2 are both 0 or both 1).
For the transition between the first and second layer, we’ll use a Θ(1) matrix that combines the values for AND and NOR:
For the transition between the second and third layer, we’ll use a Θ(2) matrix that uses the value for OR:
Let’s write out the values for all our nodes:
And there we have the XNOR operator using a hidden layer with two nodes! The following summarizes the above algorithm:
最後に今週の分の宿題のコードを載せておきます。今週はほとんど先週にやった内容だったので、サクサクできました。