python人工智能tensorflow函數(shù)tf.nn.dropout使用方法
前言
神經(jīng)網(wǎng)絡(luò)在設(shè)置的神經(jīng)網(wǎng)絡(luò)足夠復(fù)雜的情況下,可以無限逼近一段非線性連續(xù)函數(shù),但是如果神經(jīng)網(wǎng)絡(luò)設(shè)置的足夠復(fù)雜,將會(huì)導(dǎo)致過擬合(overfitting)的出現(xiàn),就好像下圖這樣。

看到這個(gè)藍(lán)色曲線,我就知道:
很明顯藍(lán)色曲線是overfitting的結(jié)果,盡管它很好的擬合了每一個(gè)點(diǎn)的位置,但是曲線是歪歪曲曲扭扭捏捏的,這個(gè)的曲線不具有良好的魯棒性,在實(shí)際工程實(shí)驗(yàn)中,我們更希望得到如黑色線一樣的曲線。
tf.nn.dropout函數(shù)介紹
tf.nn.dropout是tensorflow的好朋友,它的作用是為了減輕過擬合帶來的問題而使用的函數(shù),它一般用在每個(gè)連接層的輸出。
Dropout就是在不同的訓(xùn)練過程中,按照一定概率使得某些神經(jīng)元停止工作。也就是讓每個(gè)神經(jīng)元按照一定的概率停止工作,這次訓(xùn)練過程中不更新權(quán)值,也不參加神經(jīng)網(wǎng)絡(luò)的計(jì)算。但是它的權(quán)重依然存在,下次更新時(shí)可能會(huì)使用到它。
def dropout(x, keep_prob, noise_shape=None, seed=None, name=None)
x 一般是每一層的輸出
keep_prob,保留keep_prob的神經(jīng)元繼續(xù)工作,其余的停止工作與更新
在實(shí)際定義每一層神經(jīng)元的時(shí)候,可以加入dropout。
def add_layer(inputs,in_size,out_size,n_layer,activation_function = None,keep_prob = 1):
layer_name = 'layer%s'%n_layer
with tf.name_scope(layer_name):
with tf.name_scope("Weights"):
Weights = tf.Variable(tf.random_normal([in_size,out_size]),name = "Weights")
tf.summary.histogram(layer_name+"/weights",Weights)
with tf.name_scope("biases"):
biases = tf.Variable(tf.zeros([1,out_size]) + 0.1,name = "biases")
tf.summary.histogram(layer_name+"/biases",biases)
with tf.name_scope("Wx_plus_b"):
Wx_plus_b = tf.matmul(inputs,Weights) + biases
#dropout一般加載每個(gè)神經(jīng)層的輸出
Wx_plus_b = tf.nn.dropout(Wx_plus_b,keep_prob)
#看這里看這里,dropout在這里。
tf.summary.histogram(layer_name+"/Wx_plus_b",Wx_plus_b)
if activation_function == None :
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
tf.summary.histogram(layer_name+"/outputs",outputs)
return outputs
但需要注意的是,神經(jīng)元的輸出層不可以定義dropout參數(shù)。因?yàn)檩敵鰧泳褪禽敵龅氖墙Y(jié)果,在輸出層定義參數(shù)的話,就會(huì)導(dǎo)致輸出結(jié)果被dropout掉。
例子
本次例子使用sklearn.datasets,在進(jìn)行測試的時(shí)候,我們只需要改變最下方keep_prob:0.5的值即可,1代表不進(jìn)行dropout。
代碼
import tensorflow as tf
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
digits = load_digits()
X = digits.data
y = digits.target
y = LabelBinarizer().fit_transform(y)
X_train,X_test,Y_train,Y_test = train_test_split(X,y,test_size = 500)
def add_layer(inputs,in_size,out_size,n_layer,activation_function = None,keep_prob = 1):
layer_name = 'layer%s'%n_layer
with tf.name_scope(layer_name):
with tf.name_scope("Weights"):
Weights = tf.Variable(tf.random_normal([in_size,out_size]),name = "Weights")
tf.summary.histogram(layer_name+"/weights",Weights)
with tf.name_scope("biases"):
biases = tf.Variable(tf.zeros([1,out_size]) + 0.1,name = "biases")
tf.summary.histogram(layer_name+"/biases",biases)
with tf.name_scope("Wx_plus_b"):
Wx_plus_b = tf.matmul(inputs,Weights) + biases
Wx_plus_b = tf.nn.dropout(Wx_plus_b,keep_prob)
tf.summary.histogram(layer_name+"/Wx_plus_b",Wx_plus_b)
if activation_function == None :
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
tf.summary.histogram(layer_name+"/outputs",outputs)
return outputs
def compute_accuracy(x_data,y_data,prob = 1):
global prediction
y_pre = sess.run(prediction,feed_dict = {xs:x_data,keep_prob:prob})
correct_prediction = tf.equal(tf.arg_max(y_data,1),tf.arg_max(y_pre,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
result = sess.run(accuracy,feed_dict = {xs:x_data,ys:y_data,keep_prob:prob})
return result
keep_prob = tf.placeholder(tf.float32)
xs = tf.placeholder(tf.float32,[None,64])
ys = tf.placeholder(tf.float32,[None,10])
l1 = add_layer(xs,64,100,'l1',activation_function=tf.nn.tanh, keep_prob = keep_prob)
l2 = add_layer(l1,100,100,'l2',activation_function=tf.nn.tanh, keep_prob = keep_prob)
prediction = add_layer(l1,100,10,'l3',activation_function = tf.nn.softmax, keep_prob = 1)
with tf.name_scope("loss"):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=ys,logits = prediction),name = 'loss')
tf.summary.scalar("loss",loss)
train = tf.train.AdamOptimizer(0.01).minimize(loss)
init = tf.initialize_all_variables()
merged = tf.summary.merge_all()
with tf.Session() as sess:
sess.run(init)
train_writer = tf.summary.FileWriter("logs/strain",sess.graph)
test_writer = tf.summary.FileWriter("logs/test",sess.graph)
for i in range(5001):
sess.run(train,feed_dict = {xs:X_train,ys:Y_train,keep_prob:0.5})
if i % 500 == 0:
print("訓(xùn)練%d次的識(shí)別率為:%f。"%((i+1),compute_accuracy(X_test,Y_test,prob=0.5)))
train_result = sess.run(merged,feed_dict={xs:X_train,ys:Y_train,keep_prob:0.5})
test_result = sess.run(merged,feed_dict={xs:X_test,ys:Y_test,keep_prob:0.5})
train_writer.add_summary(train_result,i)
test_writer.add_summary(test_result,i)
keep_prob = 0.5
訓(xùn)練結(jié)果為:
訓(xùn)練1次的識(shí)別率為:0.086000。
訓(xùn)練501次的識(shí)別率為:0.890000。
訓(xùn)練1001次的識(shí)別率為:0.938000。
訓(xùn)練1501次的識(shí)別率為:0.952000。
訓(xùn)練2001次的識(shí)別率為:0.952000。
訓(xùn)練2501次的識(shí)別率為:0.946000。
訓(xùn)練3001次的識(shí)別率為:0.940000。
訓(xùn)練3501次的識(shí)別率為:0.932000。
訓(xùn)練4001次的識(shí)別率為:0.970000。
訓(xùn)練4501次的識(shí)別率為:0.952000。
訓(xùn)練5001次的識(shí)別率為:0.950000。
這是keep_prob = 0.5時(shí)tensorboard中的loss的圖像:

