4

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?

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
Animeta
  • 1,241
  • 3
  • 16
  • 30
  • 2
    Does this answer your question? [What is the difference between MaxPool and MaxPooling layers in Keras?](https://stackoverflow.com/questions/63006575/what-is-the-difference-between-maxpool-and-maxpooling-layers-in-keras) – Amnon Tal Jul 31 '20 at 18:16

1 Answers1

0

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]])>
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • I understand that the key difference is 'Max' and 'Average', but I am curious about the syntax of function name, 'Pooling' and 'Pool'. I realized `tf.keras.layers.GlobalMaxPooling2D` has 4 alias: `tf.compat.v1.keras.layers.GlobalMaxPool2D`, `tf.compat.v1.keras.layers.GlobalMaxPooling2D`, `tf.compat.v2.keras.layers.GlobalMaxPool2D`, `tf.compat.v2.keras.layers.GlobalMaxPooling2D`, kind of redundant and inconsistent. – Animeta Aug 01 '20 at 04:00
  • `v1.compat` is a way to use Tensorflow 1.X code via Tensorflow 2.X. So you don't have to change your code, only your import statements and then you can run old Tensorflow code in the newer Tensorflow versions. If you're using Keras the changes will be minimal, if any, so it explains why all of these are seem identical (except for the changes under the hood). Source: https://www.tensorflow.org/guide/migrate. your other question was answered already so I didn't mention it. – Nicolas Gervais Aug 01 '20 at 13:22