Search code examples
pythonkeraskeras-layerkeras-2

Keras 2: Using lambda function in "Merge" layers


I am trying to implement this merge layer:

policy = merge([out1, out2], mode = lambda x: x[0]-K.mean(x[0])+x[1], output_shape = (out_node,))

However, "merge" is no longer present in Keras 2. You can only access public standarized "Merge" layers, such as Add, Multiply, Dot.

How can I implement this function in Keras 2? I thought about making two merge layers but I have no idea how to implement that, especially because of the "K.mean" part.

For reference, here are the imports:

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

Solution

  • You can simply do this using a Lambda layer:

    from keras import backend as K
    from keras.layers import Lambda
    
    policy = Lambda(lambda x: x[0] - K.mean(x[0]) + x[1])([out1, out2])