PythonでDeepLearningを始めるよ
-
Upload
tanaka-yuichi -
Category
Technology
-
view
729 -
download
0
Transcript of PythonでDeepLearningを始めるよ
![Page 1: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/1.jpg)
© 2016 IBM Corporation
Python をつかってディープ・ラーニングの仕組みを見ていくよ
Tanaka Y.P2016-10-18
![Page 2: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/2.jpg)
© 2016 IBM Corporation2
お詫びと言い訳
(仮) Spark で試してみるディープ・ラーニング
昨今、人工知能関連で最大の話題である Deep Learningを、 Apache Spark を使って実装する方法について体験していただくハンズ・オン・セッションです。
Python をつかってディープ・ラーニングの仕組みを見ていくよ
昨今、人工知能関連で最大の話題である Deep Learningを、 Numpy を使って実装する方法について体験していただくハンズ・オン・セッションです。
![Page 3: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/3.jpg)
© 2016 IBM Corporation3
自己紹介田中裕一( yuichi tanaka)主にアーキテクチャとサーバーサイドプログラムを担当することが多い。 Hadoop/Spark周りをよく触ります。Node.js、 Python、最近は Spark周りの仕事で Scalaを書くことが多い気がします。
休日は OSS 周りで遊んだり。
詳解 Apache Spark
![Page 4: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/4.jpg)
© 2016 IBM Corporation4
DeepLearning とは
• 機会学習の一つである NN の層を重ね高精度な予測・分類処理を行うための手法• 色々な関数の層を重ねて精度を向上させる
• 画像識別の分野で精度が高くて有名( ResNet/GoogLeNet 等)
例) VGG16
![Page 5: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/5.jpg)
© 2016 IBM Corporation5
さっきのを簡略に書き直すと
Data Convolution Relu Affine Relu Softmax Out・・・
max poolin
g
VGG16 の簡略図
![Page 6: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/6.jpg)
© 2016 IBM Corporation6
今日のゴール
• 今日は最終的に下記の NN(fully-connected) を作成します。• 今日の例では特に層を厚くする意味もないですがせっかくなので DNN っぽく・・・
Data Affine Relu Affine Relu Softmax Out・・・ Affine
![Page 7: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/7.jpg)
© 2016 IBM Corporation7
今日の範疇
今日の話の中で説明すること
背景的な話 DeepLearning の全体像 レイヤ
- フォワード• アフィン変換
- 活性化関数について• ReLU 関数
- 出力層での正規化関数• ソフトマックス関数
バックプロパゲーション- 損失関数- 誤差伝搬について
ハイパーパラメータ
![Page 8: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/8.jpg)
© 2016 IBM Corporation8
今日の範疇
今日の話の中で説明しないこと
DeepLearning の種類的な違いとかの細かいこと- 畳み込みニューラルネットワーク: CNN @触れる程度で- 再帰ニューラルネットワーク: RNN- 教師なし学習: GAN- 強化学習: DQN
数式の話- 『うぇ』ってなるので数式なしで- 内積の話 @ アフィン変換- ネイピア数 @ ところどころ- 各関数の詳細 @ 特にバックプロパゲーションの関数
Batch Norm@ 各レイヤの値の分布調整 Dropout について@過学習とか
![Page 9: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/9.jpg)
© 2016 IBM Corporation9
背景
人間の脳の構造を模してやれば上手く識別できるのは?
ニューロン情報処理モデル
例)単純パーセプトロン
I
I
I
W
W
W
S O
Input1
Input2
Input3
output
参考)単純パーセプトロンの基本のき
Weight
![Page 10: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/10.jpg)
© 2016 IBM Corporation10
背景
層をふやしてやれば上手く識別できるのは?
例) FF-NN
I
I
I
Input1
Input2
Input3
output
O
O
![Page 11: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/11.jpg)
© 2016 IBM Corporation11
今日のサンプル(色の識別:3つに分類)
• 何色ですか?
RGB(255,0,0)
RGB(0,255,0)
RGB(0,0,255)
RGB(233,63,127)
赤
緑
青
赤
![Page 12: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/12.jpg)
© 2016 IBM Corporation12
今日のサンプル NN (色の識別)の用語説明
• こんな形の Fully-Connected を作成していきます。
Out・・・
・・・
・・・
・・・
・・・
入力層隠れ層
出力層
Softmax
![Page 13: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/13.jpg)
© 2016 IBM Corporation13
今日のサンプル NN (色の識別)の用語説明
• こんな形の Fully-Connected を作成していきます。
・・・
・・・
・・・
・・・
・・・
0 層(入力層 ) 1 層 4 層(出力層)2 層
計算処理 活性化関数 出力関数
レイヤ
Softmax Out
![Page 14: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/14.jpg)
© 2016 IBM Corporation14
今日のサンプル NN (色の識別)の用語説明
• こんな形の Fully-Connected を作成していきます。
・・・
Forward Backward
・・・
・・・
・・・
・・・
計算処理Affine 変換
活性化関数Relu 関数
出力関数Softmax 関数
OutSoftmax
![Page 15: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/15.jpg)
© 2016 IBM Corporation15
フォワード(入力レイヤ -> Affine )
122
66
94
n1
・・・
・・・
RGB の値でフォワードとバックプロパゲーションについて見ていきます。
入力値 : RGB(122, 66, 94) n1 :発火のしやすさ(バイアス)、各重み R の値 * R の重み G の値 * G の重み B の値 * B の重み
122
66
94
n1
R の重み
G の重み
B の重み
バイアス
![Page 16: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/16.jpg)
© 2016 IBM Corporation16
フォワード(入力レイヤ -> Affine )
バイアスと重みを使ってどのくらい発火するか計算します。
入力値 : RGB(122, 66, 94) n1 :バイアス、重み R の値 * R の重み G の値 * G の重み B の値 * B の重み
122
66
94
n1
R の重み
G の重み
B の重み
122
66
94
n1
R の重み
G の重み
B の重み
bias
バイアス
バイアス
(122 * R の重み ) + (66 * G の重み ) + (94 * B の重み) + バイアス
![Page 17: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/17.jpg)
© 2016 IBM Corporation17
フォワード(入力レイヤ -> Affine )
入力値と重みの内積にバイアスを足したもの
122
66
94
n1
n2
n3
・・・
・・・
bias
output = Input ・ weight + bias
122,66,94
n1R,n2R,n3Rn1G,n2G,n3
Gn1B,n2B,n3G
・・・
n1B,n2B,n3B, ・・・
python コード: out = x.dot(w) + b
n1O,n2O,n3O, ・・・
![Page 18: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/18.jpg)
© 2016 IBM Corporation18
フォワード( Affine -> Relu )
Affine の出力結果を活性化関数 Relu を使って活性化させます
・・・
・・・
・・・
s1 = np.array([[122, 66, 94]])w1 = np.array([[0.1], [0.2],[0.3]])s1.dot(w1) + [0.5]
[ 54.1]
例) Affine の重み・バイアスを決め打ちで計算
n154.1
![Page 19: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/19.jpg)
© 2016 IBM Corporation19
フォワード( Affine -> Relu )
Relu 関数は x<=0 なら 0 を x>0 なら x を返す。(超簡単)
Relu 関数の図
n154.1 54.1
phthon コード return np.maximum(0,x)
![Page 20: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/20.jpg)
© 2016 IBM Corporation20
フォワード( Affine -> 出力レイヤ)
Softmax 関数で最終値を扱いやすい形に変換
Softmax 関数
python コードe = np.exp(x)sum_e = e.sum()return e / sum_e
Softmax
n1
n2
n3
出力例 [0, 0.1, 5.3] => [ 0.00493969, 0.0054592 , 0.98960111]
98% の確率で青と判定 !
![Page 21: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/21.jpg)
© 2016 IBM Corporation21
バックプロパゲーション
• 各 node の重みとバイアスは?
・・・
Backward
・・・
・・・
・・・
・・・
OutSoftmax
![Page 22: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/22.jpg)
© 2016 IBM Corporation22
バックプロパゲーション(損失関数)
• 各 node の重みとバイアスは?
・・・
Backward
・・・
・・・
OutSoftmax
RGB(122, 66, 94)教師 Label [1,0,0]
初期値を当てる
Forward
教師 Label [1,0,0]
出力[0.3,0.28,0.32]
損失関数
誤差修正
交差エントロピー誤差
![Page 23: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/23.jpg)
© 2016 IBM Corporation23
バックプロパゲーション(誤差伝搬)
・・・
・・・
・・・
・・・
・・・
Softmax
Backward
本来は勾配という微分計算を全パラメータに対して行う必要があるところ、誤差逆伝搬法を使うことで出力層側から入力層側に向かって単純計算だけでパラメータの更新を行います。
![Page 24: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/24.jpg)
© 2016 IBM Corporation24
バックワード(出力レイヤ (Softmax + 交差エントロピー誤差) )
Softmax 関数 + 交差エントロピー誤差
yk - tk
python コードdx = (self.y - self.t)
Softmax
n1
n2
n3
ここからはバックワードする際のコードと式を紹介だけします。
Softmax
y1 – t1
y2 – t2
y3 – t3
損失関数
![Page 25: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/25.jpg)
© 2016 IBM Corporation25
バックワード( Affine -> Relu )
python コードdx = dout.dot(self.w.T) # 出力w = dx.T.dot(dout) # 重みb = dout.sum(axis=0) # バイアス
Affine 変換の式は重み・バイアス・入力の3値を計算します
・・・
・・・
Affine 変換の重み・バイアス
![Page 26: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/26.jpg)
© 2016 IBM Corporation26
バックワード( Relu -> Affine )
python コード dout[self.mask] = 0
Relu 関数は値を反転してあげるだけ
・・・
・・・
![Page 27: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/27.jpg)
© 2016 IBM Corporation27
フォワードとバックワード
Data Affine Relu Affine Relu Softmax Out・・・ Affine
Forward
Forward Forward Forward Forward Forward Forward
Backward
BackwardBackwardBackwardBackwardBackwardBackward
• Foward -> Backword を繰り返し処理を行うことで重み・バイアスの更新が可能になります
![Page 28: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/28.jpg)
© 2016 IBM Corporation28
もう一度 CNN を見てみよう
Data Convolution Relu Affine Relu Softmax Out・・・
max poolin
g
VGG16 の簡略図
![Page 29: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/29.jpg)
© 2016 IBM Corporation29
ハイパーパラメータについて(層の数と Node の数)
・・・
・・・
・・・
・・・
・・・
Softmax
• NN では各関数の重みやバイアスを自動で調整していく事で精度を上げます• では NN ではパラメータの調整は全て不要なのでしょうか?
全体で幾つの層を重ねるのか?
1層に幾つのNode を作るの
か?
![Page 30: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/30.jpg)
© 2016 IBM Corporation30
ハイパーパラメータについて(ネットワーク全体の学習率)
Affine 変換の重み・バイアス
・・・
・・・
・・・
・・・
・・・
Softmax
Affine 変換の重み・バイアス
Affine 変換の重み・バイアス
どのぐらいの強さで重み・バイアスを更新するのか?
学習率学習率学習率
![Page 31: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/31.jpg)
© 2016 IBM Corporation31
今日のサンプルプログラム
・・・
・・・
・・・
・・・
・・・
Softmax
Affine 変換の重み・バイアス
学習率 :0.01
全体4層
50 100 503 3
入力データ数train: 240test: 20
![Page 32: PythonでDeepLearningを始めるよ](https://reader036.fdocument.pub/reader036/viewer/2022070516/586f858f1a28ab54768b512d/html5/thumbnails/32.jpg)
© 2016 IBM Corporation32
DataScientistExperienceへ
Notebookはこちらそれではコードに移ります