前言

CNN中卷积完后有个步骤叫pooling, 在ICLR2013上,作者Zeiler提出了另一种pooling手段(最常见的就是mean-pooling和max-pooling),叫stochastic pooling。只需要对Feature Map中的元素按照其概率值大小随机选择,元素选中的概率与其数值大小正相关,并非如同max pooling那样直接选取最大值。这种随机池化操作不但最大化地保证了取值的Max,也部分确保不会所有的元素都被选取max值,从而提高了泛化能力。

stochastic pooling方法非常简单,只需对feature map中的元素按照其概率值大小随机选择,即元素值大的被选中的概率也大。而不像max-pooling那样,永远只取那个最大值元素。

计算过程

  1)先将方格中的元素同时除以它们的和sum,得到概率矩阵;
  2)按照概率随机选中方格;
  3)pooling得到的值就是方格位置的值。
  使用stochastic pooling时(即test过程),其推理过程也很简单,对矩阵区域求加权平均即可。
  在反向传播求导时,只需保留前向传播已经记录被选中节点的位置的值,其它值都为0,这和max-pooling的反向传播非常类似。

  假设feature map中的pooling区域元素值如下:

3*3大小的,元素值和sum=0+1.1+2.5+0.9+2.0+1.0+0+1.5+1.0=10

方格中的元素同时除以sum后得到的矩阵元素为:

每个元素值表示对应位置处值的概率,现在只需要按照该概率来随机选一个,方法是:将其看作是9个变量的多项式分布,然后对该多项式分布采样即可,theano中有直接的multinomial()来函数完成。当然也可以自己用01均匀分布来采样,将单位长度1按照那9个概率值分成9个区间(概率越大,覆盖的区域越长,每个区间对应一个位置),然后随机生成一个数后看它落在哪个区间。

  比如如果随机采样后的矩阵为:

则这时候的poolng值为1.5

  使用stochastic pooling时(即test过程),其推理过程也很简单,对矩阵区域求加权平均即可。比如对上面的例子求值过程为为: 0*0+1.1*0.11+2.5*0.25+0.9*0.09+2.0*0.2+1.0*0.1+0*0+1.5*0.15+1.0*0.1=1.625 说明此时对小矩形pooling后的结果为1.625.

  在反向传播求导时,只需保留前向传播已经记录被选中节点的位置的值,其它值都为0,这和max-pooling的反向传播非常类似。

Stochastic pooling优点:

方法简单;

  泛化能力更强;

  可用于卷积层(文章中是与Dropout和DropConnect对比的,说是Dropout和DropConnect不太适合于卷积层. 不过个人感觉这没什么可比性,因为它们在网络中所处理的结构不同);

  至于为什么stochastic pooling效果好,作者说该方法也是模型平均的一种,没怎么看懂。

  关于Stochastic Pooling的前向传播过程和推理过程的代码可参考下列代码(没包括bp过程,所以代码中pooling选择的位置没有保存下来):

