dropout(x, keep_prob, noise_shape=None, seed=None, name=None)
- 函数作用就是使得矩阵x的一部分(概率大约为keep_prob)变为0,其余变为element/keep_prob,
- noise_shape可以使得矩阵x一部分行全为0或者部分列全为0
- 用在tensorflow中使得部分神经元随机为0不参与训练,如果算法过拟合了,可以试试这个办法。
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
网友评论