tensorflow 深度學習快速上手班--深度學習
Post on 16-Apr-2017
2.425 Views
Preview:
TRANSCRIPT
TensorFlow 深度學習快速上手班二、深度學習By Mark Chang
• 深度學習的原理• 模型選擇與參數調整• 多層感知器實作
深度學習的原理
機器學習監督式學習
Supervised Learning
非監督式學習Unsupervised Learning
增強式學習Reinforcement Learning
深度學習Deep Learning
深度學習• 一種機器學習的方法• 用電腦模擬人腦神經系統構造• 讓電腦學會人腦可做的事
神經元與動作電位
http://humanphisiology.wikispaces.com/file/view/neuron.png/216460814/neuron.png
http://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Action_potential.svg/1037px-Action_potential.svg.png
模擬神經元
nW1
W2
x1
x2
bWb
y
nin
nout
(0,0)
x2
x1
模擬神經元
1
0
二元分類: AND Gate
x1 x2 y0 0 00 1 01 0 01 1 1
(0,0)
(0,1) (1,1)
(1,0)
01
n2020
b-30
yx1x2
XOR Gate ?
(0,0)
(0,1) (1,1)
(1,0)0
01
x1 x2 y0 0 00 1 11 0 11 1 0
二元分類: XOR Gate
n-20
20
b
-10
y
(0,0)
(0,1)
(1,1)
(1,0)
01
(0,0)
(0,1) (1,1)
(1,0)
10
(0,0)
(0,1) (1,1)
(1,0)0
01
n1
2020
b-30
x1x2
n2
2020
b-10
x1x2
x1 x2 n1
n2
y
0 0 0 0 00 1 0 1 11 0 0 1 11 1 1 1 0
類神經網路
x
y
n11
n12
n21
n22W12,y
W12,x
b
W11,y
W11,bW12,b
b
W11,x W21,11
W22,12
W21,12
W22,11
W21,bW22,b
z1
z2
Input Layer
HiddenLayer
OutputLayer
視覺認知
http://www.nature.com/neuro/journal/v8/n8/images/nn0805-975-F1.jpg
訓練類神經網路• 用隨機值初始化模型參數 w• Forward Propagation– 用目前的模型參數計算出答案
• 計算錯誤量(用 Error Function )• Backward Propagation– 用錯誤量來修正模型
長期記憶
http://www.pnas.org/content/102/49/17846/F7.large.jpg
訓練類神經網路
訓練資料 機器學習模型 輸出值
正確答案
對答案如果答錯了,要修正模型
初始化 Forward Propagation
ErrorFunction
BackwardPropagation
初始化• 將所有的 W 隨機設成 -N ~ N 之間的數
• 每層之間 W 的值都不能相同
x
y
n11
n12
n21
n22W12,y
W12,x
b
W11,y
W11,bW12,b
b
W11,x W21,11
W22,12
W21,12
W22,11
W21,bW22,b
z1
z2
Lk-1 :上一層的大小Lk :該層的大小
Forward Propagation
Forward Propagation
Error Function
n21
n22
z1
z2
w1 w0
Gradient Descent
Backward Propagation
Backward Propagation
Backward Propagation
Backward Propagation
Backward Propagation
Backward Propagation
Backward Propagation
Backward Propagation
http://cpmarkchang.logdown.com/posts/277349-neural-network-backward-propagation
模型選擇與參數調整
模型種類• 非線性轉換
Sigmoid:
nW1
W2
x1x2
b Wb
tanh:
ReLU:
模型種類• Hidden Layer
較小的 Hidden Layer
較大的 Hidden Layer
多層 Hidden Layer單層 Hidden Layer
模型複雜度• 模型中的參數個數( weight 和 bias 的個數)
模型複雜度低 高
訓練不足與過度訓練Tensorflow Playground
http://playground.tensorflow.org/
資料分佈
訓練適度 訓練不足 訓練過度
訓練不足( Underfitting )• 原因:– Learning Rate 太大或太小– 訓練時間太短– 模型複雜度不夠
t
過度訓練( Overfitting )• 原因:– 雜訊太多– 訓練資料太少– 訓練時間太長– 模型複雜度太高
t
驗證資料( Validation Data )
訓練資料模型 1
測試資料 最後結果
資料集
驗證資料 模型選擇參數選擇時間控制
模型 2
……
交叉驗證( Cross Validation )訓練資料驗證資料
訓練資料
訓練資料
驗證資料
驗證資料
第一回
第二回
第 N回
……
解決方式• 訓練不足– 調整 Learning Rate– 增加訓練時間– 增加模型複雜度
• 訓練過度– 增加訓練資料– 減少雜訊– 減少訓練時間– 減少模型複雜度
調整 Learning Rate• 調整 Learning Rate 數值
Learning Rate適中
Learning Rate過小
Learning Rate過大
調整 Learning Rate• 動態調整 Learning Rate :– AdagradOptimizer– RMSPropOptimizer–……
調整訓練時間• Early Stop
Validation LossTraining Loss
停止訓練t
調整模型複雜度• 調整 Hidden Layer 的寬度或層數• Regularization• Dropout
Hidden Layer 寬度
0 1 2 3 4 5 6 7 8 90
0.050.1
0.150.2
0.250.3
0.350.4
0.450.5
Validation LossTraining Loss
最適寬度
模型複雜度低 高
Loss
寬度
Regularization• 將 weights 的平方和加到 cost function 中• 可使 weights 的絕對值不要變得太大• 可降低模型複雜度
Cost Function:
λ 越大,則模型複雜度越低
Regularization最適 λ 值
模型複雜度 低高0.01 0.1 1 100
0.05
0.1
0.15
0.2
0.25
0.3
0.35
Validation LossTraining LossLo
ss
λ
Dropout• 訓練時,隨機將 Hidden Layer 的神經元拿掉• 可降低模型複雜度• ex: 25% 的 Dropout Rate
Dropout• 測試時,用所有的神經元來測試。– 將所有的 weight 乘上 (1 – dropout_rate)
Dropout
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.050.1
0.150.2
0.250.3
0.350.4
0.450.5
Validation ErrorTraining Error
最適 dropout rate
1- dropout rate
Erro
r
模型複雜度低 高
模型選擇與參數調整實作• Tensorflow Playground– http://playground.tensorflow.org/
模型選擇與參數調整實作• 訓練不足( UnderFitting )
模型選擇與參數調整實作• 過度訓練( OverFitting )
多層感知器實作
多層感知器實作https://github.com/ckmarkoh/ntc_deeplearning_tensorflow/blob/master/sec2/multilayer_perceptron.ipynb
MNIST• 數字識別• 多元分類: 0~9
https://www.tensorflow.org/versions/r0.7/images/MNIST.png
模型• 多層感知器
Input LayerSize:784
Hidden Layer
Size:200
Output Layer
Size:10
Computational Graphx_ = tf.placeholder(tf.float32, [None, 784], name="x_")y_ = tf.placeholder(tf.float32, [None, 10], name="y_")
# input -> HiddenW1 = tf.Variable(tf.truncated_normal([784,200], stddev=0.1), name="W1")b1 = tf.Variable(tf.zeros([200]), name="b1")h1 = tf.nn.sigmoid(tf.matmul(x_, W1) + b1)# Hidden -> OutputW2 = tf.Variable(tf.truncated_normal([200,10], stddev=0.1), name="W2")b2 = tf.Variable(tf.zeros([10]), name="b2")y = tf.nn.softmax(tf.matmul(h1, W2) + b2)
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))optimizer = tf.train.GradientDescentOptimizer(0.01)trainer = optimizer.minimize(cross_entropy)init = tf.initialize_all_variables()
Layer 1W1 = tf.Variable(tf.truncated_normal([784,200], stddev=0.1), name="W1”)
0
1000
2000
3000
4000
5000
6000
7000
-0.2 0.20
Layer 1W1 = tf.Variable(tf.truncated_normal([784,200], stddev=0.1), name="W1")b1 = tf.Variable(tf.zeros([200]), name="b1")h1 = tf.nn.sigmoid(tf.matmul(x_, W1) + b1)
W1x
b1
h1n
784
n
200
200
200
784× + =
Layer 2
wb
h1n 1010
200
200× + =
y
10
n
W2 = tf.Variable(tf.truncated_normal([200,10], stddev=0.1), name="W2")b2 = tf.Variable(tf.zeros([10]), name="b2")y = tf.nn.softmax(tf.matmul(h1, W2) + b2)
Regularizationlambda_ = tf.placeholder(tf.float32, name="lambda")regularizer = tf.reduce_sum(tf.square(W1))+tf.reduce_sum(tf.square(W2))cost = cross_entropy + lambda_*regularizer
Cost Function:
Regularizationhttps://github.com/ckmarkoh/ntc_deeplearning_tensorflow/blob/master/sec2/regularization.ipynb
dropoutkeep_prob = tf.placeholder(tf.float32, name="keep_prob")h1_drop = tf.nn.dropout(h1, keep_prob)y = tf.nn.softmax(tf.matmul(h1_drop, W2) + b2)
1
0
1
0Dropout
Mask
dropouthttps://github.com/ckmarkoh/ntc_deeplearning_tensorflow/blob/master/sec2/dropout.ipynb
模型儲存與載入• 儲存模型參數
• 載入模型參數saver = tf.train.Saver(max_to_keep=10)saver.save(sess, "model.ckpt")
saver = tf.train.Saver()saver.restore(sess, "model.ckpt")
66
講師資訊• Email: ckmarkoh at gmail dot com• Blog: http://cpmarkchang.logdown.com• Github: https://github.com/ckmarkoh
Mark Chang
• Facebook: https://www.facebook.com/ckmarkoh.chang
• Slideshare: http://www.slideshare.net/ckmarkohchang• Linkedin:
https://www.linkedin.com/pub/mark-chang/85/25b/847
top related