Skip to content Skip to sidebar Skip to footer

How To Conditionally Scale Values In Keras Lambda Layer?

The input tensor rnn_pv is of shape (?, 48, 1). I want to scale every element in this tensor, so I try to use Lambda layer as below: rnn_pv_scale = Lambda(lambda x: 1 if x >=100

Solution 1:

You can't use Python control flow statements such as if-else statements to perform conditional operations in the definition of a model. Instead you need to use methods defined in Keras backends. Since you are using TensorFlow as the backend you can use tf.where() to achieve that:

import tensorflow as tf

scaled = Lambda(lambda x: tf.where(x >= 1000, tf.ones_like(x), x/1000.))(input_tensor)

Alternatively, to support all the backends, you can create a mask to do this:

from keras import backend as K

defrescale(x):
    mask = K.cast(x >= 1000., dtype=K.floatx())
    return mask + (x/1000.0) * (1-mask)

#...
scaled = Lambda(rescale)(input_tensor)

Update: An alternative way to support all the backends is to use K.switch method:

from keras import backend as K

scaled = Lambda(lambda x: K.switch(x >= 1000., K.ones_like(x), x / 1000.))(input_tensor)

Post a Comment for "How To Conditionally Scale Values In Keras Lambda Layer?"