"""
An implementation of stochastic max-pooling, based onStochastic Pooling for Regularization of Deep Convolutional Neural Networks
Matthew D. Zeiler, Rob Fergus, ICLR 2013
"""__authors__ = "Mehdi Mirza"
__copyright__ = "Copyright 2010-2012, Universite de Montreal"
__credits__ = ["Mehdi Mirza", "Ian Goodfellow"]
__license__ = "3-clause BSD"
__maintainer__ = "Mehdi Mirza"
__email__ = "mirzamom@iro"import numpy
import theano
from theano import tensor
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
from theano.gof.op import get_debug_valuesdef stochastic_max_pool_bc01(bc01, pool_shape, pool_stride, image_shape, rng = None):"""Stochastic max pooling for training as defined in:Stochastic Pooling for Regularization of Deep Convolutional Neural NetworksMatthew D. Zeiler, Rob Fergusbc01: minibatch in format (batch size, channels, rows, cols),IMPORTANT: All values should be poitiviepool_shape: shape of the pool region (rows, cols)pool_stride: strides between pooling regions (row stride, col stride)image_shape: avoid doing some of the arithmetic in theanorng: theano random stream"""r, c = image_shapepr, pc = pool_shapers, cs = pool_stridebatch = bc01.shape[0] #总共batch的个数channel = bc01.shape[1] #通道个数if rng is None:rng = RandomStreams(2022)# Compute index in pooled space of last needed pool# (needed = each input pixel must appear in at least one pool)def last_pool(im_shp, p_shp, p_strd):rval = int(numpy.ceil(float(im_shp - p_shp) / p_strd))assert p_strd * rval + p_shp >= im_shpassert p_strd * (rval - 1) + p_shp < im_shpreturn rval #表示pool过程中需要移动的次数return T.dot(x, self._W)# Compute starting row of the last poollast_pool_r = last_pool(image_shape[0] ,pool_shape[0], pool_stride[0]) * pool_stride[0] #最后一个pool的起始位置# Compute number of rows needed in image for all indexes to work outrequired_r = last_pool_r + pr #满足上面pool条件时所需要image的高度last_pool_c = last_pool(image_shape[1] ,pool_shape[1], pool_stride[1]) * pool_stride[1]required_c = last_pool_c + pc# final result shaperes_r = int(numpy.floor(last_pool_r/rs)) + 1 #最后pool完成时图片的shaperes_c = int(numpy.floor(last_pool_c/cs)) + 1for bc01v in get_debug_values(bc01):assert not numpy.any(numpy.isinf(bc01v))assert bc01v.shape[2] == image_shape[0]assert bc01v.shape[3] == image_shape[1]# padding,如果不能整除移动,需要对原始图片进行扩充padded = tensor.alloc(0.0, batch, channel, required_r, required_c)name = bc01.nameif name is None:name = 'anon_bc01'bc01 = tensor.set_subtensor(padded[:,:, 0:r, 0:c], bc01)bc01.name = 'zero_padded_' + name# unravelingwindow = tensor.alloc(0.0, batch, channel, res_r, res_c, pr, pc)window.name = 'unravlled_winodows_' + namefor row_within_pool in xrange(pool_shape[0]):row_stop = last_pool_r + row_within_pool + 1for col_within_pool in xrange(pool_shape[1]):col_stop = last_pool_c + col_within_pool + 1win_cell = bc01[:,:,row_within_pool:row_stop:rs, col_within_pool:col_stop:cs]window  =  tensor.set_subtensor(window[:,:,:,:, row_within_pool, col_within_pool], win_cell) #windows中装的是所有的pooling数据块# find the normnorm = window.sum(axis = [4, 5]) #求和当分母用 norm = tensor.switch(tensor.eq(norm, 0.0), 1.0, norm) #如果norm为0,则将norm赋值为1norm = window / norm.dimshuffle(0, 1, 2, 3, 'x', 'x') #除以norm得到每个位置的概率# get probprob = rng.multinomial(pvals = norm.reshape((batch * channel * res_r * res_c, pr * pc)), dtype='float32') #multinomial()函数能够按照pvals产生多个多项式分布,元素值为0或1# selectres = (window * prob.reshape((batch, channel, res_r, res_c,  pr, pc))).max(axis=5).max(axis=4) #window和后面的矩阵相乘是点乘,即对应元素相乘,numpy矩阵符号res.name = 'pooled_' + namereturn tensor.cast(res, theano.config.floatX)def weighted_max_pool_bc01(bc01, pool_shape, pool_stride, image_shape, rng = None):"""This implements test time probability weighted pooling defined in:Stochastic Pooling for Regularization of Deep Convolutional Neural NetworksMatthew D. Zeiler, Rob Fergusbc01: minibatch in format (batch size, channels, rows, cols),IMPORTANT: All values should be poitiviepool_shape: shape of the pool region (rows, cols)pool_stride: strides between pooling regions (row stride, col stride)image_shape: avoid doing some of the arithmetic in theano"""r, c = image_shapepr, pc = pool_shapers, cs = pool_stridebatch = bc01.shape[0]channel = bc01.shape[1]if rng is None: rng = RandomStreams(2022) # Compute index in pooled space of last needed pool # (needed = each input pixel must appear in at least one pool)def last_pool(im_shp, p_shp, p_strd):rval = int(numpy.ceil(float(im_shp - p_shp) / p_strd))assert p_strd * rval + p_shp >= im_shpassert p_strd * (rval - 1) + p_shp < im_shpreturn rval# Compute starting row of the last poollast_pool_r = last_pool(image_shape[0] ,pool_shape[0], pool_stride[0]) * pool_stride[0]# Compute number of rows needed in image for all indexes to work outrequired_r = last_pool_r + prlast_pool_c = last_pool(image_shape[1] ,pool_shape[1], pool_stride[1]) * pool_stride[1]required_c = last_pool_c + pc# final result shaperes_r = int(numpy.floor(last_pool_r/rs)) + 1res_c = int(numpy.floor(last_pool_c/cs)) + 1for bc01v in get_debug_values(bc01):assert not numpy.any(numpy.isinf(bc01v))assert bc01v.shape[2] == image_shape[0]assert bc01v.shape[3] == image_shape[1]# paddingpadded = tensor.alloc(0.0, batch, channel, required_r, required_c)name = bc01.nameif name is None:name = 'anon_bc01'bc01 = tensor.set_subtensor(padded[:,:, 0:r, 0:c], bc01)bc01.name = 'zero_padded_' + name# unravelingwindow = tensor.alloc(0.0, batch, channel, res_r, res_c, pr, pc)window.name = 'unravlled_winodows_' + namefor row_within_pool in xrange(pool_shape[0]):row_stop = last_pool_r + row_within_pool + 1for col_within_pool in xrange(pool_shape[1]):col_stop = last_pool_c + col_within_pool + 1win_cell = bc01[:,:,row_within_pool:row_stop:rs, col_within_pool:col_stop:cs]window  =  tensor.set_subtensor(window[:,:,:,:, row_within_pool, col_within_pool], win_cell)# find the normnorm = window.sum(axis = [4, 5])norm = tensor.switch(tensor.eq(norm, 0.0), 1.0, norm)norm = window / norm.dimshuffle(0, 1, 2, 3, 'x', 'x')# averageres = (window * norm).sum(axis=[4,5]) #前面的代码几乎和前向传播代码一样,这里只需加权求和即可res.name = 'pooled_' + namereturn res.reshape((batch, channel, res_r, res_c))

