Sukrit Kumar
Sukrit Kumar

Reputation: 396

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

I'm trying to implement a layer GlobalMaxPooling2D layer. I have a 10x10x128 input and want it reduced to a 3D tensor of shape 1x1x128. I tried using keepdims=True, but it throws a

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

I have tried adding data_format too but with no avail (which is the default "channel_last"). Here is the code for GlobalMaxPooling2D

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

The inputs variable is the output of a 2D conv operation:

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

Am i messing up somewhere due to this Conv layer or while calling GlobalMaxPooling2D layer ? Is there a way to get a 1x1x128 output from the GlobalMaxPooling2D layer ?

Upvotes: 3

Views: 8351

Answers (1)

Innat
Innat

Reputation: 17239

For tf < 2.6, you can do

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)

And from tf > = 2.6, you can use keepdims arguments.

!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)

Upvotes: 5

Related Questions