I need to implement an activation function that is similar to Keras's "hard-sigmoid", only for different limit values:

  • 0 if x < 0
  • 1 if x > 1
  • x if 0 <= x <= 1

How do I implement it with a tensorflow backend Keras?


1 Answer 1


Based on this post, hard-sigmoid in Keras is implemented as max(0, min(1, x*0.2 + 0.5)). To obtain the graph you like you have to tweak the shift and slope parameters, i.e. leave them out in your case:

$$ max(0, min(1, x)) $$

This will generate following graph:


For Keras' TensorFlow backend you can find the implementation here. This would be the corresponding changed "hard-sigmoid", for your case:

zero = _to_tensor(0., x.dtype.base_dtype)
one = _to_tensor(1., x.dtype.base_dtype)
x = tf.clip_by_value(x, zero, one)
return x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.