當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.nn.experimental.stateless_dropout用法及代碼示例

計算 dropout:隨機將元素設置為零以防止過度擬合。

用法

tf.nn.experimental.stateless_dropout(
    x, rate, seed, rng_alg=None, noise_shape=None, name=None
)

參數

  • x 浮點張量。
  • rate 與 x 具有相同類型的標量 Tensor。每個元素被丟棄的概率。例如,設置 rate=0.1 將丟棄 10% 的輸入元素。
  • seed 形狀為 [2] 的整數張量。隨機數的種子。
  • rng_alg 用於生成隨機數的算法(默認為 "auto_select" )。有關支持的值,請參閱tf.random.stateless_uniformalg 參數。
  • noise_shape 一維整數 Tensor ,表示隨機生成的保持/丟棄標誌的形狀。
  • name 此操作的名稱。

返回

  • x 具有相同形狀和 dtype 的張量。

拋出

  • ValueError 如果 rate 不在 [0, 1) 中,或者如果 x 不是浮點張量。 rate=1 是不允許的,因為輸出將全為零,這可能不是預期的。

Dropout 對於正則化 DNN 模型很有用。輸入元素被隨機設置為零(並且其他元素被重新縮放)。這鼓勵每個節點獨立有用,因為它不能依賴其他節點的輸出。

更準確地說:xrate 元素的概率設置為 0 。其餘元素按 1.0 / (1 - rate) 放大,以便保留預期值。

x = tf.ones([3,5])
tf.nn.experimental.stateless_dropout(x, rate=0.5, seed=[1, 0])
<tf.Tensor:shape=(3, 5), dtype=float32, numpy=
array([[2., 0., 2., 0., 0.],
       [0., 0., 2., 0., 2.],
       [0., 0., 0., 0., 2.]], dtype=float32)>
x = tf.ones([3,5])
tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[1, 0])
<tf.Tensor:shape=(3, 5), dtype=float32, numpy=
array([[5., 0., 0., 0., 0.],
       [0., 0., 0., 0., 5.],
       [0., 0., 0., 0., 5.]], dtype=float32)>
tf.nn.experimental.stateless_dropout(x, rate=0.0, seed=[1, 0]) == x
<tf.Tensor:shape=(3, 5), dtype=bool, numpy=
array([[ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True]])>

該函數是tf.nn.dropout的無狀態版本,從某種意義上說,無論調用多少次,相同的seed會導致相同的結果,不同的seed會導致不同的結果。

x = tf.ones([3,5])
tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[1, 0])
<tf.Tensor:shape=(3, 5), dtype=float32, numpy=
array([[5., 0., 0., 0., 0.],
       [0., 0., 0., 0., 5.],
       [0., 0., 0., 0., 5.]], dtype=float32)>
tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[1, 0])
<tf.Tensor:shape=(3, 5), dtype=float32, numpy=
array([[5., 0., 0., 0., 0.],
       [0., 0., 0., 0., 5.],
       [0., 0., 0., 0., 5.]], dtype=float32)>
tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[2, 0])
<tf.Tensor:shape=(3, 5), dtype=float32, numpy=
array([[5., 0., 0., 0., 0.],
       [0., 0., 0., 5., 0.],
       [0., 0., 0., 0., 0.]], dtype=float32)>
tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[2, 0])
<tf.Tensor:shape=(3, 5), dtype=float32, numpy=
array([[5., 0., 0., 0., 0.],
       [0., 0., 0., 5., 0.],
       [0., 0., 0., 0., 0.]], dtype=float32)>

將上述結果與以下tf.nn.dropout 的結果進行比較。第二次使用相同的種子調用tf.nn.dropout,它將給出不同的輸出。

tf.random.set_seed(0)
x = tf.ones([3,5])
tf.nn.dropout(x, rate=0.8, seed=1)
<tf.Tensor:shape=(3, 5), dtype=float32, numpy=
array([[0., 0., 0., 5., 5.],
       [0., 5., 0., 5., 0.],
       [5., 0., 5., 0., 5.]], dtype=float32)>
tf.nn.dropout(x, rate=0.8, seed=1)
<tf.Tensor:shape=(3, 5), dtype=float32, numpy=
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 5., 0.],
       [0., 0., 0., 0., 0.]], dtype=float32)>
tf.nn.dropout(x, rate=0.8, seed=2)
<tf.Tensor:shape=(3, 5), dtype=float32, numpy=
array([[0., 0., 0., 0., 0.],
       [0., 5., 0., 5., 0.],
       [0., 0., 0., 0., 0.]], dtype=float32)>
tf.nn.dropout(x, rate=0.8, seed=2)
<tf.Tensor:shape=(3, 5), dtype=float32, numpy=
array([[0., 0., 0., 0., 0.],
       [5., 0., 5., 0., 5.],
       [0., 5., 0., 0., 5.]], dtype=float32)>

此函數和 tf.nn.dropout 之間的區別類似於 tf.random.stateless_uniformtf.random.uniform 之間的區別。有關 TF 中各種 RNG 係統的詳細說明,請參閱隨機數生成指南。正如指南所述,像 tf.random.uniform 和 tf.nn.dropout 這樣的傳統有狀態 RNG 操作尚未被棄用,但非常不鼓勵,因為它們的狀態難以控製。

默認情況下,每個元素都是獨立保留或刪除的。如果指定了noise_shape,則必須廣播到x的形狀,並且隻有具有noise_shape[i] == shape(x)[i]的維度才會做出獨立的決定。這對於從圖像或序列中刪除整個通道很有用。例如:

x = tf.ones([3,10])
tf.nn.experimental.stateless_dropout(x, rate=2/3, noise_shape=[1,10],
                                     seed=[1, 0])
<tf.Tensor:shape=(3, 10), dtype=float32, numpy=
array([[3., 0., 0., 0., 0., 0., 0., 3., 0., 3.],
       [3., 0., 0., 0., 0., 0., 0., 3., 0., 3.],
       [3., 0., 0., 0., 0., 0., 0., 3., 0., 3.]], dtype=float32)>

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.nn.experimental.stateless_dropout。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。