SSブログ

いよいよNeural Networksに取り組む [Octave]

(2018.09.20)
Machine Learning(↓)の受講を始めて3週間近く経ちました。
https://www.coursera.org/learn/machine-learning/home/welcome

途中経過はこんな感じ(↓)です。
9/3 受講開始
9/10 第1週の課題提出(<==期限ギリギリ)
9/17 第2週の課題提出(<==期限ギリギリ)
9/19 第3週の課題提出(<==余裕の提出)
9/20 第4週の課題提出(<==勢い付いて連続提出) 

ここに来て調子付いてます。¥(・_・)

2018_0920_1.png手書きの数字
いよいよニューラル・ネットワークで手書き文字の認識!と張り切ったのですが、
最初の課題は、ロジスティック回帰分析を使ったマルチクラスの分類でした。

先週の課題で分かったつもりになっていた『ロジスティック回帰分析』でしたが、実際にマルチクラスの分類をやらされて、四苦八苦しました。

それでも、何とかlrCostFunction.mを書き上げて(<==カンニングしてません)実行すると・・・
Testing lrCostFunction() with regularization
Cost: 2.534819
Expected cost: 2.534819
Gradients:
0.146561
-0.548558
0.724722
1.398003
Expected gradients:
0.146561
-0.548558
0.724722
1.398003
Program paused. Press enter to continue.

実行した結果とExpectedが一致しました。
パチパチパチ~

lrCostFunction.mの回答はこんな感じ(↓)
hx = sigmoid(X * theta );
theta(1) = 0;
J = -sum(y.*log(hx)+(1-y).*log(1-hx))/m + lambda/(2*m)*(sum(theta.^2));
grad = X'*(hx - y)/m + lambda/m*theta;

マルチクラスの分類でも合否判定(二値)クラスの分類と同じコスト関数です。
当然と言えば当然なんですが・・・添え字でループ回したらこうは行きません。
あらためてベクトル表記の威力を感じます。

続いて、20×20ピクセル画像5000枚を教師付き学習(<==Θベクトルを最適化)するonevsAll.mを書き上げて(<==カンニングしてません)実行すると・・・
Training One-vs-All Logistic Regression...
Iteration 50 | Cost: 1.405529e-02
Iteration 50 | Cost: 5.725224e-02
Iteration 50 | Cost: 6.397841e-02
Iteration 50 | Cost: 3.810361e-02
Iteration 50 | Cost: 6.186249e-02
Iteration 50 | Cost: 2.205048e-02
Iteration 50 | Cost: 3.554620e-02
Iteration 50 | Cost: 8.549827e-02
Iteration 50 | Cost: 7.939720e-02
Iteration 50 | Cost: 9.886766e-03
Program paused. Press enter to continue.

何か結果は出ますが、この段階では正否は分かりません。

さらに結果を判定するpredictOneVsAl.mを書いて実行した結果は・・・
Training Set Accuracy: 9.80000 <== 認識率9.8% orz

どこに間違いがあるのか????

最初は何も考えず、ex2からコピーしたコード(↓)をpredictOneVsAl.mに書きました。
hx = sigmoid(X * theta);
for i=1:m
if( hx(i) >= 0.5 )p(i)=1;
endif
endfor

改めて、predictOneVsAl.mに書かれた指示を読み直すと・・・
『You should set p to a vector of predictions (from 1 to num_labels).』
『Hint: This code can be done all vectorized using the max function.』

今ならすんなり読み取れますが、最初は回答のことを考えながら目で英文を追いかけるので意味が読み取れません。hxの中(5000×10ベクトル)を調べなおして、漸く指示(↑)の意味が分かってきました。

書きなおしたpredictOneVsAl.mのコードがこれ(↓)です。
hx = sigmoid( X1 * all_theta' );
for i=1:m
[ w,iw ] = max ( hx(i,:) );
p(i)=iw;
endfor

で、改めてex3を実行してみると・・・
Training Set Accuracy: 95.000000
パチパチパチ~

if( hx(i) >= 0.5 )p(i)=iw;とすると89%に下がります。どちらが正しいのか?分かっていません。

勢いに乗ってpredict.mを書き上げex3_nnを実行すると・・・
Loading and Visualizing Data ...
Program paused. Press enter to continue.

Loading Saved Neural Network Parameters ...

Training Set Accuracy: 97.520000
Program paused. Press enter to continue.

ロジスティック回帰の認識率が95.00%
ニューラル・ネットワークの認識率が97.52%
思ったより地味な結果です。
しかしこの後、さらに驚きの結果が待ち受けていました。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
管理人もお世話になっています(↓)


ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

>> submit
== Submitting solutions | Multi-class Classification and Neural Networks...
Use token from last successful submission :
Iteration 33 | Cost: 3.391340e-01
Iteration 23 | Cost: 6.631438e-02
Iteration 34 | Cost: 1.002636e-01
Iteration 20 | Cost: 3.598119e-01
Iteration 50 | Cost: 2.353348e-07
Iteration 50 | Cost: 2.353348e-07
Iteration 50 | Cost: 2.353348e-07
Iteration 50 | Cost: 2.353348e-07
Iteration 50 | Cost: 2.353348e-07
Iteration 50 | Cost: 2.353348e-07
==
== Part Name | Score | Feedback
== --------- | ----- | --------
== Regularized Logistic Regression | 30 / 30 | Nice work!
== One-vs-All Classifier Training | 0 / 20 | <==何故!
== One-vs-All Classifier Prediction | 20 / 20 | Nice work!
== Neural Network Prediction Function | 30 / 30 | Nice work!
== --------------------------------
== | 80 / 100 |
==

自力でここまで(なんとか)やってきたのですが、この結果に打ちのめされました。orz

しかし、『困った時のGoogle先生』で他の方の回答を調べても、間違いは見つかりませんでした。
これは一体どういうことなのでしょうか?

暫し悩んで・・・

まぁ、達成率80%でも合格ですから気にせず先に進みます。(<==結果オーライな奴)

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
管理人もお世話になっています(↓)


nice!(1)  コメント(0) 
共通テーマ:日記・雑感

nice! 1

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

※ブログオーナーが承認したコメントのみ表示されます。

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。