激活函数的作用如下-引用《TensorFlow实践》:

这些函数与其他层的输出联合使用可以生成特征图。他们用于对某些运算的结果进行平滑或者微分。其目标是为神经网络引入非线性。曲线能够刻画出输入的复杂的变化。TensorFlow提供了多种激活函数,在CNN中一般使用tf.nn.relu的原因是因为,尽管relu会导致一些信息的损失,但是性能突出。在刚开始设计模型时,都可以采用relu的激活函数。高级用户也可以自己创建自己的激活函数,评价激活函数是否有用的主要因素参看如下几点:

1)该函数是单调的,随着输入的增加增加减小减小,从而利用梯度下降法找到局部极值点成为可能。

2)该函数是可微分的,以保证函数定义域内的任意一点上导数都存在,从而使得梯度下降法能够正常使用来自这类激活函数的输出。

常见的TensorFlow提供的激活函数如下:(详细请参考http://www.tensorfly.cn/tfdoc/api_docs/python/nn.html)

1.tf.nn.relu(features, name=None)

Computes rectified linear: max(features, 0).

  • features: A Tensor. Must be one of the following types: float32float64int32int64,uint8int16int8.
  • name: A name for the operation (optional).

注:

优点在于不受‘梯度消失’的影响,取值范围为[0,+∞]。

缺点在于当使用了较大的学习速率时,易受到饱和的神经元的影响。

2.tf.nn.relu6(features, name=None)

Computes Rectified Linear 6: min(max(features, 0), 6).

  • features: A Tensor with type floatdoubleint32int64uint8int16, or int8.
  • name: A name for the operation (optional).

3.tf.sigmoid(x, name=None)

Computes sigmoid of x element-wise.

Specifically, y = 1 / (1 + exp(-x)).

  • x: A Tensor with type floatdoubleint32complex64int64, or qint32.
  • name: A name for the operation (optional).

注:

优点在于sigmoid函数在样本训练的神经网络中可以将输出保持在[0.0,1.0]内部的能力非常有用。

缺点在于当输出接近饱和或剧烈变化时,对输出范围的这种缩减往往会带来一些不利影响。

4.tf.nn.softplus(features, name=None)

Computes softplus: log(exp(features) + 1).

  • features: A Tensor. Must be one of the following types: float32float64int32int64,uint8int16int8.
  • name: A name for the operation (optional).

5.tf.tanh(x, name=None)

Computes hyperbolic tangent of x element-wise.

  • x: A Tensor with type floatdoubleint32complex64int64, or qint32.
  • name: A name for the operation (optional).

注:

优点在于双曲正切函数和sigmoid函数比较相似,tanh拥有sigmoid的优点,用时tanh具有输出负值的能力,tanh的值域为[-1.0,1.0].

MATLAB代码来体现函数的类型

clear all
close all
clc
% ACTVE FUNCTION %
X = linspace(-5,5,100);
plot(X)
title('feature = X')
% tf.nn.relu(features, name=None):max(features, 0) %
Y_relu = max(X,0);
figure,plot(Y_relu)
title('tf.nn.relu(features, name=None)')
% tf.nn.relu6(features, name=None):min(max(features, 0), 6) %
Y_relu6 = min(max(X,0),6);
figure,plot(Y_relu6)
title('tf.nn.relu6(features, name=None)')
% tf.sigmoid(x, name=None):y = 1 / (1 + exp(-x))%
Y_sigmoid = 1./(1+exp(-1.*X));
figure,plot(Y_sigmoid)
title('tf.sigmoid(x, name=None)')
% tf.nn.softplus(features, name=None):log(exp(features) + 1) %
Y_softplus = log(exp(X) + 1);
figure,plot(Y_softplus)
title('tf.nn.softplus(features, name=None)')
% tf.tanh(x, name=None):tanh(features) %
Y_tanh = tanh(X);
figure,plot(Y_tanh)
title('tf.tanh(x, name=None)')

           X=feature                                 tf.nn.relu(features, name=None)    

       tf.nn.relu6(features, name=None)                         tf.sigmoid(x, name=None)

tf.nn.softplus(features, name=None)                        tf.tanh(x, name=None)

归一化函数的重要作用-引用《TensorFlow实践》

归一化层并非CNN所独有。在使用tf.nn.relu时,考虑输出的归一化是有价值的(详细参看http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf)。由于relu是无界函数,利用某些形式的归一化来识别哪些高频特征通常是十分有用的。local response normalization最早是由Krizhevsky和Hinton在关于ImageNet的论文里面使用的一种数据标准化方法,即使现在,也依然会有不少CNN网络会使用到这种正则手段。

tf.nn.local_response_normalization(input, depth_radius=None, bias=None, alpha=None, beta=None, name=None)

Local Response Normalization.

The 4-D input tensor is treated as a 3-D array of 1-D vectors (along the last dimension), and each vector is normalized independently. Within a given vector, each component is divided by the weighted, squared sum of inputs within depth_radius. In detail,

sqr_sum[a, b, c, d] =
sum(input[a, b, c, d - depth_radius : d + depth_radius + 1] ** 2)
output = input / (bias + alpha * sqr_sum ** beta)

  • 第一个参数input:这个输入就是feature map了,既然是feature map,那么它就具有[batch, height, width, channels]这样的shape
  • 第二个参数depth_radius:这个值需要自己指定,就是上述公式中的n/2
  • 第三个参数bias:上述公式中的k
  • 第四个参数alpha:上述公式中的α
  • 第五个参数beta:上述公式中的β
  • 第六个参数name:上述操作的名称
  • 返回值是新的feature map,它应该具有和原feature map相同的shape

以上是这种归一手段的公式,其中a的上标指该层的第几个feature map,a的下标x,y表示feature map的像素位置,N指feature map的总数量,公式里的其它参数都是超参,需要自己指定的。

这种方法是受到神经科学的启发,激活的神经元会抑制其邻近神经元的活动(侧抑制现象),至于为什么使用这种正则手段,以及它为什么有效,查阅了很多文献似乎也没有详细的解释,可

能是由于后来提出的batch normalization手段太过火热,渐渐的就把local response normalization掩盖了吧。

import tensorflow as tf  

a = tf.constant([
[[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[8.0, 7.0, 6.0, 5.0],
[4.0, 3.0, 2.0, 1.0]],
[[4.0, 3.0, 2.0, 1.0],
[8.0, 7.0, 6.0, 5.0],
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0]]
])
#reshape a,get the feature map [batch:1 height:2 width:2 channels:8]
a = tf.reshape(a, [1, 2, 2, 8]) normal_a=tf.nn.local_response_normalization(a,2,0,1,1)
with tf.Session() as sess:
print("feature map:")
image = sess.run(a)
print (image)
print("normalized feature map:")
normal = sess.run(normal_a)
print (normal)

运行结果:

解释:

这里我取了n/2=2,k=0,α=1,β=1。公式中的N就是输入张量的通道总数:由a = tf.reshape(a, [1, 2, 2, 8]) 得到 N=8,变量i代表的是不同的通道,从0开始到7.

举个例子,比如对于一通道的第一个像素“1”来说,我们把参数代人公式就是1/(1^2+2^2+3^2)=0.07142857,对于四通道的第一个像素“4”来说,公式就是4/(2^2+3^2+4^2+5^2+6^2)=0.04444445,以此类推。转载:http://blog.csdn.net/mao_xiao_feng/article/details/53488271

TensorFlow激活函数+归一化-函数的更多相关文章

  1. Tensorflow Batch normalization函数

    Tensorflow Batch normalization函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 stackoverflow上tensorflow实现BN的不同函数的 ...

  2. tensorflow.nn.bidirectional_dynamic_rnn()函数的用法

    在分析Attention-over-attention源码过程中,对于tensorflow.nn.bidirectional_dynamic_rnn()函数的总结: 首先来看一下,函数: def bi ...

  3. TensorFlow多层感知机函数逼近过程详解

    http://c.biancheng.net/view/1924.html Hornik 等人的工作(http://www.cs.cmu.edu/~bhiksha/courses/deeplearni ...

  4. TensorFlow——批量归一化操作

    批量归一化 在对神经网络的优化方法中,有一种使用十分广泛的方法——批量归一化,使得神经网络的识别准确度得到了极大的提升. 在网络的前向计算过程中,当输出的数据不再同一分布时,可能会使得loss的值非常 ...

  5. TensorFlow从0到1之TensorFlow多层感知机函数逼近过程(23)

    Hornik 等人的工作(http://www.cs.cmu.edu/~bhiksha/courses/deeplearning/Fall.2016/notes/Sonia_Hornik.pdf)证明 ...

  6. TensorFlow常用的函数

    TensorFlow中维护的集合列表 在一个计算图中,可以通过集合(collection)来管理不同类别的资源.比如通过 tf.add_to_collection 函数可以将资源加入一个 或多个集合中 ...

  7. Tensorflow常用的函数:tf.cast

    1.tf.cast(x,dtype,name) 此函数的目的是为了将x数据,准换为dtype所表示的类型,例如tf.float32,tf.bool,tf.uint8等 example:  import ...

  8. [转载]Tensorflow 的reduce_sum()函数的axis,keep_dim这些参数到底是什么意思?

    转载链接:https://www.zhihu.com/question/51325408/answer/125426642来源:知乎 这个问题无外乎有三个难点: 什么是sum 什么是reduce 什么 ...

  9. 查询tensorflow中的函数用法

    一下均在ubuntu环境下: (1)方法一,使用help()函数: 比如对于tf.placeholder(),在命令行中输入import tensorflow as tf , help(tf.plac ...

随机推荐

  1. response.setContentType()的String参数及对应类型

    response.addHeader("Content-Disposition", "attachment;filename="+ filename); res ...

  2. Oracle12c 从入门到精通(第二版) 闫红岩 金松河 编著

    声明:本文只是用于学习笔记使用.方便查询.若需要书本,请到书店购买. 本书封面 前言 第1章 Oracle数据库概述 1.1 Oracle数据库产品结构及组成 1.1.1 标准版 1.1.2 标准版 ...

  3. Confluence 6 白名单允许进入和禁用

    允许进入 启用 CORS 请求,将会从特定的 URL 来源中允许进入(Allow Incoming).URL 格式必须匹配格式 scheme://host[:port],结尾是没有反斜杠的(:port ...

  4. Confluence 6 数据导入和导出

    Confluence 管理员和用户可以从各种方法向  Confluence 中导入数据.针对不同的导入方式,有关权限的要求也是不相同的.请参考页面 Import Content Into Conflu ...

  5. day14 迭代器 生成器 面向过程思想

    "" 迭代器 什么是迭代器(iterator) 器指的某种工具, 迭代指的是更新换代的过程,例如应用程序的版本更新从1.0 变成 1.1 再1.2 迭代的目的是要根据上一个结果,产 ...

  6. mysql中的sql

    变量 用户变量: 在用户变量前加@ 系统变量: 在系统变量前加@@ 运算符 算术运算符有: +(加), -(减), * (乘), / (除) 和% (求模) 五中运算 位运算符有: & (位于 ...

  7. Nginx详解二十六:Nginx架构篇之性能优化

    一.性能优化考虑点 1.当前系统结构瓶颈 通过压力测试观察指标.日志检测.性能分析 2.了解业务模式 接口业务类型.系统层次化结构 3.性能与安全 二.ab接口压力测试工具 1.安装:yum -y i ...

  8. Ajax增删改查-----------删 改

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 16位和32位的80X86汇编语言的区别

    需要注意的是汇编不是一种语言,不同平台有不同的汇编语言对应,因为汇编和操作系统平台相关,所以汇编语言没有移植性.对于IA-32架构平台而言,选用的32位80386汇编语言,也就只说讨论的操作系统平台是 ...

  10. windows server 2012 R2 远程桌面授权模式尚未配置

    windows server 2012 R2 远程桌面授权模式尚未配置,远程桌面服务将在120天内停止工作.如何破解这个宽限期,目前企业7位协议号码均不包含2012 R2以上授权. 那么只能蛋疼的“破 ...