参考《Tensorflow技术解析与实战》
- 当输入数据特征相差明显时,tanh效果好,在nlp上用处广泛。
- 当特征不明显时,sigmoid效果比较好。
- 使用sigmoid和tanh时,输入需要进行规范化,否则激活后的值全部进入平坦区,隐层输出趋于相同,丧失特征表达。
- relu有时可以不需要,目前大多数选择relu
import tensorflow as tf
a = tf.constant([[1.,2.],[5.,-2.]])
relu_a = tf.nn.relu(a)
sigmoid_a = tf.nn.sigmoid(a)
tanh_a = tf.nn.tanh(a)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
result_relu_a = sess.run(relu_a)
result_sigmoid_a = sess.run(sigmoid_a)
result_tanh_a = sess.run(tanh_a)
print('the result of relu(a) is : \n{}'.format(result_relu_a))
print('the result of sigmoid(a) is : \n{}'.format(result_sigmoid_a))
print('the result of tanh(a) is : \n{}'.format(result_tanh_a))
the result of relu(a) is :
[[ 1. 2.]
[ 5. 0.]]
the result of sigmoid(a) is :
[[ 0.7310586 0.88079703]
[ 0.99330717 0.11920292]]
the result of tanh(a) is :
[[ 0.76159418 0.96402758]
[ 0.99990916 -0.96402758]]
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
d = tf.constant([[1.,2.,3.,4.],[5.,6.,7.,8.],[9.,10.,11.,12.],[13.,14.,15.,16.]])
print(sess.run(tf.shape(d)))
#由于[4,4] == [4,4] 行和列都为独立
dropout_a44 = tf.nn.dropout(d, 0.5, noise_shape = [4,4])
result_dropout_a44 = sess.run(dropout_a44)
print(result_dropout_a44)
#noise_shpae[0]=4 == tf.shape(d)[0]=4
#noise_shpae[1]=4 != tf.shape(d)[1]=1
#所以[0]即行独立,[1]即列相关,每个行同为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)
#noise_shpae[0]=1 != tf.shape(d)[0]=4
#noise_shpae[1]=4 == tf.shape(d)[1]=4
#所以[1]即列独立,[0]即行相关,每个列同为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
[4 4]
[[ 0. 4. 0. 8.]
[ 0. 0. 14. 0.]
[ 0. 0. 22. 0.]
[ 0. 0. 30. 0.]]
[[ 2. 4. 6. 8.]
[ 0. 0. 0. 0.]
[ 18. 20. 22. 24.]
[ 26. 28. 30. 32.]]
[[ 0. 0. 6. 0.]
[ 0. 0. 14. 0.]
[ 0. 0. 22. 0.]
[ 0. 0. 30. 0.]]
d.shape
TensorShape([Dimension(4), Dimension(4)])