2
$\begingroup$

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?

$\endgroup$
1
$\begingroup$

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:

hard-sigmoid

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
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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