美文网首页
tf.nn.max_pool()

tf.nn.max_pool()

作者: 我是谁的小超人 | 来源:发表于2017-10-19 11:08 被阅读0次

概述

max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似

说明

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'
  • use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true
  • name:指定该操作的name

返回

返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式

实例

示例源码:
假设有这样一张图,双通道
通道1



通道2



用程序去做最大值池化:
import tensorflow as tf

a = tf.constant([[
            [[1., 17.],
             [2., 18.], 
             [3., 19.],
             [4., 20.]],
            [[5., 21.],
             [6., 22.],
             [7., 23.],
             [8., 24.]],
            [[9., 25.],
             [10., 26.],
             [11., 27.],
             [12., 28.]],
            [[13., 29.],
             [14., 30.],
             [15., 31.],
             [16., 32.]]
        ]])
pooling = tf.nn.max_pool(a, [1, 2, 2, 1], [1, 1, 1, 1], padding='VALID')
with tf.Session() as sess:
    print('image: ')
    print(sess.run(a))
    print('\n')
    print('result: ')
    print(sess.run(pooling))

image:
[[[[ 1. 17.]
[ 2. 18.]
[ 3. 19.]
[ 4. 20.]]

[[ 5. 21.]
[ 6. 22.]
[ 7. 23.]
[ 8. 24.]]

[[ 9. 25.]
[ 10. 26.]
[ 11. 27.]
[ 12. 28.]]

[[ 13. 29.]
[ 14. 30.]
[ 15. 31.]
[ 16. 32.]]]]

result:
[[[[ 6. 22.]
[ 7. 23.]
[ 8. 24.]]

[[ 10. 26.]
[ 11. 27.]
[ 12. 28.]]

[[ 14. 30.]
[ 15. 31.]
[ 16. 32.]]]]

通道1

通道2


可以改变步长

pooling = tf.nn.max_pool(a, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')

输出结果:
result:
[[[[ 6. 22.]
[ 8. 24.]]

[[ 14. 30.]
[ 16. 32.]]]]

通道1

通道2


相关文章

网友评论

      本文标题:tf.nn.max_pool()

      本文链接:https://www.haomeiwen.com/subject/qqzyuxtx.html