How can I clip the values returned by a layer in Keras?
Asked Answered
T

2

7

How can I clip the values returned by the Lambda layer?

I tried using this:

from keras.backend.tensorflow_backend import clip
from keras.layers.core import Lambda

...
model.add(Dense(1))
model.add(Activation('linear'))
model.add(Lambda(lambda x: clip(x, min_value=200, max_value=1000)))

But it does not matter where I put my Lambda+clip, it does not affect anything?

Testis answered 29/3, 2017 at 16:32 Comment(1)
Could you print out model.summary()? And - by clip doesn't affecting anything you mean that values are not clipped, yes?Philomena
T
5

It actually has to be implemented as loss, at the model.compile step.

from keras import backend as K

def clipped_mse(y_true, y_pred):
    return K.mean(K.square(K.clip(y_pred, 0., 1900.) - K.clip(y_true, 0., 1900.)), axis=-1)

model.compile(loss=clipped_mse)

EDIT: Actually, now in hindsight I think that this might not be the right approach. This actually means we do not add penalty for going over too high of a values - it's in a way the opposite of what we want.

Testis answered 29/3, 2017 at 19:23 Comment(0)
B
2

Try this solution:

tf.clip_by_value(yur_values, clip_value_min=0, clip_value_max=1)
Blackman answered 6/12, 2022 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.