池化层的作用如下-引用《TensorFlow实践》:

池化层的作用是减少过拟合,并通过减小输入的尺寸来提高性能。他们可以用来对输入进行降采样,但会为后续层保留重要的信息。只使用tf.nn.conv2d来减小输入的尺寸也是可以的,但是池化层的效率更高。

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

1.tf.nn.max_pool(value, ksize, strides, padding, name=None)

Performs the max pooling on the input.

  • value: A 4-D Tensor with shape [batch, height, width, channels] and type float32,float64qint8quint8qint32.
  • ksize: A list of ints that has length >= 4. The size of the window for each dimension of the input tensor.
  • strides: A list of ints that has length >= 4. The stride of the sliding window for each dimension of the input tensor.
  • padding: A string, either 'VALID' or 'SAME'. The padding algorithm.
  • name: Optional name for the operation.

当我们的ksize=[1 3 3 1]式,我们取3X3模板池pool当中最大的数据当做中心点的值,strides作为滑动跳跃的间隔,代码如下所示:

import tensorflow as tf

batch_size = 1
input_height = 3
input_width = 3
input_channels = 1
layer_input = tf.constant([
[
[[1.0],[0.2],[1.5]],
[[0.1],[1.2],[1.4]],
[[1.1],[0.4],[0.4]]
]
])
kernel = [batch_size, input_height, input_width,input_channels]
max_pool = tf.nn.max_pool(layer_input,kernel,[1,1,1,1],padding='VALID',name=None)
sess = tf.Session()
sess.run(max_pool)

输出结果如下(注意max_pool的输入的参数的维数一定要正确,否则会报错):

2.tf.nn.avg_pool(value, ksize, strides, padding, name=None)

Performs the average pooling on the input.

Each entry in output is the mean of the corresponding size ksize window in value.

  • value: A 4-D Tensor of shape [batch, height, width, channels] and type float32,float64qint8quint8, or qint32.
  • ksize: A list of ints that has length >= 4. The size of the window for each dimension of the input tensor.
  • strides: A list of ints that has length >= 4. The stride of the sliding window for each dimension of the input tensor.
  • padding: A string, either 'VALID' or 'SAME'. The padding algorithm.
  • name: Optional name for the operation.

跳跃遍历一个张量,并将被卷积核覆盖的各深度值去平均。当整个卷积核都非常重要时,若需要实现值的缩减,平均池化非常有用,例如输入张量宽度和高度很大,但深度很小的情况。

import tensorflow as tf

batch_size = 1
input_height = 3
input_width = 3
input_channels = 1
layer_input = tf.constant([
[
[[1.0],[1.0],[1.0]],
[[1.0],[0.5],[0.0]],
[[0.0],[0.0],[0.0]]
]
])
kernel = [batch_size, input_height, input_width,input_channels]
avg_pool = tf.nn.mavg_pool(layer_input,kernel,[1,1,1,1],padding='VALID',name=None)
sess = tf.Session()
sess.run(avg_pool)

输出结果如下:(1.0+1.0+1.0+0.5+0.0+0.0+0.0+0.0)/9=0.5

