转载自此大神 http://blog.csdn.net/mao_xiao_feng/article/details/53453926

max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似

有些地方可以从卷积去参考【TensorFlow】tf.nn.conv2d是怎样实现卷积的?

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

参数是四个,和卷积很类似:

第一个参数value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape

第二个参数ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1

第三个参数strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,stride, 1]

第四个参数padding:和卷积类似,可以取'VALID' 或者'SAME'

返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式

示例源码:

假设有这样一张图,双通道

第一个通道:

第二个通道:

用程序去做最大值池化:

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]]
]) a=tf.reshape(a,[1,4,4,2]) pooling=tf.nn.max_pool(a,[1,2,2,1],[1,1,1,1],padding='VALID')
with tf.Session() as sess:
print("image:")
image=sess.run(a)
print (image)
print("reslut:")
result=sess.run(pooling)
print (result)

这里步长为1,窗口大小2×2,输出结果:

image:
[[[[ 1. 2.]
[ 3. 4.]
[ 5. 6.]
[ 7. 8.]] [[ 8. 7.]
[ 6. 5.]
[ 4. 3.]
[ 2. 1.]] [[ 4. 3.]
[ 2. 1.]
[ 8. 7.]
[ 6. 5.]] [[ 1. 2.]
[ 3. 4.]
[ 5. 6.]
[ 7. 8.]]]]
reslut:
[[[[ 8. 7.]
[ 6. 6.]
[ 7. 8.]] [[ 8. 7.]
[ 8. 7.]
[ 8. 7.]] [[ 4. 4.]
[ 8. 7.]
[ 8. 8.]]]]

池化后的图就是:

证明了程序的结果是正确的。

我们还可以改变步长

pooling=tf.nn.max_pool(a,[1,2,2,1],[1,2,2,1],padding='VALID')  

最后的result就变成:

reslut:
[[[[ 8. 7.]
[ 7. 8.]] [[ 4. 4.]
[ 8. 8.]]]]

下面是我自己写的测试代码和测试结果:

import tensorflow as tf

# def max_pool(value, ksize, strides, padding, data_format="NHWC", name=None)
#8x8的4维全1矩阵
value = tf.ones([1, 8, 8, 1], dtype=tf.float32) oplist = [] ksize = [1, 2, 2, 1]
strides = [1, 1, 1, 1]
reth = tf.nn.max_pool(value, ksize, strides, padding='VALID')
oplist.append([reth, 'case 1']) ksize = [1, 4, 4, 1]
strides = [1, 1, 1, 1]
reth = tf.nn.max_pool(value, ksize, strides, padding='VALID')
oplist.append([reth, 'case 2']) ksize = [1, 6, 6, 1]
strides = [1, 1, 1, 1]
reth = tf.nn.max_pool(value, ksize, strides, padding='VALID')
oplist.append([reth, 'case 3']) ksize = [1, 2, 2, 1]
strides = [1, 2, 2, 1]
reth = tf.nn.max_pool(value, ksize, strides, padding='VALID')
oplist.append([reth, 'case 4']) ksize = [1, 2, 2, 1]
strides = [1, 2, 2, 1]
reth = tf.nn.max_pool(value, ksize, strides, padding='SAME')
oplist.append([reth, 'case 5']) with tf.Session() as a_sess:
a_sess.run(tf.global_variables_initializer())
for aop in oplist:
print("----------{}---------".format(aop[1]))
print("shape =",aop[0].shape)
print("content=",a_sess.run(aop[0]))
print('---------------------\n\n')

结果为

C:\Users\Administrator\Anaconda3\python.exe C:/Users/Administrator/PycharmProjects/p3test/tf_maxpool.py
2017-05-10 16:43:25.690336: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations.
2017-05-10 16:43:25.691336: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-10 16:43:25.691336: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-10 16:43:25.692336: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-10 16:43:25.692336: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-10 16:43:25.692336: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
----------case 1---------
shape = (1, 7, 7, 1)
content= [[[[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]]]]
--------------------- ----------case 2---------
shape = (1, 5, 5, 1)
content= [[[[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]]]]
--------------------- ----------case 3---------
shape = (1, 3, 3, 1)
content= [[[[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]]]]
--------------------- ----------case 4---------
shape = (1, 4, 4, 1)
content= [[[[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]]]]
--------------------- ----------case 5---------
shape = (1, 4, 4, 1)
content= [[[[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]] [[ 1.]
[ 1.]
[ 1.]
[ 1.]]]]
--------------------- Process finished with exit code 0

