亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python人工智能tensorflow函數(shù)tf.nn.dropout使用方法

 更新時(shí)間:2022年05月05日 11:15:52   作者:Bubbliiiing  
這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.nn.dropout使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

神經(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)行測(cè)試的時(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)練集和測(cè)試集的曲線更加貼近。

以上就是python人工智能tensorflow函數(shù)tf.nn.dropout使用示例的詳細(xì)內(nèi)容,更多關(guān)于tensorflow函數(shù)tf.nn.dropout的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 用Python編寫一個(gè)簡單的CS架構(gòu)后門的方法

    用Python編寫一個(gè)簡單的CS架構(gòu)后門的方法

    今天小編就為大家分享一篇關(guān)于用Python編寫一個(gè)簡單的CS架構(gòu)后門的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-11-11
  • Python?類方法和靜態(tài)方法之間的區(qū)別

    Python?類方法和靜態(tài)方法之間的區(qū)別

    這篇文章主要介紹了Python?類方法和靜態(tài)方法之間的區(qū)別,靜態(tài)方法并不是真正意義上的類方法,它只是一個(gè)被放到類里的函數(shù)而已,更多內(nèi)容需要的朋友可以參考一下
    2022-07-07
  • Python Socket編程之多線程聊天室

    Python Socket編程之多線程聊天室

    這篇文章主要為大家詳細(xì)介紹了Python Socket編程之多線程聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • python實(shí)現(xiàn)的登錄與提交表單數(shù)據(jù)功能示例

    python實(shí)現(xiàn)的登錄與提交表單數(shù)據(jù)功能示例

    這篇文章主要介紹了python實(shí)現(xiàn)的登錄與提交表單數(shù)據(jù)功能,結(jié)合實(shí)例形式分析了Python表單登錄相關(guān)的請(qǐng)求與響應(yīng)操作實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-09-09
  • python買賣股票的最佳時(shí)機(jī)(基于貪心/蠻力算法)

    python買賣股票的最佳時(shí)機(jī)(基于貪心/蠻力算法)

    這篇文章主要介紹了python買賣股票的最佳時(shí)機(jī)(基于貪心/蠻力算法),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python?subprocess.Popen?實(shí)時(shí)輸出?stdout的解決方法(正確管道寫法)

    Python?subprocess.Popen?實(shí)時(shí)輸出?stdout的解決方法(正確管道寫法)

    這篇文章主要介紹了Python?subprocess.Popen實(shí)時(shí)輸出stdout正確管道寫法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • python的exec、eval使用分析

    python的exec、eval使用分析

    這篇文章主要介紹了python的exec、eval使用分析,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Python中模塊(Module)和包(Package)的區(qū)別詳解

    Python中模塊(Module)和包(Package)的區(qū)別詳解

    這篇文章主要介紹了Python中模塊(Module)和包(Package)的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • pygame.display.flip()和pygame.display.update()的區(qū)別及說明

    pygame.display.flip()和pygame.display.update()的區(qū)別及說明

    這篇文章主要介紹了pygame.display.flip()和pygame.display.update()的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Python繪制頻率分布直方圖的示例

    Python繪制頻率分布直方圖的示例

    今天小編就為大家分享一篇Python繪制頻率分布直方圖的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07

最新評(píng)論