TensorFlow池化层-函数

池化层的作用如下-引用《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

转载于:https://www.cnblogs.com/uestc-mm/p/7326924.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值