'GlobalAveragePooling1D equivalence with Lambda Layer

Is the GlobalAveragePooling1D Layer the same like calculating the mean with a custom Lambda Layer?

The data is temporal, so x has shape (batch, time, features)

x=keras.layers.Lambda(lambda x: keras.backend.mean(x, axis=1))(x)

compared to

x=GlobalAveragePooling1D()(x)

Since my results differ drastically there seems something missing.

Any Ideas?



Solution 1:[1]

you can test it on your own...

X = np.random.uniform(0,1, (32,24,10)).astype('float32')

x_lambda = Lambda(lambda x: tf.keras.backend.mean(x, axis=1))(X)
x_pool = GlobalAveragePooling1D()(X)

tf.reduce_all(x_lambda == x_pool)
# <tf.Tensor: shape=(), dtype=bool, numpy=True>

They are the same

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marco Cerliani