How to implement L2-norm pooling in Keras?

keras, pooling

Solution

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)

Problem

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?

Original source