tf.nn.max_pool
Args:
value: A 4-D
Tensor
with shape[batch, height, width, channels]
and typetf.float32
.
value是一个4D的tensor,是我们对其进行maxpooling的对象,它的shape是[batch, height, width, channels]
ksize: A list of ints that has length >= 4. The size of the window for each dimension of the input tensor.
ksize表示对于输入的tensor的每一维的池化尺度,比如在height那一维是2的话,表示在height轴会以2作为池化尺度。因为往往不会对batch和channel作池化,所以值一般为1。即ksize
=[1, kheight, kwidth, 1]
。
strides: A list of ints that has length >= 4. The stride of the sliding window for each dimension of the input tensor.
strides表示步长, 意同conv2d里面的strides,每一维度都表示窗口的在对应维度上的滑动步长。
padding: A string, either
'VALID'
or'SAME'
. The padding algorithm. See the @{tf.nn.convolution$comment here}
padding表示pooling的模式,意同conv2d里面的strides。SAME
模式就是将滑动窗口与矩阵进行左对齐,然后向右滑动。一直滑到与矩阵最右边那一列不相交为止。
data_format: A string. ‘NHWC’ and ‘NCHW’ are supported.
数据格式,这个一般都是默认的,不需要改变,具体是什么作用暂时不清楚。
name: Optional name for the operation.
op名字
Returns: A
Tensor
with typetf.float32
. The max pooled output tensor.
举例:
import tensorflow as tf
import numpy as np
data = np.random.randint(1, 9, size=[1, 5, 5, 1])
print(np.transpose(data, (0, 3, 1, 2)))
input_ = tf.convert_to_tensor(data, dtype=tf.float32)
res = tf.nn.max_pool(input_, ksize=[1, 2, 2, 1], strides=[1, 1, 1, 1],padding='SAME')
with tf.Session() as sess:
tf.global_variables_initializer().run()
output = sess.run(res, feed_dict={input_: data})
output = np.array(output)
print(np.transpose(output, (0, 3, 1, 2)))