Tensorflow difference between *pool2D and *pooling2D

Viewed 800

I am learning this TensorFlow-2.x-Tutorials where it use layers.MaxPooling2D. The autocompletion also hint layers.MaxPool2D, so I search for the difference between them.

Refer to this api_docs, I find their entire name tf.compat.v1.layers.MaxPooling2D and tf.keras.layers.MaxPool2D, which have almost same arguments, can I just consider layers.MaxPooling2D = layers.MaxPool2D, but the former is to tf1.x, the latter is to tf2.x?

What's more, I also find tf.keras.layers.GlobalMaxPool1D(Global max pooling operation for 1D temporal data) and tf.keras.layers.GlobalAveragePooling1D(Global average pooling operation for temporal data), these two have exact the same arguments, why is the syntax of function name different?

1 Answers

I'm only going to answer your second question because someone found a duplicate for your first one.

MaxPooling2D takes the maximum value from a 2D array. Take for example this input:

import tensorflow as tf

x = tf.random.uniform(minval=0, maxval=10, dtype=tf.int32, shape=(3, 3, 3), seed=42)
<tf.Tensor: shape=(3, 3, 3), dtype=int32, numpy=
array([[[2, 4, 3],
        [9, 1, 8],
        [8, 3, 5]],
       [[6, 6, 9],
        [9, 6, 1],
        [7, 5, 2]],
       [[2, 0, 8],
        [1, 6, 1],
        [2, 3, 9]]])>

MaxPooling2D will take the average value of all of these three elements:

gmp = tf.keras.layers.GlobalMaxPooling2D()

gmp(x[..., None])
<tf.Tensor: shape=(3, 1), dtype=int32, numpy=
array([[9],
       [9],
       [9]])>

There's a 9 in every elements so the operation returns a 9 for all three. For GlobalAveragePooling2D, it's the exact same thing but with averaging.

gap = tf.keras.layers.GlobalAveragePooling2D()

gap(x[..., None])
<tf.Tensor: shape=(3, 1), dtype=int32, numpy=
array([[3],
       [6],
       [5]])>
Related