4 TensorFlow入門之dropout解決overfitting問題

————————————————————————————————————

寫在開頭:此文參照莫煩python教程(牆裂推薦!!!)

————————————————————————————————————

dropout解決overfitting問題

  • overfitting:當機器學習學習得太好了,就會出現過擬合(overfitting)問題。所以,我們就要採取一些措施來避免過擬合的問題。此實驗就來看一下dropout對於解決過擬合問題的效果。
  • 例子實驗內容:識別手寫數字。此實驗的步驟和上一篇的識別手寫數字步驟很相似。
  • 例子實驗的數據集:sklearn中的datasets

  • 主要運用的函數tf.nn.dropout()

  • 主要參數keep_prob。keep_prob表示留下來的結果的百分比,比如你要drop0.4,那麼keep_prob就爲0.6
import tensorflow as tf
from sklearn.datasets import load_digits
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelBinarizer

#加載數據
digits = load_digits()
X = digits.data
y = digits.target
y = LabelBinarizer().fit_transform(y)  #把數字變成1x10的向量
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = .3)  #把數據分成train數據和test數據

#定義添加層
def add_layer(inputs,in_size,out_size,activation_function=None):
    #定義添加層內容,返回這層的outputs
    Weights = tf.Variable(tf.random_normal([in_size,out_size]))#Weigehts是一個in_size行、out_size列的矩陣,開始時用隨機數填滿
    biases = tf.Variable(tf.zeros([1,out_size])+0.1) #biases是一個1行out_size列的矩陣,用0.1填滿
    Wx_plus_b = tf.matmul(inputs,Weights)+biases  #預測
    #實現dropout,keep_drop爲丟棄後剩下的百分比
    Wx_plus_b = tf.nn.dropout(Wx_plus_b, keep_prob)
    if activation_function is None:  #如果沒有激勵函數,那麼outputs就是預測值
        outputs = Wx_plus_b
    else:  #如果有激勵函數,那麼outputs就是激勵函數作用於預測值之後的值
        outputs = activation_function(Wx_plus_b)
    return outputs

#定義計算正確率的函數
def t_accuracy(t_xs,t_ys):
    global prediction
    y_pre = sess.run(prediction,feed_dict={xs:t_xs,keep_prob:1})#測試結果不dropout
    correct_pre = tf.equal(tf.argmax(y_pre,1),tf.argmax(t_ys,1))
    accuracy = tf.reduce_mean(tf.cast(correct_pre,tf.float32))
    result = sess.run(accuracy,feed_dict={xs:t_xs,ys:t_ys,keep_prob:1})
    return result

#定義輸入輸出值,和keep_drop值
keep_prob = tf.placeholder(tf.float32)
xs = tf.placeholder(tf.float32, [None, 64])  # 8x8
ys = tf.placeholder(tf.float32, [None, 10])

#添加層
l1 = add_layer(xs, 64, 50,activation_function=tf.nn.tanh)
prediction = add_layer(l1, 50, 10,activation_function=tf.nn.softmax)

#誤差
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),reduction_indices=[1]))  # loss

#訓練
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

#開始訓練
sess = tf.Session()
merged = tf.summary.merge_all()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(1000):
    # 設置keep_drop爲1,即不進行dropout
    sess.run(train_step, feed_dict={xs: X_train, ys: y_train, keep_prob: 1})
    if i % 50 == 0:
        # 輸出正確率
        print (t_accuracy(X_test,y_test)) 
0.20925926
0.7574074
0.81296295
0.8388889
0.85555553
0.8537037
0.84814817
0.8537037
0.85555553
0.8537037
0.85555553
0.8537037
0.8574074
0.85555553
0.8574074
0.8574074
0.8611111
0.8574074
0.85925925
0.8611111
for i in range(1000):
    # 設置keep_drop爲0.5
    sess.run(train_step, feed_dict={xs: X_train, ys: y_train, keep_prob: 0.5})
    if i % 50 == 0:
        # 輸出正確率
        print (t_accuracy(X_test,y_test)) 
0.86851853
0.89444447
0.91481483
0.9166667
0.91481483
0.9222222
0.9259259
0.9222222
0.9296296
0.94074076
0.94074076
0.9351852
0.9351852
0.9351852
0.9351852
0.93333334
0.94074076
0.9351852
0.93703705
0.9351852

由上面的結果可知,當dropout爲0.5時,效果明顯比一點兒也不丟棄的好!


*點擊[這兒:TensorFlow]發現更多關於TensorFlow的文章*


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章