TF-池化函数 tf.nn.max_pool 的介绍的更多相关文章

  1. tf入门-池化函数 tf.nn.max_pool 的介绍

    转载自此大神 http://blog.csdn.net/mao_xiao_feng/article/details/53453926 max pooling是CNN当中的最大值池化操作,其实用法和卷积 ...

  2. CNN之池化层tf.nn.max_pool | tf.nn.avg_pool | tf.reduce_mean | padding的规则解释

    摘要:池化层的主要目的是降维,通过滤波器映射区域内取最大值.平均值等操作. 均值池化:tf.nn.avg_pool(input,ksize,strides,padding) 最大池化:tf.nn.ma ...

  3. 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)

    上一节,我们已经讲解了使用全连接网络实现手写数字识别,其正确率大概能达到98%,这一节我们使用卷积神经网络来实现手写数字识别, 其准确率可以超过99%,程序主要包括以下几块内容 [1]: 导入数据,即 ...

  4. 深度学习原理与框架-Tensorflow卷积神经网络-卷积神经网络mnist分类 1.tf.nn.conv2d(卷积操作) 2.tf.nn.max_pool(最大池化操作) 3.tf.nn.dropout(执行dropout操作) 4.tf.nn.softmax_cross_entropy_with_logits(交叉熵损失) 5.tf.truncated_normal(两个标准差内的正态分布)

    1. tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')  # 对数据进行卷积操作 参数说明:x表示输入数据,w表示卷积核, stride ...

  5. tensorflow max_pool(最大池化)应用

    1.最大池化 max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似. tf.nn.max_pool(value, ksize, strides, padding, name=Non ...

  6. 第十四节,TensorFlow中的反卷积,反池化操作以及gradients的使用

    反卷积是指,通过测量输出和已知输入重构未知输入的过程.在神经网络中,反卷积过程并不具备学习的能力,仅仅是用于可视化一个已经训练好的卷积神经网络,没有学习训练的过程.反卷积有着许多特别的应用,一般可以用 ...

  7. 【小白学PyTorch】21 Keras的API详解(下)池化、Normalization层

    文章来自微信公众号:[机器学习炼丹术].作者WX:cyx645016617. 参考目录: 目录 1 池化层 1.1 最大池化层 1.2 平均池化层 1.3 全局最大池化层 1.4 全局平均池化层 2 ...

  8. MinkowskiPooling池化(上)

    MinkowskiPooling池化(上) 如果内核大小等于跨步大小(例如kernel_size = [2,1],跨步= [2,1]),则引擎将更快地生成与池化函数相对应的输入输出映射. 如果使用U网 ...

  9. CNN学习笔记:池化层

    CNN学习笔记:池化层 池化 池化(Pooling)是卷积神经网络中另一个重要的概念,它实际上是一种形式的降采样.有多种不同形式的非线性池化函数,而其中“最大池化(Max pooling)”是最为常见 ...

随机推荐

  1. keepalived回顾

    Keepalived是lvs的扩展项目,因此它们之间具备良好的兼容性. 通过对服务器池对象的健康检查,实现对失效机器/服务的故障隔离: 负载均衡器之间的失败切换failover,通过VRRPv2 st ...

  2. BZOJ 1391 [Ceoi2008]order

    1391: [Ceoi2008]order Description 有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成,你可以通过购买或租用机器来完 ...

  3. Centos最小化安装后还需要安装的软件包收集

    自己也是初学,只是记录一下自己使用的时候需要安装的包: 1. yum install -y make wget mlocate net-tools 2. yum install -y gcc open ...

  4. IDA error of " positive sp value has been found"

    问:用IDA静态分析,函数结尾出现 endp ; sp-analysis failed 用F5调不出伪代码,不知道是什么原因,请问有什么解决办法没有? 答:endp ; sp-analysis fai ...

  5. Docker 安装tensorflow

    安装DOCKER 1. https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/ nstall from a packag ...

  6. P1392 取数

    P1392 取数 题目描述 在一个n行m列的数阵中,你须在每一行取一个数(共n个数),并将它们相加得到一个和.对于给定的数阵,请你输出和前k小的取数方法. 说明 对于20%的数据,n≤8 对于100% ...

  7. Casting a Classifier into a Fully Convolutional Network将带全连接的网络做成全卷积网络

    详见:http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/net_surgery.ipynb 假设使用标准的caffe ...

  8. Mac下Clang编译libcurl

    使用终端进入curl的根目录下,执行 ./configre CC=clang make make install 之后前往/usr/local/lib下生成了libcurl.a和相应的动态库,头文件在 ...

  9. HTTP 错误 500.19 请求的页面的相关配置数据无效 解决办法

    "HTTP 错误 500.19 请求的页面的相关配置数据无效" 解决办法   HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该 ...

  10. [转] malloc基本实现

    任何一个用过或学过C的人对malloc都不会陌生.大家都知道malloc可以分配一段连续的内存空间,并且在不再使用时可以通过free释放掉.但是,许多程序员对malloc背后的事情并不熟悉,许多人甚至 ...