Dropout
類別tf_keras.layers.Dropout(rate, noise_shape=None, seed=None, **kwargs)
對輸入套用 Dropout。
Dropout 層在訓練期間,會以 rate
的頻率隨機將輸入單元設定為 0,這有助於防止過擬合。未設定為 0 的輸入會放大 1/(1 - rate) 倍,使得所有輸入的總和保持不變。
請注意,Dropout 層僅在 training
設定為 True 時才會套用,以便在推論期間不丟棄任何值。使用 model.fit
時,training
會自動正確設定為 True,在其他情況下,您可以在呼叫層時將 kwarg 明確設定為 True。
(這與將 Dropout 層設定為 trainable=False
是相反的。trainable
不會影響層的行為,因為 Dropout 沒有任何可在訓練期間凍結的變數/權重。)
>>> tf.random.set_seed(0)
>>> layer = tf.keras.layers.Dropout(.2, input_shape=(2,))
>>> data = np.arange(10).reshape(5, 2).astype(np.float32)
>>> print(data)
[[0. 1.]
[2. 3.]
[4. 5.]
[6. 7.]
[8. 9.]]
>>> outputs = layer(data, training=True)
>>> print(outputs)
tf.Tensor(
[[ 0. 1.25]
[ 2.5 3.75]
[ 5. 6.25]
[ 7.5 8.75]
[10. 0. ]], shape=(5, 2), dtype=float32)
參數
(batch_size, timesteps, features)
,並且您希望所有時間步長的 Dropout 遮罩都相同,則可以使用 noise_shape=(batch_size, 1, features)
。呼叫參數