空间数据的平均池化操作。
用法
tf.keras.layers.AveragePooling2D(
pool_size=(2, 2), strides=None, padding='valid', data_format=None,
**kwargs
)参数
-
pool_size整数或 2 个整数的元组,缩小比例的因子(垂直,水平)。(2, 2)将两个空间维度的输入减半。如果只指定一个整数,则两个维度将使用相同的窗口长度。 -
strides整数,2 个整数的元组,或无。跨步值。如果没有,它将默认为pool_size。 -
padding"valid"或"same"之一(不区分大小写)。"valid"表示没有填充。"same"导致在输入的左/右或上/下均匀填充,以使输出具有与输入相同的高度/宽度尺寸。 -
data_format一个字符串,是channels_last(默认)或channels_first之一。输入中维度的排序。channels_last对应于形状为(batch, height, width, channels)的输入,而channels_first对应于形状为(batch, channels, height, width)的输入。它默认为您的 Keras 配置文件中的image_data_format值~/.keras/keras.json。如果您从未设置它,那么它将是"channels_last"。
通过对输入的每个通道在输入窗口(大小由 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])
avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2),
strides=(1, 1), padding='valid')
avg_pool_2d(x)
<tf.Tensor:shape=(1, 2, 2, 1), dtype=float32, numpy=
array([[[[3.],
[4.]],
[[6.],
[7.]]]], dtype=float32)>
例如,对于 stride=(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])
avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2),
strides=(2, 2), padding='valid')
avg_pool_2d(x)
<tf.Tensor:shape=(1, 1, 2, 1), dtype=float32, numpy=
array([[[[3.5],
[5.5]]]], dtype=float32)>
例如,对于 strides=(1, 1) 和 padding="same" :
x = tf.constant([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
x = tf.reshape(x, [1, 3, 3, 1])
avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2),
strides=(1, 1), padding='same')
avg_pool_2d(x)
<tf.Tensor:shape=(1, 3, 3, 1), dtype=float32, numpy=
array([[[[3.],
[4.],
[4.5]],
[[6.],
[7.],
[7.5]],
[[7.5],
[8.5],
[9.]]]], dtype=float32)>
输入形状:
- 如果
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 张量。
相关用法
- Python tf.keras.layers.AveragePooling3D用法及代码示例
- Python tf.keras.layers.AveragePooling1D用法及代码示例
- Python tf.keras.layers.Average用法及代码示例
- Python tf.keras.layers.Activation用法及代码示例
- Python tf.keras.layers.Attention用法及代码示例
- Python tf.keras.layers.Add用法及代码示例
- Python tf.keras.layers.AbstractRNNCell用法及代码示例
- Python tf.keras.layers.AdditiveAttention用法及代码示例
- Python tf.keras.layers.InputLayer用法及代码示例
- Python tf.keras.layers.serialize用法及代码示例
- Python tf.keras.layers.Dropout用法及代码示例
- Python tf.keras.layers.maximum用法及代码示例
- Python tf.keras.layers.LayerNormalization用法及代码示例
- Python tf.keras.layers.Conv2D用法及代码示例
- Python tf.keras.layers.RepeatVector用法及代码示例
- Python tf.keras.layers.Multiply用法及代码示例
- Python tf.keras.layers.Conv1D用法及代码示例
- Python tf.keras.layers.experimental.preprocessing.PreprocessingLayer.adapt用法及代码示例
- Python tf.keras.layers.CategoryEncoding用法及代码示例
- Python tf.keras.layers.subtract用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.keras.layers.AveragePooling2D。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
