小淼博客

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

池化层的作用如下-引用《TensorFlow实践》:

  池化层的作用是减少过拟合,并通过减小输入的尺寸提高性能。他们可以用来对输入进行降采样,但会为后续层保留重要的信息。只使用tf.nn.conv2d来减小输入的尺寸也是可以的,但是池化层的效率更高

常见的TensorFlow提供的激活函数如下:(详细请参考http://www.tensorfly.cn/tfdoc/api_docs/python/nn.html)

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

Performs the max pooling on the input.

  • value: A 4-D Tensor with shape [batch, height, width, channels] and type float32,float64qint8quint8qint32.
  • ksize: A list of ints that has length >= 4. The size of the window for each dimension of the input tensor.
  • strides: A list of ints that has length >= 4. The stride of the sliding window for each dimension of the input tensor.
  • padding: A string, either 'VALID' or 'SAME'. The padding algorithm.
  • name: Optional name for the operation.

当我们的ksize=[1 3 3 1]式,我们取3X3模板池pool当中最大的数据当做中心点的值,strides作为滑动跳跃的间隔,代码如下所示:

import tensorflow as tf

batch_size = 1
input_height = 3
input_width = 3
input_channels = 1
layer_input = tf.constant([
    [
        [[1.0],[0.2],[1.5]],
        [[0.1],[1.2],[1.4]],
        [[1.1],[0.4],[0.4]]
    ]
])
kernel = [batch_size, input_height, input_width,input_channels]
max_pool = tf.nn.max_pool(layer_input,kernel,[1,1,1,1],padding='VALID',name=None)
sess = tf.Session()
sess.run(max_pool)

输出结果如下(注意max_pool的输入的参数的维数一定要正确,否则会报错):

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

Performs the average pooling on the input.

Each entry in output is the mean of the corresponding size ksize window in value.

  • value: A 4-D Tensor of shape [batch, height, width, channels] and type float32,float64qint8quint8, or qint32.
  • ksize: A list of ints that has length >= 4. The size of the window for each dimension of the input tensor.
  • strides: A list of ints that has length >= 4. The stride of the sliding window for each dimension of the input tensor.
  • padding: A string, either 'VALID' or 'SAME'. The padding algorithm.
  • name: Optional name for the operation.

跳跃遍历一个张量,并将被卷积核覆盖的各深度值去平均。当整个卷积核都非常重要时,若需要实现值的缩减,平均池化非常有用,例如输入张量宽度和高度很大,但深度很小的情况。

import tensorflow as tf

batch_size = 1
input_height = 3
input_width = 3
input_channels = 1
layer_input = tf.constant([
    [
        [[1.0],[1.0],[1.0]],
        [[1.0],[0.5],[0.0]],
        [[0.0],[0.0],[0.0]]
    ]
])
kernel = [batch_size, input_height, input_width,input_channels]
avg_pool = tf.nn.mavg_pool(layer_input,kernel,[1,1,1,1],padding='VALID',name=None)
sess = tf.Session()
sess.run(avg_pool)

 输出结果如下:(1.0+1.0+1.0+0.5+0.0+0.0+0.0+0.0)/9=0.5

posted on 2017-08-09 18:15  小淼博客  阅读(1253)  评论(0)    收藏  举报

大家转载请注明出处!谢谢! 在这里要感谢GISPALAB实验室的各位老师和学长学姐的帮助!谢谢~