keep_prob = 1
訓(xùn)練結(jié)果為:
訓(xùn)練1次的識(shí)別率為:0.160000。
訓(xùn)練501次的識(shí)別率為:0.754000。
訓(xùn)練1001次的識(shí)別率為:0.846000。
訓(xùn)練1501次的識(shí)別率為:0.854000。
訓(xùn)練2001次的識(shí)別率為:0.852000。
訓(xùn)練2501次的識(shí)別率為:0.852000。
訓(xùn)練3001次的識(shí)別率為:0.860000。
訓(xùn)練3501次的識(shí)別率為:0.854000。
訓(xùn)練4001次的識(shí)別率為:0.856000。
訓(xùn)練4501次的識(shí)別率為:0.852000。
訓(xùn)練5001次的識(shí)別率為:0.852000。
這是keep_prob = 1時(shí)tensorboard中的loss的圖像:

可以明顯看出來keep_prob = 0.5的訓(xùn)練集和測試集的曲線更加貼近。
以上就是python人工智能tensorflow函數(shù)tf.nn.dropout使用示例的詳細(xì)內(nèi)容,更多關(guān)于tensorflow函數(shù)tf.nn.dropout的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Tornado Web Server框架編寫簡易Python服務(wù)器
這篇文章主要為大家詳細(xì)介紹了Tornado Web Server框架編寫簡易Python服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
使用python寫的opencv實(shí)時(shí)監(jiān)測和解析二維碼和條形碼
這篇文章主要介紹了使用python寫的opencv實(shí)時(shí)監(jiān)測和解析二維碼和條形碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Python collections.deque雙邊隊(duì)列原理詳解
這篇文章主要介紹了Python collections.deque雙邊隊(duì)列原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
利用Celery實(shí)現(xiàn)Django博客PV統(tǒng)計(jì)功能詳解
給網(wǎng)站增加pv、uv統(tǒng)計(jì),可以是件很簡單的事,也可以是件很復(fù)雜的事。下面這篇文章主要給大家介紹了利用Celery實(shí)現(xiàn)Django博客PV統(tǒng)計(jì)功能的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05

