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