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)”是最为常见 ...
随机推荐
- Alpha 冲刺 —— 十分之三
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助后端界面的开发 搭建项目运行的服务器环境 ...
- UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举)
UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举) 题意分析 给出n*m的矩形格子,给出k个点,每个格子里面可以放一个点.现在要求格子的最外围一圈的每行每列,至少要放一个 ...
- 单点登录(一)-----理论-----单点登录SSO的介绍和CAS+选型
什么是单点登录(SSO) 单点登录主要用于多系统集成,即在多个系统中,用户只需要到一个中央服务器登录一次即可访问这些系统中的任何一个,无须多次登录. 单点登录(Single Sign On),简称为 ...
- k最近邻算法(kNN)
from numpy import * import operator from os import listdir def classify0(inX, dataSet, labels, k): d ...
- Linux上给php配置redis扩展
说明,在项目开发中难免会遇到redis中,那我应该如何配置redis这样的一个扩展呢,看下面流程: 一.安装Redis PHP在安装redis扩展时,难免要看一下官网下载安装流程,链接如下: http ...
- web项目中日志管理工具的使用
在web项目中,很多时候会用到日志管理工具,常见的日志管理用具有:JDK logging(配置文件:logging.properties) 和log4j(配置文件:log4j.properties) ...
- 【Asp.net入门4-04】使用Nuget
- 【Asp.net入门04】第一个ASP.NET 应用程序-如何添加Web窗体到网站中
添加Web窗体 本部分内容: 什么是web form 怎样添加web form 1.添加Web窗体到项目中 Web 窗体是一项 ASP.NET 功能,您可以使用它为 Web 应用程序创建用户界面.We ...
- SGD中的重要参数
Learning Rate 学习率决定了权值更新的速度,设置得太大会使结果超过最优值,太小会使下降速度过慢.仅靠人为干预调整参数需要不断修改学习率,因此后面3种参数都是基于自适应的思路提出的解决方案. ...
- 蓝桥杯 剪邮票 DFS (不错的题目)
剪邮票 如[图1.jpg], 有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连)比如,[图2.jpg],[图3.jpg]中,粉红色所示部分就是合格的 ...