Gilfoyle
Gilfoyle

Reputation: 3616

Tensorflow Replace values in tensor which are close to zero

I want to avoid numerical instabilities in my code which contains many divisions. How can I replace small values in a tensor by a predefined numerical stabilizer?

Let the stabilizer be EPS=1e-9. T is a tensor which contains many small values which are close to zero. I want to replace all values within the interval [-EPS,EPS] with EPS. How can I do that?

Upvotes: 2

Views: 823

Answers (1)

javidcf
javidcf

Reputation: 59701

You can just do something like:

 my_tensor_stable = tf.where(tf.abs(my_tensor) < EPS,
                             EPS * tf.ones_like(my_tensor),
                             my_tensor)

Upvotes: 4

Related Questions