LoginSignup
2
3

More than 5 years have passed since last update.

coursera の機械学習コースで自然と覚えることになりそうな式(Week 5 まで)

Last updated at Posted at 2017-12-28

TL;DR

coursera の機械学習のコース1では数式がたくさん出てきます。これらについてポイントを押さえて覚えていけば学習効率あがるかなと思ったので、重要そうな式を書き下してみました。ニューラルネットワークを扱っているWeek 5 までです。

hypothesis functionの変形

普通に書いた場合 ($x_0$はbias nodeなので1で固定です。)

h_\theta (x) = \theta_0 + \theta_1 x_1 + \theta_2 x_2 + \theta_3 x_3 + \cdots + \theta_n x_n

行列積で表現するとこうなる。

\begin{align*}h_\theta(x) =\begin{bmatrix}\theta_0 \hspace{2em} \theta_1 \hspace{2em} ... \hspace{2em} \theta_n\end{bmatrix}\begin{bmatrix}x_0 \newline x_1 \newline \vdots \newline x_n\end{bmatrix}= \theta^T x\end{align*}

1行n列の行列とn列1行の行列の積なので、計算結果は1行1列の行列になる。(値は各要素の積の総和)
octaveでは1行1列の行列と普通の数字(スカラー)の演算はそのままできるので、普通の数字として扱うことができる。

Cost function (コスト関数)

J(\theta_0 , \theta_1 , ... ,  \theta_n) = \frac{1}{2m}\sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)})^2

教師データの値との差の2乗の平均を2で割った数字。微分したときに都合が良いため2で割る。
(regularization についてはここでは割愛。)

Gradient Descent for Multiple Variables (多変数の最急降下法)

\begin{align*} & \text{repeat until convergence:} \; \lbrace \newline \; & \theta_0 := \theta_0 - \alpha \frac{1}{m} \sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) \cdot x_0^{(i)}\newline \; & \theta_1 := \theta_1 - \alpha \frac{1}{m} \sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) \cdot x_1^{(i)} \newline \; & \theta_2 := \theta_2 - \alpha \frac{1}{m} \sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) \cdot x_2^{(i)} \newline & \cdots \newline \rbrace \end{align*}

1行にまとめるとこうなる。

\begin{align*}& \text{repeat until convergence:} \; \lbrace \newline \; & \theta_j := \theta_j - \alpha \frac{1}{m} \sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) \cdot x_j^{(i)} \; & \text{for j := 0...n}\newline \rbrace\end{align*}

各シータについてコスト関数をそれぞれのシータで偏微分した値と学習率の積をシータから引く。このことを繰り返す。

Sigmoid Function (シグモイド関数)

\begin{align*}& h_\theta (x) = g ( \theta^T x ) \newline \newline& z = \theta^T x \newline& g(z) = \dfrac{1}{1 + e^{-z}}\end{align*}

正なら1に近い、負なら0に近い関数。場合分けではなく式の計算で0と1の対応がつけられる。数学的に使いやすい。

ロジスティクス解析でのコスト関数

y が1の場合と0の場合を1行に含めた形。場合分けではなく式の計算で0と1の対応がつけられる。数学的に使いやすい。

J(\theta) = - \frac{1}{m} \displaystyle \sum_{i=1}^m [y^{(i)}\log (h_\theta (x^{(i)})) + (1 - y^{(i)})\log (1 - h_\theta(x^{(i)}))]

vectorizedするとこうなる。

\begin{align*} & h = g(X\theta)\newline & J(\theta) = \frac{1}{m} \cdot \left(-y^{T}\log(h)-(1-y)^{T}\log(1-h)\right) \end{align*}

多クラス分類

それぞれのクラスに入る確率のうち、一番大きいものを取る。

\begin{align*}& y \in \lbrace0, 1 ... n\rbrace \newline& h_\theta^{(0)}(x) = P(y = 0 | x ; \theta) \newline& h_\theta^{(1)}(x) = P(y = 1 | x ; \theta) \newline& \cdots \newline& h_\theta^{(n)}(x) = P(y = n | x ; \theta) \newline& \mathrm{prediction} = \max_i( h_\theta ^{(i)}(x) )\newline\end{align*}

ニューラルネットワークでのモデル表現

featureが3個( $x_1 ,x_2 ,x_3$ $x_0$はbias nodeなので1固定)、隠れ層1個($a^{(2)}$ 1種類だけ)での、 activation nodes($a_i^{(j)}$) の値と仮説関数 ($ h_\Theta(x) $ )。

\begin{align*} a_1^{(2)} = g(\Theta_{10}^{(1)}x_0 + \Theta_{11}^{(1)}x_1 + \Theta_{12}^{(1)}x_2 + \Theta_{13}^{(1)}x_3) \newline a_2^{(2)} = g(\Theta_{20}^{(1)}x_0 + \Theta_{21}^{(1)}x_1 + \Theta_{22}^{(1)}x_2 + \Theta_{23}^{(1)}x_3) \newline a_3^{(2)} = g(\Theta_{30}^{(1)}x_0 + \Theta_{31}^{(1)}x_1 + \Theta_{32}^{(1)}x_2 + \Theta_{33}^{(1)}x_3) \newline h_\Theta(x) = a_1^{(3)} = g(\Theta_{10}^{(2)}a_0^{(2)} + \Theta_{11}^{(2)}a_1^{(2)} + \Theta_{12}^{(2)}a_2^{(2)} + \Theta_{13}^{(2)}a_3^{(2)}) \newline \end{align*}

ただし

\begin{align*}& a_i^{(j)} = \text{"activation" of unit $i$ in layer $j$} \newline& \Theta^{(j)} = \text{層 j から j + 1 への対応の関数を制御する重みの行列}\end{align*}

とする。

ニューラルネットワークでのコスト関数

\begin{gather*} J(\Theta) = - \frac{1}{m} \sum_{i=1}^m \sum_{k=1}^K \left[y^{(i)}_k \log ((h_\Theta (x^{(i)}))_k) + (1 - y^{(i)}_k)\log (1 - (h_\Theta(x^{(i)}))_k)\right] + \frac{\lambda}{2m}\sum_{l=1}^{L-1} \sum_{i=1}^{s_l} \sum_{j=1}^{s_{l+1}} ( \Theta_{j,i}^{(l)})^2\end{gather*}

入力層、隠れ層、出力層があるニューラルネットワークでのコスト関数。

ただし、Lが層の数。${s_l}$ はl番目の層でのユニットの数(bias nodeは数えない)。Kを出力層でのユニットの数とする。

ロジスティクス解析に比べて、出力ユニット毎にKを添え字にして合計する必要があるのと、$\Theta^{(j)}$により、層ごとの計算が評価関数に入ることが違う。(評価関数の中身はここには見えない。)

regularization parameterについても層ごとと各層間での重みごとにシータを総和する。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3