4

I would like to add a global temporal pooling layer to my CNN that has three different pooling functions: mean, maximum, and L2-norm. Keras has mean and maximum pooling functions but I haven't been able to find one for L2. How could I implement this myself?

KayBay
  • 719
  • 1
  • 7
  • 10

2 Answers2

5

I was also looking for this, there's no such pool out of the box in keras. But you can implement it with the Lambda Layer

from keras.layers import Lambda
import keras.backend as K

def l2_norm(x):
    x = x ** 2
    x = K.sum(x, axis=1)
    x = K.sqrt(x)
    return x
global_l2 = Lambda(lambda x: l2_norm(x))(previous_layer)
Diego Aguado
  • 1,604
  • 18
  • 36
0

I've been searching for this and there are little information on how to calculate the pooling. If someone are interested in implementing it, see the following comparison:

Max Pooling: get the max value from the NxM cell being assessed and put this as the value of the downsampled matrix for this cell position.

Avg Pooling: Similar to above, instead of the max value, get the average of all value within the cell

L2-Norm Pooling: Similar to above, instead of average, calculate the L2-Norm of the numbers within a cell and then use this number in the target downsampled matrix. Let's suppose we get a cell (kernel size) that is 2x2, which are four numbers. Now suppose these four numbers are 1,2,3,4. The L2-Norm would be sqrt(sum(12+ 22 + 32 + 42). This is the number that goes into targeting cell.

Carlos Barcellos
  • 544
  • 1
  • 4
  • 10