小Tips:

概率是频率随样本趋于无穷的极限

期望是平均数随样本趋于无穷的极限

随机池化(Stochastic Pooling)相关推荐

  1. 池化层(pooling layer) 感受野(Receptive Field) 神经网络的基本组成

    目录 (1)本文涉及的函数 (2)池化层 (3)感受野 (4)代码示例(含注释) 承接上两篇博客:卷积层(空洞卷积对比普通卷积).激活函数层 (1)本文涉及的函数 import torch impor ...

  2. CNN基础知识——池化(pooling)

    概述 池化过程在一般卷积过程后.池化(pooling) 的本质,其实就是采样.Pooling 对于输入的 Feature Map,选择某种方式对其进行降维压缩,以加快运算速度. 两种主要池化方式 最大 ...

  3. 随机采样池化--S3Pool: Pooling with Stochastic Spatial Sampling

    S3Pool: Pooling with Stochastic Spatial Sampling CVPR2017 https://github.com/Shuangfei/s3pool 本文将常规池 ...

  4. Rethinking场景分析中的空间池化 | Strip Pooling(CVPR2020,何凯明)

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 原文链接:https://arxiv.org/abs/2003.13328v1 代码链接:https: ...

  5. 图像处理池化层pooling和卷积核

    1.池化层的作用 在卷积神经网络中,卷积层之间往往会加上一个池化层.池化层可以非常有效地缩小参数矩阵的尺寸,从而减少最后全连层中的参数数量.使用池化层即可以加快计算速度也有防止过拟合的作用. 2.为什 ...

  6. 池化(Pooling)

    基础概念 在图像处理中,由于图像中存在较多冗余信息,可用某一区域子块的统计信息(如最大值或均值等)来刻画该区域中所有像素点呈现的空间分布模式,以替代区域子块中所有像素点取值,这就是卷积神经网络中池化( ...

  7. CNN(卷积层convolutional layer,激励层activating layer,池化层pooling,全连接层fully connected)

    CNN产生的原因:当使用全连接的神经网络时,因为相邻两层之间的神经元都是有边相连的,当输入层的特征纬度非常高时(譬如图片),全连接网络需要被训练的参数就会非常多(参数太多,训练缓慢),CNN可以通过训 ...

  8. 池化层-Pooling(CNN卷积神经网络)

    文章目录 汇聚层(池化层) 最大汇聚层和平均汇聚层 填充和步幅 多个通道 小结 汇聚层(池化层) 本节将介绍汇聚(pooling)层(又名池化层),它具有双重目的: 1.降低卷积层对位置的敏感性 2. ...

  9. mysql pooling 作用_关于池化(pooling)理解!!!

    网上看到一个池化的解释是: 为了描述大的图像,可以对不同位置的特征进行聚合统计,如计算平均值或者是最大值,即mean-pooling和max-pooling 我的想法是,图像做卷积以后,将图像信息(特 ...

最新文章

  1. WPF的图片操作效果(一):RenderTransform
  2. Day13 目录结构自定义Yum仓库源码包编译安装(Service02)
  3. 黑马程序员:从零基础到精通的前端学习路线
  4. 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-9底层驱动之USART
  5. boost::fast_pool_allocator
  6. 安装与配置-以前的某个程序安装已在安装计算机上创建挂起的文件操作......
  7. POI--HSSFCellStyle类
  8. 安装nginx报错:the HTTP gzip module requires the zlib library
  9. [转]Handler学习笔记(一)
  10. 基础练习: 使用openssl命令创建RSA密钥
  11. 【初赛】「程序填空」题答v1.0
  12. 纠结的链接——ln、ln -s、fs.symlink、require
  13. VMware vCenter Server安装与配置
  14. 张俊芳电机学11章计算题答案
  15. 【软考】软件设计师知识点整理(待更新)
  16. 深度学习模型部署学习一
  17. 整理的一篇iOS面试题大全
  18. 【计算机网络】第一章:计算机网络概述
  19. IT公司年会:程序员专属小品《疯狂营救》,改编自沈腾《人生自古谁无死》
  20. linux如何设置Java环境变量呢?

热门文章

  1. vue2的动画,混入Mixin,插件,指令,渲染函数,响应式,MVVM
  2. redis当中各种数据类型的操作
  3. 在火狐浏览器上安装Tampermonkey和Greasy Fork和使用iciba划词翻译
  4. 学习笔记(14):C++编程FFMpeg(QT5+OpenCV)实战--实时美颜直播推流-打开rtmp推流输出并从文件读取音视频帧...
  5. 中国首台超级计算机“天河一号,中国首台千万亿次超级计算机天河一号安装完毕...
  6. 唤客猫获客营销之全员激励
  7. Tilemap瓦片地图
  8. 四大险种之间有什么区别?理赔会冲突吗?
  9. 大班运用计算机教学案例,【大班教学案例】_幼儿园大班教学案例:《让孩子在自主探索中获取成功》...
  10. 【Python】使用31条规则编写高质量且美丽的Python代码