TF-池化函数 tf.nn.max_pool 的介绍
转载自此大神 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 的介绍的更多相关文章
- tf入门-池化函数 tf.nn.max_pool 的介绍
转载自此大神 http://blog.csdn.net/mao_xiao_feng/article/details/53453926 max pooling是CNN当中的最大值池化操作,其实用法和卷积 ...
- CNN之池化层tf.nn.max_pool | tf.nn.avg_pool | tf.reduce_mean | padding的规则解释
摘要:池化层的主要目的是降维,通过滤波器映射区域内取最大值.平均值等操作. 均值池化:tf.nn.avg_pool(input,ksize,strides,padding) 最大池化:tf.nn.ma ...
- 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)
上一节,我们已经讲解了使用全连接网络实现手写数字识别,其正确率大概能达到98%,这一节我们使用卷积神经网络来实现手写数字识别, 其准确率可以超过99%,程序主要包括以下几块内容 [1]: 导入数据,即 ...
- 深度学习原理与框架-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 ...
- tensorflow max_pool(最大池化)应用
1.最大池化 max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似. tf.nn.max_pool(value, ksize, strides, padding, name=Non ...
- 第十四节,TensorFlow中的反卷积,反池化操作以及gradients的使用
反卷积是指,通过测量输出和已知输入重构未知输入的过程.在神经网络中,反卷积过程并不具备学习的能力,仅仅是用于可视化一个已经训练好的卷积神经网络,没有学习训练的过程.反卷积有着许多特别的应用,一般可以用 ...
- 【小白学PyTorch】21 Keras的API详解(下)池化、Normalization层
文章来自微信公众号:[机器学习炼丹术].作者WX:cyx645016617. 参考目录: 目录 1 池化层 1.1 最大池化层 1.2 平均池化层 1.3 全局最大池化层 1.4 全局平均池化层 2 ...
- MinkowskiPooling池化(上)
MinkowskiPooling池化(上) 如果内核大小等于跨步大小(例如kernel_size = [2,1],跨步= [2,1]),则引擎将更快地生成与池化函数相对应的输入输出映射. 如果使用U网 ...
- CNN学习笔记:池化层
CNN学习笔记:池化层 池化 池化(Pooling)是卷积神经网络中另一个重要的概念,它实际上是一种形式的降采样.有多种不同形式的非线性池化函数,而其中“最大池化(Max pooling)”是最为常见 ...
随机推荐
- linux内核分析 第六周读书笔记
第三章 进程管理 3.1 进程 进程:处于执行期的程序 线程是在进程活动中的对象:内核调度的对象是线程而不是进程,在Linux系统中,并不区分线程和进程 在现代操作系统中, 进程提供两种虚拟机制:虚拟 ...
- rovio视觉里程计的笔记
rovio是一个紧耦合,基于图像块的滤波实现的VIO. 他的优点是:计算量小(EKF,稀疏的图像块),但是对应不同的设备需要调参数,参数对精度很重要.没有闭环,没有mapping thread.经常存 ...
- sql 事务的四种隔离级别
在 SQL 标准中定义了四种隔离级别,每一种级别都规定了一个事务中所做的修改,哪些在事务内和事务间是可见的,哪些是不可见的.较低级别的隔离通常可以执行更高的并发,系统的开销也更低. read unco ...
- Java入门:MyEclipse安装与破解教程
MyEclipse Pro 2014 GA的安装过程请参考网页:http://blog.my-eclipse.cn/myeclipse-2014-download-and-install.html 安 ...
- 安装ucenter discuzX
需要先安装ucenter再安装discuzX!! ucenter下载,官网上不大好下载: http://www.comsenz.com/downloads/install/ucenter 下载不了; ...
- Java基础-IO流对象之转换流(InputStreamReader与OutoutStreamWriter)
Java基础-IO流对象之转换流(InputStreamReader与OutoutStreamWriter) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.转换流概述 我们之前 ...
- SiteMesh的使用--笔记
本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处. http://www.cnblogs.com/king-xg/p/6472659.html Sitemesh ...
- nginx.conf 基础配置
### 全局块开始### #配置允许运行nginx服务器的用户和用户组 user nobody; #配置允许nginx进程生成的worker process 数 worker_processes 1; ...
- soj1046. Plane Spotting
1046. Plane Spotting Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Craig is fond o ...
- 逆序对 inversion
评测传送门 [问题描述] 有一个1 − n的排列,你会依次进行m次操作,第i次操作表示为(x i , y i ),交换以这两个 值为下标的元素,每次操作有一半的概率成功,你需要求出最后序列的逆序对 ...