tensorflow-激活函数及tf.nn.dropout
来源:互联网 发布:张国荣为什么自杀知乎 编辑:程序博客网 时间:2024/04/27 08:45
参考《Tensorflow技术解析与实战》
激活函数
- 激活函数(activation function)将神经元计算
wTx+b 的结果经过非线性表达映射到下一层。 - 需要可微,激活函数不会改变输入数据维度。
- sigmoid函数:
σ(x)=11+e−x(1) - 将输出映射到(0,1)内,缺点:
- 软饱和性:取值无穷大时,一阶导数趋于0,容易产生梯度消失。(硬饱和:某阶段一阶导数等于0)
- 将输出映射到(0,1)内,缺点:
- tanh函数:
tanh(x)=1−e−2x1+e−2x(2) - 也具有软饱和,收敛速度比sigmoid快
- 会发生梯度消失
- relu函数:
f(x)=max(x,0)(3)
softplus函数:f(x)=log(1+ex)(4) - relu在
x<0 时硬饱和,x>0 梯度不衰减,为1,缓解梯度消失问题 - 收敛快,提供神经网络稀疏表达能力
- 缺点:
- 部分输入落入硬饱和区,权重无法更新,发生“神经元死亡”
- relu在
- 当输入数据特征相差明显时,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)
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)])
阅读全文
0 0
- tensorflow-激活函数及tf.nn.dropout
- 【Tensorflow】tf.nn.dropout函数
- Tensorflow学习笔记(用哪学哪)tf.nn.dropout
- Tensorflow(r1.4)API--tf.nn.dropout
- TensorFlow-tf.nn.conv2d 函数
- 【Tensorflow】tf.nn.relu函数
- tensorflow函数 tf.nn.xw_plus_b
- TensorFlow函数:tf.nn.in_top_k()
- tf.nn.dropout
- TensorFlow学习---tf.nn.dropout防止过拟合
- 【转载】TensorFlow学习---tf.nn.dropout防止过拟合
- tensorflow-BatchNormalization(tf.nn.moments及tf.nn.batch_normalization)
- Tensorflow -- tf.nn.conv2d() 函数详解
- tensorflow中的tf.nn这类函数
- Tensorflow函数:tf.nn.softmax_cross_entropy_with_logits 讲解
- Tensorflow-tf.nn.embedding_lookup函数原理
- Tensorflow-tf.nn.dynamic_rnn函数解析
- TensorFlow(四) tf.nn.conv2d函数
- LINUX 下载ftp文件夹下的所有内容
- 算法题练习系列之(二十九): 跟奥巴马一起编程
- HDU6180Schedule(贪心)
- HDU 6047
- Java三大器之过滤器(Filter)的工作原理和代码演示
- tensorflow-激活函数及tf.nn.dropout
- uva 116 动态规划 多阶段决策问题 路径记录 lrj-P270
- java内存区域
- 微信内H5支付报签名错误
- [NOIP模拟赛]黑白棋
- Java知识(File类)
- VisualVM安装插件报错
- 关于sklearn的简单使用1(分类)
- javaIO流中文件的拷贝和图片的拷贝