我爱编程程序员

tensorflow之--激活函数

2018-02-22  本文已影响0人  DonkeyJason

激活函数

dropout函数

image.png

各激活函数运行代码:

note:代码运行环境 windows10, python3.x, tensorflow1.4.0

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]]

dropout()函数实例代码:

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
1
TensorShape([Dimension(4), Dimension(4)])
上一篇下一篇

猜你喜欢

热点阅读