Processing math: 8%
Machine Learning
Stanford Univ, Coursera
Logistic Regression Model
Cost Function
linear regression (線形回帰)と同じ cost functionは使えない。
ロジスティック関数は convex (凸)ではないので、
すなわち波打ったような出力を生成するので、
たくさんの局所最適解を持つからである。
ロジスティック関数に対しては、次のような cost function を用いる。
Logistiec Function 用のCost Function |
J(θ)=1mm∑i=1Cost(hθ(x(i))),y(i))Cost(hθ(x(i)),y(i))=−log(hθ(x(i)))ify=1Cost(hθ(x(i)),y(i))=−log(1−hθ(x(i)))ify=0
|
すると、Cost Function は次のような性質を持つ。
Cost Function の性質 |
Cost(hθ(x(i)),y(i))=−0ifhθ(x(i))=yCost(hθ(x(i)),y(i))→∞ify=0andhθ(x(i))→1Cost(hθ(x(i)),y(i))→∞ify=1andhθ(x(i))→0
|
Simplified Cost Function and Gradient Descent
Cost Function はまとめると次のように書くことができる。
Cost(hθ(x(i)),y(i))=−ylog(hθ(x(i)))−(1−y)log(1−hθ(x(i)))
|
Cost Function 全体を次のように定義できる。
J(θ)=1mm∑i=1(−ylog(hθ(x(i)))−(1−y)log(1−hθ(x(i))))
|
ベクトル化した実装は以下の通り。
\displaystyle
\begin{eqnarray}
\boldsymbol{h} & = & g(X \boldsymbol{\theta}) \\
J( \boldsymbol{\theta} ) & = & \frac{1}{m} (
- \boldsymbol{y}^T \log ( \boldsymbol{h} )
- (\boldsymbol{1} - \boldsymbol{y})^T \log (\boldsymbol{1} - \boldsymbol{h})
) \\
\end{eqnarray}
|
Gradient Descent
次の式を繰り返す
|
\displaystyle
\begin{eqnarray}
{\theta}_j & := & {\theta}_j - \alpha \frac{\partial}{\partial {\theta}_j} J(\theta) \\
\end{eqnarray}
|
\displaystyle
\begin{eqnarray}
{\theta}_j & := & {\theta}_j - \frac{\alpha}{m} \sum_{i=1}^m (h_{\theta}(x^{(i)}) - y^{(i)}) x_j^{(i)} \\
\end{eqnarray}
|
ベクトル化
|
\displaystyle
\begin{eqnarray}
\boldsymbol{\theta} & := & \boldsymbol{\theta} - \frac{\alpha}{m} X^T (G(X \boldsymbol{\theta}) - \boldsymbol{y}) \\
\end{eqnarray}
|
Week3, "Simplified Cost Function and Gradient Descent"
(3-5 のビデオ) の8:50 あたりで
Gradient Descend を用いて
\theta_j を更新していく
\theta_j = \theta_j - \alpha
\displaystyle \sum_{i=1}^m (h_{\theta}(x^{(i)}) - y^{(i)}) x_j^{(i)}
という数式が提示されているが、この
(h_{\theta}(x^{(i)}) - y^{(i)})
の部分が
\displaystyle \frac{\partial Cost}{\partial z}
に相当する。
\begin{eqnarray}
Cost &=& -y \log h - (1-y) \log (1-h) \\
&=& -y \log \displaystyle \frac{1}{1+e^{-z}} - (1-y) \log (1 - \displaystyle \frac{1}{1+e^{-z}}) \\
&=& -y \log \displaystyle \frac{e^z}{1+e^z} - (1-y) \log \displaystyle \frac{1}{1+e^z} \\
&=& -y (\log e^z -\log (1+e^z)) - (1-y) (\log 1 - \log (1+e^z)) \\
&=& -y z + (y + (1 - y)) \log (1+e^z) \\
&=& -y z + \log(1+e^z)
\end{eqnarray}
\begin{eqnarray}
\displaystyle \frac{\partial Cost}{\partial z} &=& -y + \frac{e^z}{1+e^z} \\
&=& -y + h
\end{eqnarray}
\begin{eqnarray}
\displaystyle \frac{\partial Cost}{\partial \theta_j} &=&
\frac{\partial Cost}{\partial z} \cdot \frac{\partial z}{\partial \theta_j} \\
&=& (-y + \frac{e^z}{1+e^z}) \cdot x_j \\
&=& (-y + h_{\theta}(x)) \cdot x_j \\
\end{eqnarray}
Advanced Optimization
- Gradient descent
- Conjugate gradient
- BFGS
- L-BFGS
例1
\displaystyle
\begin{eqnarray}
\boldsymbol{\theta} & = &
\begin{pmatrix}
{\theta}_0 \\
{\theta}_1
\end{pmatrix} \\
J(\boldsymbol{\theta}) & = & ({\theta}_1 - 5)^2 + ({\theta}_2 - 5)^2 \\
\frac{\partial}{\partial {\theta}_1}
J(\boldsymbol{\theta}) & = & 2 ({\theta}_1 - 5) \\
\frac{\partial}{\partial {\theta}_2}
J(\boldsymbol{\theta}) & = & 2 ({\theta}_2 - 5) \\
\end{eqnarray}
Cost Function (octave) |
function [jVal, gradient] = costFunction(theta)
jVal = (theta(1) - 5)^2 + (theta()-5)^2;
gradient = zeros(2,1);
gradient(1) = 2 * (theta(1) - 5);
gradient(2) = 2 * (theta(2) - 5);
|
例2
\displaystyle
\begin{eqnarray}
\boldsymbol{\theta} & = &
\begin{pmatrix}
{\theta}_0 \\
{\theta}_1 \\
\vdots
{\theta}_n \\
\end{pmatrix} \\
J(\boldsymbol{\theta}) & = & ({\theta}_1 - 5)^2 + ({\theta}_2 - 5)^2 \\
\frac{\partial}{\partial {\theta}_1}
J(\boldsymbol{\theta}) & = & 2 ({\theta}_1 - 5) \\
\frac{\partial}{\partial {\theta}_2}
J(\boldsymbol{\theta}) & = & 2 ({\theta}_2 - 5) \\
\vdots \\
\frac{\partial}{\partial {\theta}_n}
J(\boldsymbol{\theta}) & = & 2 ({\theta}_n - 5) \\
\end{eqnarray}
Cost Function (octave) |
function [jVal, gradient] = costFunction(theta)
jVal = (theta(1) - 5)^2 + (theta()-5)^2;
gradient = zeros(2,1);
gradient(1) = 2 * (theta(1) - 5);
gradient(2) = 2 * (theta(2) - 5);
...
gradient(n+1) = 2 * (theta(n+1) - 5);
|
Octave の
"optimset()" 関数を呼び出すことで、"fminunc()" 最適化アルゴリズムを使う。
Cost Function (octave) |
options = optimset('GradObj', 'on', 'MaxIter', 100);
initialTheta = zeros(2,1);
[optTheta, functionVal, exitFlag] = fminunc(@costFunction, initialTheta, options);
|
3-4-1 test
cost function J(\theta) を最小化することを考える。convex(凸)な関数はどれか。
- □ 下に2つ山のグラフ
- 〆 下に凸の放物線
- □ 下に2つ山のグラフ
- □ 下に沢山の山があるグラフ
3-4-2 test
logistic regression でcost function
\displaystyle
cost(h_{\theta}(x), y) =
\left\{
\begin{array}{ll}
- \log h_{\theta}(x) & if ~~ y=1 \\
- \log (1 - h_{\theta}(x) ) & if ~~ y=0
\end{array}
\right.
正しい文をすべて選べ
- 〆 (1) h=yのとき、y=0にせよy=1にせよ cost(h)=0
y=0のとき: h=0 のとき cost(0)=0
y=1のとき: h=1 のとき cost(1)=0
〆 (2) y=0 のとき、h \rightarrow 1 とすると cost(h) \rightarrow +\infty
□ (3) y=0 のとき、h \rightarrow 0 とすると cost(h) \rightarrow +\infty
〆 (4) y=0 にせよ y=1 にせよ、 h=0.5 のとき cost(h) > 0
3-5-1 test
パラメータ \theta \in R^{n-1} を持つ logistic regression モデルを gradient descent で訓練する。
learning rate \alpha が適切に設定されていて gradient descent が正しく動作していることを確かめる方法はどれか?
- □ 繰り返し回数の関数として、J(\theta) = \frac{1}{m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)}) -y^{(i)})^2 をプロットして、繰り返しの度に J(\theta)が減少していることを確認する。
- 〆 繰り返し回数の関数として、J(\theta) = -\frac{1}{m} \sum_{i=1}^{m} [y^{(i)} \log h_{\theta}(x) + (1-y^{(i)}) \log (1 - h_{\theta}(x^{(i)}) ] をプロットして、繰り返しの度に J(\theta)が減少していることを確認する。
← logistic regression に対する正しいJ(\theta)の式の値が、繰り返しごとに減っていることを確認する。
- □ \thetaの関数として、J(\theta) をプロットして、繰り返しの度に J(\theta)が減少していることを確認する。
- □ \thetaの関数として、J(\theta) をプロットして、グラフがconvex (凸)であることを確認する。
3-5-2 test
gradient descent の1回の繰返しで同時に \theta_0, \theta_1, cdots, \theta_n をupdateする。
\theta := \\theta - \alpha \rho をベクトル化による実装をしたい。vector化実装は次のどれか?
- 〆 \theta := \theta - \alpha \frac{1}{m} \sum_{i=1}^{m} [ (h_{\theta}(x^{(i)}) - y^{(i)}) \cdot x^{(i)}]
- □ \theta := \theta - \alpha \frac{1}{m} \sum_{i=1}^{m} [ (h_{\theta}(x^{(i)}) - y^{(i)}) ]\cdot x^{(i)}
- □ \theta := \theta - \alpha \frac{1}{m} x^{(i)} \sum_{i=1}^{m} [ (h_{\theta}(x^{(i)}) - y^{(i)}) ]
- □ 上の式はどれも正しい実装ではない
Yoshihisa Nitta
http://nw.tsuda.ac.jp/