计算dropout:将元素随机设置为零以防止过拟合。
dropout就是在不同的训练过程中随机扔掉一部分神经元。也就是让某个神经元的激活值以一定的概率p,让其停止工作,这次训练过程中不更新权值,也不参加神经网络的计算。但是它的权重得保留下来(只是暂时不更新而已),因为下次样本输入时它可能又得工作了。示意图如下:
但在测试及验证中:每个神经元都要参加运算,但其输出要乘以概率p。
tf.nn.dropout(
x, rate, noise_shape=None, seed=None, name=None
)
另请参阅:tf.keras.layers.Dropout
有关Dropout
层的信息。
Dropout
对于正则化DNN模型很有用。输入元素被随机设置为零(其他元素被重新缩放)。这鼓励每个节点独立使用,因为它不能依赖其他节点的输出。
更精确地:x的
元素有rate
概率设置为0
。其余元素按放大1.0 / (1 - rate)
,以便保留期望值。
import tensorflow as tf
tf.set_random_seed(0)
x = tf.ones([3, 5])
y = tf.nn.dropout(x, rate=0.5, seed=1)
print(x)
print(y)
# 上面两条语句输出结果:
# Tensor("ones:0", shape=(3, 5), dtype=float32)
# Tensor("dropout/mul:0", shape=(3, 5), dtype=float32)
with tf.Session() as sess:
print(x)
print(y)
# 上面两条语句输出结果:
# Tensor("ones:0", shape=(3, 5), dtype=float32)
# Tensor("dropout/mul:0", shape=(3, 5), dtype=float32)
print(sess.run(x))
# 上面一条语句输出结果:
# [[1. 1. 1. 1. 1.]
# [1. 1. 1. 1. 1.]
# [1. 1. 1. 1. 1.]]
print(sess.run(y))
# 上面一条语句输出结果:重置的值为1.0 /(1.0-0.5)= 2
# [[2. 0. 0. 2. 2.]
# [2. 2. 2. 2. 2.]
# [2. 0. 2. 0. 2.]]
注意:在tf1.0中的tf.set_random_seed(0)等价于tf2.0中的tf.random.set_seed(0)
import tensorflow as tf
tf.set_random_seed(0)
x = tf.ones([3, 5])
# seed指的是按照不同的随机空间进行置0,
# 因此当rate一定时,若seed相同,则无论执行多少次,结果都是相同的,
# 但是若seed不同,则随机置0方式不同
# y = tf.nn.dropout(x, rate=0.5)
# 仅使用上面指定的随机空间tf.set_random_seed(0)
# [[2. 2. 2. 0. 2.]
# [2. 2. 0. 2. 2.]
# [0. 0. 0. 2. 2.]]
y = tf.nn.dropout(x, rate=0.5, seed=0)
# 使用随机空间seed=0
# [[0. 0. 0. 2. 0.]
# [2. 0. 0. 2. 2.]
# [2. 0. 0. 0. 0.]]
# y = tf.nn.dropout(x, rate=0.5, seed=1)
# 使用随机空间seed=1
# [[2. 0. 0. 2. 2.]
# [2. 2. 2. 2. 2.]
# [2. 0. 2. 0. 2.]]
# y = tf.nn.dropout(x, rate=0.5, seed=2)
# 使用随机空间seed=2
# [[2. 0. 0. 0. 0.]
# [0. 2. 2. 2. 2.]
# [0. 0. 0. 0. 2.]]
y = tf.nn.dropout(x, rate=0.5, seed=0)
# 不设置tf.set_random_seed(0),但是设置seed,
# 可以发现,这与设置tf.set_random_seed(0)的结果不同,
# 说明这是两个不同的随机空间,尽管他们设置都是0
# [[0. 2. 2. 0. 0.]
# [2. 2. 0. 2. 2.]
# [2. 0. 0. 2. 2.]]
y = tf.nn.dropout(x, rate=0.5)
# 既不指定tf.set_random_seed(0),又不指定seed,则每次随机挑选随机场,
# 就是说每次的执行结果都不一样
# 运行一次
# [[2. 0. 2. 2. 0.]
# [2. 0. 2. 0. 0.]
# [0. 2. 2. 2. 2.]]
# 运行两次
# [[0. 2. 2. 2. 2.]
# [0. 0. 2. 2. 0.]
# [2. 2. 2. 2. 0.]]
print(x)
print(y)
with tf.Session() as sess:
print(x)
print(y)
print(sess.run(x))
print(sess.run(y))
import tensorflow as tf
tf.set_random_seed(0)
x = tf.ones([3, 5])
y = tf.nn.dropout(x, rate=0.8, seed=1)
print(x)
print(y)
# 上面两条语句输出结果:
# Tensor("ones:0", shape=(3, 5), dtype=float32)
# Tensor("dropout/mul:0", shape=(3, 5), dtype=float32)
with tf.Session() as sess:
print(x)
print(y)
# 上面两条语句输出结果:
# Tensor("ones:0", shape=(3, 5), dtype=float32)
# Tensor("dropout/mul:0", shape=(3, 5), dtype=float32)
print(sess.run(x))
# 上面一条语句输出结果:
# [[1. 1. 1. 1. 1.]
# [1. 1. 1. 1. 1.]
# [1. 1. 1. 1. 1.]]
print(sess.run(y))
# 上面一条语句输出结果:重置的值为1.0 /(1.0-0.8)= 5
# [[0. 0. 0. 5.0000005 5.0000005]
# [0. 5.0000005 0. 5.0000005 0. ]
# [5.0000005 0. 5.0000005 0. 5.0000005]]
# tf.nn.dropout(x, rate = 0.0) == x
# 即一个也不置0
import tensorflow as tf
tf.set_random_seed(0)
x = tf.ones([3, 5])
y = tf.nn.dropout(x, rate=0, seed=1)
print(x)
print(y)
# 上面两条语句输出结果:
# Tensor("ones:0", shape=(3, 5), dtype=float32)
# Tensor("ones:0", shape=(3, 5), dtype=float32)
with tf.Session() as sess:
print(x)
print(y)
# 上面两条语句输出结果:
# Tensor("ones:0", shape=(3, 5), dtype=float32)
# Tensor("ones:0", shape=(3, 5), dtype=float32)
print(sess.run(x))
# 上面一条语句输出结果:
# [[1. 1. 1. 1. 1.]
# [1. 1. 1. 1. 1.]
# [1. 1. 1. 1. 1.]]
print(sess.run(y))
# 上面一条语句输出结果:
# [[1. 1. 1. 1. 1.]
# [1. 1. 1. 1. 1.]
# [1. 1. 1. 1. 1.]]
默认情况下,每个元素都是独立保留或dropped。如果noise_shape
指定,则必须将其 广播 为x
的形状,只有noise_shape[i] == shape(x)[i]
的维度才能做出独立的决定。这对于从图像或序列中dropping整个通道很有用。例如:
import tensorflow as tf
# tf.set_random_seed(0)
x = tf.ones([3, 10])
# 不设置tf.set_random_seed(0),仅设置seed=2
# [[3.0000002 3.0000002 0. 0. 0. 0. 0. 0. 0. 3.0000002]
# [3.0000002 3.0000002 0. 0. 0. 0. 0. 0. 0. 3.0000002]
# [3.0000002 3.0000002 0. 0. 0. 0. 0. 0. 0. 3.0000002]]
# y = tf.nn.dropout(x, rate=2 / 3, noise_shape=[1, 10])
# 仅设置tf.set_random_seed(0),不设置seed
# [[3.0000002 3.0000002 3.0000002 0. 3.0000002 3.0000002 0. 0. 3.0000002 0. ]
# [3.0000002 3.0000002 3.0000002 0. 3.0000002 3.0000002 0. 0. 3.0000002 0. ]
# [3.0000002 3.0000002 3.0000002 0. 3.0000002 3.0000002 0. 0. 3.0000002 0. ]]
y = tf.nn.dropout(x, rate=2 / 3, noise_shape=[1, 10])
# 既不设置tf.set_random_seed(0),也不设置seed
# 第一次运行
# [[3.0000002 0. 0. 0. 3.0000002 3.0000002 0. 0. 3.0000002 0. ]
# [3.0000002 0. 0. 0. 3.0000002 3.0000002 0. 0. 3.0000002 0. ]
# [3.0000002 0. 0. 0. 3.0000002 3.0000002 0. 0. 3.0000002 0. ]]
# 第二次运行
# [[0. 0. 3.0000002 0. 0. 0. 0. 0. 0. 0. ]
# [0. 0. 3.0000002 0. 0. 0. 0. 0. 0. 0. ]
# [0. 0. 3.0000002 0. 0. 0. 0. 0. 0. 0. ]]
# 这里seed指的是在noise_shape中指定随机空间,且x的每个维度按照noise_shape来,复制到x的形状
# y = tf.nn.dropout(x, rate=2 / 3, noise_shape=[1, 10], seed=2)
# [[3.0000002 0. 0. 0. 0. 0. 3.0000002 3.0000002 3.0000002 0. ]
# [3.0000002 0. 0. 0. 0. 0. 3.0000002 3.0000002 3.0000002 0. ]
# [3.0000002 0. 0. 0. 0. 0. 3.0000002 3.0000002 3.0000002 0. ]]
# y = tf.nn.dropout(x, rate=2 / 3, noise_shape=[1, 10], seed=1)
# [[0. 0. 0. 3.0000002 3.0000002 0. 3.0000002 3.0000002 3.0000002 0. ]
# [0. 0. 0. 3.0000002 3.0000002 0. 3.0000002 3.0000002 3.0000002 0. ]
# [0. 0. 0. 3.0000002 3.0000002 0. 3.0000002 3.0000002 3.0000002 0. ]]
print(x)
print(y)
with tf.Session() as sess:
print(x)
print(y)
print(sess.run(x))
print(sess.run(y))
import tensorflow as tf
tf.set_random_seed(0)
x = tf.ones([5, 15])
# 按列设置随机场,每一列为一个场,所有列都相同
y = tf.nn.dropout(x, rate=2 / 3, noise_shape=[5, 1])
# [[3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002]
# [3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002]
# [3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
# [3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002 3.0000002]]
print(x)
print(y)
with tf.Session() as sess:
print(x)
print(y)
print(sess.run(x))
print(sess.run(y))
Args |
|
---|---|
x |
一个浮点型张量。 |
rate |
与x具有相同类型的标量Tensor 。每个元素被dropout的概率。例如,设置rate = 0.1将drop输入元素的10%。这里只是按照概率算,可能会减少10%,不是一定会减少10%。 |
noise_shape |
int32 类型的1-D Tensor ,表示随机生成的保留/删除(keep/drop)标志的形状。 |
seed |
一个Python整型。用于创建随机种子。请参阅 tf.random.set_seed 行为。 |
name |
此操作的名称(可选)。 |
Returns |
---|
具有与x 相同形状的张量。 |
Raises |
|
---|---|
ValueError |
如果rate 不在[0, 1] 或如果x 不是浮点型张量。禁止rate=1 ,是因为输出将会全为零,这可能不是预期的结果。 |