TensorFlow池化层-函数的更多相关文章

  1. TensorFlow 池化层

    在 TensorFlow 中使用池化层 在下面的练习中,你需要设定池化层的大小,strides,以及相应的 padding.你可以参考 tf.nn.max_pool().Padding 与卷积 pad ...

  2. tensorflow 1.0 学习:池化层(pooling)和全连接层(dense)

    池化层定义在 tensorflow/python/layers/pooling.py. 有最大值池化和均值池化. 1.tf.layers.max_pooling2d max_pooling2d( in ...

  3. tensorflow的卷积和池化层(二):记实践之cifar10

    在tensorflow中的卷积和池化层(一)和各种卷积类型Convolution这两篇博客中,主要讲解了卷积神经网络的核心层,同时也结合当下流行的Caffe和tf框架做了介绍,本篇博客将接着tenso ...

  4. tensorflow中的卷积和池化层(一)

    在官方tutorial的帮助下,我们已经使用了最简单的CNN用于Mnist的问题,而其实在这个过程中,主要的问题在于如何设置CNN网络,这和Caffe等框架的原理是一样的,但是tf的设置似乎更加简洁. ...

  5. tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图

    tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图 因为很多 demo 都比较复杂,专门抽出这两个函数,写的 demo. 更多教程:http://www.tensorflown ...

  6. 『TensorFlow』卷积层、池化层详解

    一.前向计算和反向传播数学过程讲解

  7. 第十三节,使用带有全局平均池化层的CNN对CIFAR10数据集分类

    这里使用的数据集仍然是CIFAR-10,由于之前写过一篇使用AlexNet对CIFAR数据集进行分类的文章,已经详细介绍了这个数据集,当时我们是直接把这些图片的数据文件下载下来,然后使用pickle进 ...

  8. 学习笔记TF014:卷积层、激活函数、池化层、归一化层、高级层

    CNN神经网络架构至少包含一个卷积层 (tf.nn.conv2d).单层CNN检测边缘.图像识别分类,使用不同层类型支持卷积层,减少过拟合,加速训练过程,降低内存占用率. TensorFlow加速所有 ...

  9. 基于深度学习和迁移学习的识花实践——利用 VGG16 的深度网络结构中的五轮卷积网络层和池化层,对每张图片得到一个 4096 维的特征向量,然后我们直接用这个特征向量替代原来的图片,再加若干层全连接的神经网络,对花朵数据集进行训练(属于模型迁移)

    基于深度学习和迁移学习的识花实践(转)   深度学习是人工智能领域近年来最火热的话题之一,但是对于个人来说,以往想要玩转深度学习除了要具备高超的编程技巧,还需要有海量的数据和强劲的硬件.不过 Tens ...

随机推荐

  1. swift 实践- 10 -- UIProgressView

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  2. C# 通用数据库配置界面,微软原生DLL重整合

    C# 通用数据库配置界面,微软原生DLL重整合 自己整合了 微软自带的数据连接配置界面对话库    Microsoft.Data.ConnectionUI.Dialog.dll  微软自带的数据连接配 ...

  3. mvn tomcat7:help的14个命令

    D:\2018\code\XXX>mvn tomcat7:help [INFO] Scanning for projects... [INFO] [INFO] ----------------- ...

  4. Confluence 6 管理员联系表单的后台配置界面

    管理员联系表单的后台配置界面截图和配置. 对输入的数据进行编辑和选择是否启用发送电子邮件给管理员 https://www.cwiki.us/display/CONFLUENCEWIKI/Configu ...

  5. 解决在编程方式下无法访问Spark Master问题

    我们可以选择使用spark-shell,spark-submit或者编写代码的方式运行Spark.在产品环境下,利用spark-submit将jar提交到spark,是较为常见的做法.但是在开发期间, ...

  6. 2。创建第一个angular应用,已经开发前的一些配置

    现在我们开始新建一个angular5的脚手架  . 到想要建项目的目录下.比如我的 在  D:\JsProjects 进入cmd或者powershell cd 进入该文件夹下 然后开始新建,ng ne ...

  7. linux三剑客

    grep grep       "oldboy"     test.txt   过滤掉文件中oldboy的字符串 -v                               ...

  8. 初识dubbo

    1. 为什么需要 Dubbo(摘自http://dubbo.apache.org/zh-cn/docs/user/quick-start.html) 随着互联网的发展,网站应用的规模不断扩大,常规的垂 ...

  9. Django标签&迭代&循环&过滤

    1.{% for Person in persons %}模板标签的替换,就是利用了基础模板的底层设计,嵌套了其他显示的内容.常见的内容替换标签{% block content %}{%endbloc ...

  10. MongoDB数据库备份与还原、单表的导入导出

    -------------------MongoDB备份与恢复------------------- 1.MongoDB数据库备份     1.语法:         mongodump -h dbh ...