Keras全局最大池化2D TypeError: ('无法理解的关键字参数:', 'keepdims')

3

我正在尝试实现一个全局最大池化(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)

inputs变量是2D卷积操作的输出:

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

由于 Conv 层或调用 GlobalMaxPooling2D 层而导致我出了什么问题吗? 是否有一种方法可以从 GlobalMaxPooling2D 层获得 1x1x128 的输出?


2
我认为keepdimstf 2.6开始可用。 - Innat
使用 expand_dims 会产生我想要实现的相同效果吗? - Sukrit Kumar
1
检查给定的答案。让我知道。 - Innat
1个回答

5

对于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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接