Keras 3 API 文档 / 层 API / 池化层 / 全局平均池化二维层

全局平均池化二维层

[source]

GlobalAveragePooling2D class

keras.layers.GlobalAveragePooling2D(data_format=None, keepdims=False, **kwargs)

全局平均池化操作,用于2D数据.

参数: data_format: 字符串,可以是 "channels_last""channels_first". 输入数据中维度的顺序."channels_last" 对应输入形状为 (batch, height, width, channels), 而 "channels_first" 对应输入形状为 (batch, features, height, weight). 它默认为在 ~/.keras/keras.json 文件中找到的 image_data_format 值. 如果你从未设置过它,那么它将是 "channels_last". keepdims: 一个布尔值,是否保留时间维度. 如果 keepdimsFalse(默认),张量的秩会减少空间维度. 如果 keepdimsTrue,空间维度将以长度1保留. 其行为与 tf.reduce_meannp.mean 相同.

输入形状:

  • 如果 data_format='channels_last': 4D张量,形状为: (batch_size, height, width, channels)
  • 如果 data_format='channels_first': 4D张量,形状为: (batch_size, channels, height, width)

输出形状:

  • 如果 keepdims=False: 2D张量,形状为 (batch_size, channels).
  • 如果 keepdims=True: - 如果 data_format="channels_last": 4D张量,形状为 (batch_size, 1, 1, channels) - 如果 data_format="channels_first": 4D张量,形状为 (batch_size, channels, 1, 1)

示例:

>>> x = np.random.rand(2, 4, 5, 3)
>>> y = keras.layers.GlobalAveragePooling2D()(x)
>>> y.shape
(2, 3)