tf.nn.dropout(x,keep_prob,noise_shape=None,seed=None,name=None)
参数:
x:一个浮点型Tensor.
keep_prob:一个标量Tensor,它与x具有相同类型.保留每个元素的概率.
noise_shape:类型为int32的1维Tensor,表示随机产生的保持/丢弃标志的形状.
seed:一个Python整数.用于创建随机种子.
name:此操作的名称(可选).
返回:
该函数返回与x具有相同形状的Tensor.
该函数使x的一部分(概率大约为keep_prob)变为0,其余变为x/keep_prob,
noise_shape可以使得矩阵x一部分行全为0或者部分列全为0
sample
with tf.Session() as sess:
d = tf.to_float(tf.reshape(tf.range(1,17),[4,4]))
sess.run(tf.global_variables_initializer())
print(sess.run(tf.shape(d)))
print(sess.run(d[0]))
# 矩阵有一半左右的元素变为element/0.5,其余为0
dropout_a44 = tf.nn.dropout(d, 0.5, noise_shape = None)
result_dropout_a44 = sess.run(dropout_a44)
print(result_dropout_a44)
# 行大小相同4,行同为0,或同不为0
dropout_a41 = tf.nn.dropout(d, 0.5, noise_shape = [4,1])
result_dropout_a41 = sess.run(dropout_a41)
print(result_dropout_a41)
# 列大小相同4,列同为0,或同不为0
dropout_a24 = tf.nn.dropout(d, 0.5, noise_shape = [1,4])
result_dropout_a24 = sess.run(dropout_a24)
print(result_dropout_a24)
#不相等的noise_shape只能为1
output
[[ 0. 4. 0. 8.]
[10. 12. 14. 0.]
[ 0. 20. 22. 0.]
[26. 28. 30. 32.]]
[[ 2. 4. 6. 8.]
[10. 12. 14. 16.]
[18. 20. 22. 24.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 6. 0.]
[ 0. 0. 14. 0.]
[ 0. 0. 22. 0.]
[ 0. 0. 30. 0.]]