Keras GlobalMaxPooling2D TypeError: ('Keyword argument not understood:', 'keepdims')

Keras GlobalMaxPooling2D TypeError: ('Keyword argument not understood:', 'keepdims')

我正在尝试实现一个 GlobalMaxPooling2D 层。我有一个 10x10x128 输入,并希望将其缩减为形状为 1x1x128 的 3D 张量。我尝试使用 keepdims=True,但它抛出

TypeError: ('Keyword argument not understood:', 'keepdims')

我也尝试添加 data_format 但无济于事(这是默认的“channel_last”)。 这是 GlobalMaxPooling2D

的代码
ug = layers.GlobalMaxPooling2D(data_format='channel_last',keepdims=True)(inputs)

输入变量是二维转换操作的输出:

conv4 = layers.Conv2D(filters=128, kernel_size=3, strides=1, padding='valid', activation='relu', name='conv4')(conv3)

我是因为这个 Conv 层还是在调用 GlobalMaxPooling2D 层时弄乱了某个地方? 有没有办法从 GlobalMaxPooling2D 层获得 1x1x128 的输出?

对于tf < 2.6,你可以做到

import tensorflow as tf; print(tf.__version__)

input_shape = (1, 10, 10, 128)
x = tf.random.normal(input_shape) 
y = tf.keras.layers.GlobalMaxPool2D()(x)
z = tf.keras.layers.Reshape((1, 1, input_shape[-1]))(y)

print(x.shape)
print(y.shape)
print(z.shape)
2.5.0
(1, 10, 10, 128)
(1, 128)
(1, 1, 1, 128)

tf > = 2.6 开始,您可以使用 keepdims 个参数。

!pip install tensorflow==2.6.0rc0 -q

import tensorflow as tf; print(tf.__version__)

input_shape = (1, 10, 10, 128)
x = tf.random.normal(input_shape) 
y = tf.keras.layers.GlobalMaxPool2D(keepdims=True)(x)

print(x.shape)
print(y.shape)

2.6.0-rc0
(1, 10, 10, 128)
(1, 1, 1, 128)