網(wǎng)站首頁 編程語言 正文
前言
神經(jīng)網(wǎng)絡(luò)在設(shè)置的神經(jīng)網(wǎng)絡(luò)足夠復(fù)雜的情況下,可以無限逼近一段非線性連續(xù)函數(shù),但是如果神經(jīng)網(wǎng)絡(luò)設(shè)置的足夠復(fù)雜,將會導(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í)可能會使用到它。
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ù)的話,就會導(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次的識別率為:%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次的識別率為:0.086000。
訓(xùn)練501次的識別率為:0.890000。
訓(xùn)練1001次的識別率為:0.938000。
訓(xùn)練1501次的識別率為:0.952000。
訓(xùn)練2001次的識別率為:0.952000。
訓(xùn)練2501次的識別率為:0.946000。
訓(xùn)練3001次的識別率為:0.940000。
訓(xùn)練3501次的識別率為:0.932000。
訓(xùn)練4001次的識別率為:0.970000。
訓(xùn)練4501次的識別率為:0.952000。
訓(xùn)練5001次的識別率為:0.950000。
這是keep_prob = 0.5時(shí)tensorboard中的loss的圖像:
keep_prob = 1
訓(xùn)練結(jié)果為:
訓(xùn)練1次的識別率為:0.160000。
訓(xùn)練501次的識別率為:0.754000。
訓(xùn)練1001次的識別率為:0.846000。
訓(xùn)練1501次的識別率為:0.854000。
訓(xùn)練2001次的識別率為:0.852000。
訓(xùn)練2501次的識別率為:0.852000。
訓(xùn)練3001次的識別率為:0.860000。
訓(xùn)練3501次的識別率為:0.854000。
訓(xùn)練4001次的識別率為:0.856000。
訓(xùn)練4501次的識別率為:0.852000。
訓(xùn)練5001次的識別率為:0.852000。
這是keep_prob = 1時(shí)tensorboard中的loss的圖像:
可以明顯看出來keep_prob = 0.5的訓(xùn)練集和測試集的曲線更加貼近。
原文鏈接:https://blog.csdn.net/weixin_44791964/article/details/96972541
相關(guān)推薦
- 2022-08-23 Django配合python進(jìn)行requests請求的問題及解決方法_python
- 2022-12-10 Qt顯示QImage圖像在label上,并保持自適應(yīng)大小問題_C 語言
- 2022-03-08 android?studio組件通信:Intend啟動Activity接收返回結(jié)果_Android
- 2022-03-03 解決composer提示版本太舊Warning from https://mirrors.aliyu
- 2022-07-12 用戶手抖,連續(xù)點(diǎn)了兩次?優(yōu)雅解決表單重復(fù)提交
- 2022-08-03 docker安裝redis掛載容器卷同時(shí)開啟持久化_docker
- 2022-07-08 C#四種計(jì)時(shí)器Timer的區(qū)別和用法_C#教程
- 2023-07-25 MyBatis數(shù)據(jù)操作和動態(tài)SQL
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支