MaxPooling2D
类tf_keras.layers.MaxPooling2D(
pool_size=(2, 2), strides=None, padding="valid", data_format=None, **kwargs
)
针对二维空间数据的最大池化操作。
通过对输入数据的每个通道,在由 pool_size
定义的输入窗口上取最大值,从而沿其空间维度(高度和宽度)对输入进行下采样。该窗口沿每个维度按 strides
进行移动。
当使用 "valid"
填充选项时,输出的空间形状(行数或列数)为:output_shape = math.floor((input_shape - pool_size) / strides) + 1
(当 input_shape >= pool_size
时)
当使用 "same"
填充选项时,输出形状为:output_shape = math.floor((input_shape - 1) / strides) + 1
例如,当 strides=(1, 1)
和 padding="valid"
时
>>> x = tf.constant([[1., 2., 3.],
... [4., 5., 6.],
... [7., 8., 9.]])
>>> x = tf.reshape(x, [1, 3, 3, 1])
>>> max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2),
... strides=(1, 1), padding='valid')
>>> max_pool_2d(x)
<tf.Tensor: shape=(1, 2, 2, 1), dtype=float32, numpy=
array([[[[5.],
[6.]],
[[8.],
[9.]]]], dtype=float32)>
例如,当 strides=(2, 2)
和 padding="valid"
时
>>> x = tf.constant([[1., 2., 3., 4.],
... [5., 6., 7., 8.],
... [9., 10., 11., 12.]])
>>> x = tf.reshape(x, [1, 3, 4, 1])
>>> max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2),
... strides=(2, 2), padding='valid')
>>> max_pool_2d(x)
<tf.Tensor: shape=(1, 1, 2, 1), dtype=float32, numpy=
array([[[[6.],
[8.]]]], dtype=float32)>
用法 # 示例
>>> input_image = tf.constant([[[[1.], [1.], [2.], [4.]],
... [[2.], [2.], [3.], [2.]],
... [[4.], [1.], [1.], [1.]],
... [[2.], [2.], [1.], [4.]]]])
>>> output = tf.constant([[[[1], [0]],
... [[0], [1]]]])
>>> model = tf.keras.models.Sequential()
>>> model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2),
... input_shape=(4, 4, 1)))
>>> model.compile('adam', 'mean_squared_error')
>>> model.predict(input_image, steps=1)
array([[[[2.],
[4.]],
[[4.],
[4.]]]], dtype=float32)
例如,当 stride=(1, 1) 和 padding="same" 时
>>> x = tf.constant([[1., 2., 3.],
... [4., 5., 6.],
... [7., 8., 9.]])
>>> x = tf.reshape(x, [1, 3, 3, 1])
>>> max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2),
... strides=(1, 1), padding='same')
>>> max_pool_2d(x)
<tf.Tensor: shape=(1, 3, 3, 1), dtype=float32, numpy=
array([[[[5.],
[6.],
[6.]],
[[8.],
[9.],
[9.]],
[[8.],
[9.],
[9.]]]], dtype=float32)>
参数
(2, 2)
将在 2x2 的池化窗口上取最大值。如果只指定一个整数,则两个维度都将使用相同的窗口长度。pool_size
。"valid"
或 "same"
之一(不区分大小写)。"valid"
表示无填充。"same"
会在输入的左/右或上/下方均匀填充,使得输出具有与输入相同的高度/宽度维度。channels_last
(默认)或 channels_first
。输入中维度的顺序。channels_last
对应于形状为 (batch, height, width, channels)
的输入,而 channels_first
对应于形状为 (batch, channels, height, width)
的输入。如果未指定,则使用您的 TF-Keras 配置文件(位于 ~/.keras/keras.json
)中找到的 image_data_format
值(如果存在),否则为 'channels_last'。默认为 'channels_last'。输入形状
data_format='channels_last'
: 形状为 (batch_size, rows, cols, channels)
的 4D 张量。data_format='channels_first'
: 形状为 (batch_size, channels, rows, cols)
的 4D 张量。输出形状
data_format='channels_last'
: 形状为 (batch_size, pooled_rows, pooled_cols, channels)
的 4D 张量。data_format='channels_first'
: 形状为 (batch_size, channels, pooled_rows, pooled_cols)
的 4D 张量。返回值
表示最大池化值的 4 阶张量。输出形状参见上文。