原创文章,转载请注明: 转载自慢慢的回味
本文链接地址: Tensorflow Pooling池化原理
池化(Pooling):也称为欠采样或下采样。主要用于特征降维,在保持旋转、平移、伸缩等不变性的前提下,压缩数据和参数的数量,减小过拟合,同时提高模型的容错性。常用的有按均值池化(mean-pooling):更大地保留图像背景信息,按最大值池化(max-pooling):更多的保留纹理信息。
池化原理
Tensorflow的池化(pooling.py)API主要包括1D,2D和3D的平均池化和最大池化。
平均池化通过API math_ops.reduce_mean 完成,最大池化通过API math_ops.reduce_max完成。
测试程序
以下面程序为例(这儿是GlobalMaxPooling2D):
from __future__ import absolute_import, division, print_function, unicode_literals import tensorflow as tf from tensorflow.python.ops import math_ops import numpy as np a = tf.Variable(np.arange(48.0).reshape(4,4,3)) print (a) print ("########## math_ops.reduce_mean(input_tensor=a, axis=[0], keepdims=True); #########") print(math_ops.reduce_mean(input_tensor=a, axis=[0], keepdims=True)) print ("########## math_ops.reduce_max(input_tensor=a, axis=[0], keepdims=True); #########") print(math_ops.reduce_max(input_tensor=a, axis=[0], keepdims=True)) print ("########## math_ops.reduce_mean(input_tensor=a, axis=[1], keepdims=True); #########") print(math_ops.reduce_mean(input_tensor=a, axis=[1], keepdims=True)) print ("########## math_ops.reduce_max(input_tensor=a, axis=[1], keepdims=True); #########") print(math_ops.reduce_max(input_tensor=a, axis=[1], keepdims=True)) print ("########## math_ops.reduce_mean(input_tensor=a, axis=[2], keepdims=True); #########") print(math_ops.reduce_mean(input_tensor=a, axis=[2], keepdims=True)) print ("########## math_ops.reduce_max(input_tensor=a, axis=[2], keepdims=True); #########") print(math_ops.reduce_max(input_tensor=a, axis=[2], keepdims=True)) print ("########## math_ops.reduce_mean(input_tensor=a, axis=[0,1], keepdims=True); #########") print(math_ops.reduce_mean(input_tensor=a, axis=[0,1], keepdims=True)) print ("########## math_ops.reduce_max(input_tensor=a, axis=[0,1], keepdims=True); #########") print(math_ops.reduce_max(input_tensor=a, axis=[0,1], keepdims=True)) print ("########## math_ops.reduce_mean(input_tensor=a, axis=[0,1,2], keepdims=True); #########") print(math_ops.reduce_mean(input_tensor=a, axis=[0,1,2], keepdims=True)) print ("########## math_ops.reduce_max(input_tensor=a, axis=[0,1,2], keepdims=True); #########") print(math_ops.reduce_max(input_tensor=a, axis=[0,1,2], keepdims=True)) |
代码输出:
<tf.Variable 'Variable:0' shape=(4, 4, 3) dtype=float64, numpy= array([[[ 0., 1., 2.], [ 3., 4., 5.], [ 6., 7., 8.], [ 9., 10., 11.]], [[12., 13., 14.], [15., 16., 17.], [18., 19., 20.], [21., 22., 23.]], [[24., 25., 26.], [27., 28., 29.], [30., 31., 32.], [33., 34., 35.]], [[36., 37., 38.], [39., 40., 41.], [42., 43., 44.], [45., 46., 47.]]])> ########## math_ops.reduce_mean(input_tensor=a, axis=[0], keepdims=True); ######### tf.Tensor( [[[18. 19. 20.] [21. 22. 23.] [24. 25. 26.] [27. 28. 29.]]], shape=(1, 4, 3), dtype=float64) ########## math_ops.reduce_max(input_tensor=a, axis=[0], keepdims=True); ######### tf.Tensor( [[[36. 37. 38.] [39. 40. 41.] [42. 43. 44.] [45. 46. 47.]]], shape=(1, 4, 3), dtype=float64) ########## math_ops.reduce_mean(input_tensor=a, axis=[1], keepdims=True); ######### tf.Tensor( [[[ 4.5 5.5 6.5]] [[16.5 17.5 18.5]] [[28.5 29.5 30.5]] [[40.5 41.5 42.5]]], shape=(4, 1, 3), dtype=float64) ########## math_ops.reduce_max(input_tensor=a, axis=[1], keepdims=True); ######### tf.Tensor( [[[ 9. 10. 11.]] [[21. 22. 23.]] [[33. 34. 35.]] [[45. 46. 47.]]], shape=(4, 1, 3), dtype=float64) ########## math_ops.reduce_mean(input_tensor=a, axis=[2], keepdims=True); ######### tf.Tensor( [[[ 1.] [ 4.] [ 7.] [10.]] [[13.] [16.] [19.] [22.]] [[25.] [28.] [31.] [34.]] [[37.] [40.] [43.] [46.]]], shape=(4, 4, 1), dtype=float64) ########## math_ops.reduce_max(input_tensor=a, axis=[2], keepdims=True); ######### tf.Tensor( [[[ 2.] [ 5.] [ 8.] [11.]] [[14.] [17.] [20.] [23.]] [[26.] [29.] [32.] [35.]] [[38.] [41.] [44.] [47.]]], shape=(4, 4, 1), dtype=float64) ########## math_ops.reduce_mean(input_tensor=a, axis=[0,1], keepdims=True); ######### tf.Tensor([[[22.5 23.5 24.5]]], shape=(1, 1, 3), dtype=float64) ########## math_ops.reduce_max(input_tensor=a, axis=[0,1], keepdims=True); ######### tf.Tensor([[[45. 46. 47.]]], shape=(1, 1, 3), dtype=float64) ########## math_ops.reduce_mean(input_tensor=a, axis=[0,1,2], keepdims=True); ######### tf.Tensor([[[23.5]]], shape=(1, 1, 1), dtype=float64) ########## math_ops.reduce_max(input_tensor=a, axis=[0,1,2], keepdims=True); ######### tf.Tensor([[[47.]]], shape=(1, 1, 1), dtype=float64) |
输入为4X4X3矩阵:
如果按照第0轴,取均值池化,则把第1轴,第2轴坐标相同的数据取均值输出结果,得到1X4X3的矩阵;
(0+12+24+36)/4=18
[[[18. 19. 20.] [21. 22. 23.] [24. 25. 26.] [27. 28. 29.]]] |
如果按照第0轴,第1轴,取均值池化,则在上面的结果集上,把第1轴坐标相同的数据再取均值输出结果,得到1X1X3的矩阵;
(18+21+24+27)/4=22.5
[[[22.5 23.5 24.5]]] |
如果按照第0轴,第1轴,第2轴,取均值池化,则在上面的结果集上,把第2轴坐标相同的数据再取均值输出结果,得到1X1X1的矩阵。
(22.5+23.5+24.5)/3=23.5
[[[23.5]]] |
本作品采用知识共享署名 4.0 国际许可协议进行许可。