DL_tf-activation function and tf.nn.dropout

来源:互联网 发布:证件照制作软件 编辑:程序博客网 时间:2024/03/29 20:48

参考《Tensorflow技术解析与实战》

激活函数

  • 激活函数(activation function)将神经元计算wTx+b的结果经过非线性表达映射到下一层。
  • 需要可微,激活函数不会改变输入数据维度。
  • sigmoid函数:
    σ(x)=11+ex(1)

    • 将输出映射到(0,1)内,缺点:
      • 软饱和性:取值无穷大时,一阶导数趋于0,容易产生梯度消失。(硬饱和:某阶段一阶导数等于0)
  • tanh函数:
    tanh(x)=1e2x1+e2x(2)

    • 也具有软饱和,收敛速度比sigmoid快
    • 会发生梯度消失
  • relu函数:
    f(x)=max(x,0)(3)

    softplus函数:
    f(x)=log(1+ex)(4)

    • relu在x<0梯度不衰减,为1,缓解梯度消失问题
    • 收敛快,提供神经网络稀疏表达能力
    • 缺点:
      • 部分输入落入硬饱和区,权重无法更新,发生“神经元死亡”
  • 当输入数据特征相差明显时,tanh效果好,在nlp上用处广泛。
  • 当特征不明显时,sigmoid效果比较好。
  • 使用sigmoid和tanh时,输入需要进行规范化,否则激活后的值全部进入平坦区,隐层输出趋于相同,丧失特征表达。
  • relu有时可以不需要,目前大多数选择relu

dropout函数

  • 以keep_prob的概率值决定是否被抑制,若抑制则神经元为0,若不被抑制,则神经元输出值y y=1keep_prob
import tensorflow as tfa = tf.constant([[1.,2.],[5.,-2.]])relu_a = tf.nn.relu(a)sigmoid_a = tf.nn.sigmoid(a)tanh_a = tf.nn.tanh(a)
  • 1
  • 2
  • 3
  • 4
  • 5
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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
[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)])
原创粉丝点击