Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Keras' MaxPooling1D and GlobalMaxPooling1D functions?

Both MaxPooling1D and GlobalMaxPooling1D are described as a max pooling operation for temporal data.

keras.layers.pooling.MaxPooling1D(pool_size=2, strides=None, padding='valid')

I understand that GlobalMaxPooling1D takes no input parameters. keras.layers.pooling.GlobalMaxPooling1D()

I would just like to visually understand how the two of them differ in the way they work?

like image 403
KayBay Avatar asked May 02 '17 00:05

KayBay


People also ask

What is MaxPooling1D keras?

MaxPooling1D classMax pooling operation for 1D temporal data. Downsamples the input representation by taking the maximum value over a spatial window of size pool_size . The window is shifted by strides .

What is the difference between global max pooling and Max pooling?

Global max pooling = ordinary max pooling layer with pool size equals to the size of the input (minus filter size + 1, to be precise). You can see that MaxPooling1D takes a pool_length argument, whereas GlobalMaxPooling1D does not.

What is GlobalMaxPooling1D?

The 1D Global max pooling block takes a 2-dimensional tensor tensor of size (input size) x (input channels) and computes the maximum of all the (input size) values for each of the (input channels).

What is the advantage of Max pooling layer?

Max pooling is done to in part to help over-fitting by providing an abstracted form of the representation. As well, it reduces the computational cost by reducing the number of parameters to learn and provides basic translation invariance to the internal representation.


1 Answers

Td;lr GlobalMaxPooling1D for temporal data takes the max vector over the steps dimension. So a tensor with shape [10, 4, 10] becomes a tensor with shape [10, 10] after global pooling. MaxPooling1D takes the max over the steps too but constrained to a pool_size for each stride. So a [10, 4, 10] tensor with pooling_size=2 and stride=1 is a [10, 3, 10] tensor after MaxPooling(pooling_size=2, stride=1)

Long answer with graphic aid

Lets say we have a simple sentence with 4 words and we have some vector encoding for the words (like word2vec embeddings). Of course you wont normally max pool over and embedding Tensor but this should do for an example. Also global pooling works across channels but I'll leave that out of this illustration. Finally, things get slightly more complicated with padding but we dont need that here either.

Suppose we have MaxPooling1D(pool_size=2, strides=1). Then

the  [[.7, -0.2, .1]   | pool size is two                   boy   [.8, -.3,  .2]   | so look at two words at a time    | stride=1 will will  [.2, -.1,  .4]     and take the max over those       | move the pool down live  [.4  -.4,  .8]]    2 vectors. Here we looking         1 word. Now we look                               'the' and 'boy'.                'boy' and 'will' and                                                              take the max. 

So that will result in a [1, 3, 3] Tensor with the each timestep being the max over a 2D pool. And since we had 3 pools we have effectively downsampled our timesteps from 4 to 3.

However, if we use GlobalMaxPooling1D we will just take the max vector of that sentence (Tensor) which is probably the vector representation of the word 'live'.

Indeed, here the how GlobalMaxPooling1D is defined in keras

class GlobalMaxPooling1D(_GlobalPooling1D):     """Global max pooling operation for temporal data.     # Input shape         3D tensor with shape: `(batch_size, steps, features)`.     # Output shape         2D tensor with shape:         `(batch_size, features)`     """      def call(self, inputs):         return K.max(inputs, axis=1) 

Hopefully that helps, please ask for me to clarify anything.

Additionally here is a example that you can play with:

import numpy as np from keras.models import Sequential from keras.layers import Dense, LSTM, GlobalMaxPooling1D, MaxPooling1D  D = np.random.rand(10, 6, 10)  model = Sequential() model.add(LSTM(16, input_shape=(6, 10), return_sequences=True)) model.add(MaxPooling1D(pool_size=2, strides=1)) model.add(LSTM(10)) model.add(Dense(1)) model.compile(loss='binary_crossentropy', optimizer='sgd')  # print the summary to see how the dimension change after the layers are  # applied  print(model.summary())  # try a model with GlobalMaxPooling1D now  model = Sequential() model.add(LSTM(16, input_shape=(6, 10), return_sequences=True)) model.add(GlobalMaxPooling1D()) model.add(Dense(1)) model.compile(loss='binary_crossentropy', optimizer='sgd')  print(model.summary()) 
like image 176
parsethis Avatar answered Sep 23 '22 10:09

parsethis