tf.nn.dropout和tf.keras.layers.Dropout的区别(TensorFlow2.3)与实验
生活随笔
收集整理的這篇文章主要介紹了
tf.nn.dropout和tf.keras.layers.Dropout的区别(TensorFlow2.3)与实验
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這里寫目錄標(biāo)題
- 場(chǎng)景:dropout和Dropout區(qū)別
- 問題描述:
- 結(jié)論:
- 深層次原因:dropout是底層API,Dropout是高層API
場(chǎng)景:dropout和Dropout區(qū)別
全網(wǎng)搜索tf.nn.dropout和tf.keras.layers.Dropout區(qū)別,發(fā)現(xiàn)好多都是錯(cuò)誤的講解,因此有必要進(jìn)行一次實(shí)驗(yàn)和糾錯(cuò)。
tf.nn.dropout和tf.keras.layers.Dropout的區(qū)別,看本文就足夠了。
問題描述:
tf.nn.dropout和tf.keras.layers.Dropout的區(qū)別。
先實(shí)驗(yàn)一下,再出結(jié)論,代碼如下:
import tensorflow as tfx = tf.ones((10,10)) print("the orginal x is x_numpy() : ") print(x.numpy()) x_after_nn_dropout = tf.nn.dropout(x=x,rate=0.2) # 每個(gè)元素以rate的概率丟棄,隨機(jī)丟棄一些特征,在訓(xùn)練和預(yù)測(cè)時(shí)都生效 print("x_after_nn_dropout.numpy()") print(x_after_nn_dropout.numpy())x_after_nn_dropout2 = tf.nn.dropout(x=x,rate=0.9999) # 每個(gè)元素以rate的概率丟棄,隨機(jī)丟棄一些特征,在訓(xùn)練和預(yù)測(cè)時(shí)都生效 print("x_after_nn_dropout2.numpy()") print(x_after_nn_dropout2.numpy())print("tf.nn.dropout(x, rate = 0.0) == x : ") print(tf.nn.dropout(x, rate = 0.0) == x) print("tf.nn.dropout(x, rate = 1) == x : ") print(tf.nn.dropout(x, rate = 0.999) == x)x_after_dropout1 = tf.keras.layers.Dropout(0.2)(x) print("x_after_dropout1.numpy(): ") print(x_after_dropout1.numpy()) #tf.keras.layers.Dropout只會(huì)在訓(xùn)練fit時(shí)才會(huì)起作用,隨機(jī)設(shè)定輸入的值x=0,這個(gè)概率為輸入的百分?jǐn)?shù)#在模型預(yù)測(cè)(predict)時(shí),不生效,所有神經(jīng)元均保留也就是不進(jìn)行dropout。所以我們常用的應(yīng)該是這個(gè)。運(yùn)行結(jié)果如下:
the orginal x is x_numpy() : [[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]] x_after_nn_dropout.numpy() [[1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25][1.25 1.25 0. 1.25 1.25 1.25 1.25 1.25 0. 1.25][1.25 1.25 0. 1.25 1.25 1.25 1.25 1.25 1.25 1.25][1.25 1.25 1.25 1.25 0. 1.25 0. 0. 1.25 1.25][0. 0. 1.25 1.25 1.25 1.25 1.25 1.25 1.25 0. ][1.25 0. 1.25 1.25 1.25 1.25 1.25 0. 1.25 1.25][1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 0. 1.25][1.25 1.25 1.25 0. 0. 1.25 1.25 1.25 0. 1.25][1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 0. 0. ][1.25 1.25 1.25 1.25 0. 1.25 1.25 1.25 1.25 1.25]] x_after_nn_dropout2.numpy() [[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]] tf.nn.dropout(x, rate = 0.0) == x : tf.Tensor( [[ True True True True True True True True True True][ True True True True True True True True True True][ True True True True True True True True True True][ True True True True True True True True True True][ True True True True True True True True True True][ True True True True True True True True True True][ True True True True True True True True True True][ True True True True True True True True True True][ True True True True True True True True True True][ True True True True True True True True True True]], shape=(10, 10), dtype=bool) tf.nn.dropout(x, rate = 1) == x : tf.Tensor( [[False False False False False False False False False False][False False False False False False False False False False][False False False False False False False False False False][False False False False False False False False False False][False False False False False False False False False False][False False False False False False False False False False][False False False False False False False False False False][False False False False False False False False False False][False False False False False False False False False False][False False False False False False False False False False]], shape=(10, 10), dtype=bool)x_after_dropout1.numpy(): [[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.][1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]結(jié)論:
綜合實(shí)驗(yàn)結(jié)果,得到如下結(jié)論。
- 1.tf.nn.dropout(x=x,rate=0.2) # 每個(gè)元素以rate的概率丟棄,也就是變成了0,在訓(xùn)練和預(yù)測(cè)時(shí)都生效
- 2.tf.nn.dropout(x=x,rate=0.9999) # rate增大時(shí),基本上全部元素都只為了0
- 3.tf.keras.layers.Dropout(0.2)(x) #只會(huì)在訓(xùn)練fit時(shí)才會(huì)起作用,隨機(jī)設(shè)定輸入的值x的某一維=0,這個(gè)概率為輸入的百分?jǐn)?shù) ,訓(xùn)練fit才有效。在模型預(yù)測(cè)(predict)時(shí),不生效,所有神經(jīng)元均保留也就是不進(jìn)行dropout。
所以我們常用的應(yīng)該是這個(gè)tf.keras.layers.Dropout,里面數(shù)值就是我們需要丟棄掉神經(jīng)元的比例,0.2就表示隨機(jī)丟掉20%的神經(jīng)元不激活。
深層次原因:dropout是底層API,Dropout是高層API
產(chǎn)生這種結(jié)果的原因
- 1.tf.nn.dropout是底層API,功能很簡(jiǎn)單,沒有其他額外的附加功能。因此,dropout就真的是在dropout,一個(gè)單一的函數(shù)。
- 2.tf.keras.layers.Dropout是個(gè)高層API,功能完善,他會(huì)區(qū)分當(dāng)前狀態(tài)是fit還是predict,如果是fit,他會(huì)把is_training = True,dropout生效;當(dāng)遇到是predict、evalue時(shí),is_training = False,dropout不生效。
也正是因?yàn)閠f.keras.layers.Dropout幫我們實(shí)現(xiàn)了這一個(gè)功能,不然,自己要去實(shí)現(xiàn)這套東西,還挺復(fù)雜。
總結(jié)
以上是生活随笔為你收集整理的tf.nn.dropout和tf.keras.layers.Dropout的区别(TensorFlow2.3)与实验的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tensorflow2 神经网络模型构建
- 下一篇: tensorboard : 无法将“te