Check if NaN in Tensorflow
Asked Answered
V

6

10

I would like to check a tensorflow variable and set it to zero if it is NaN.

How can I do this? The following trick seems not to work:

if tf.is_nan(v) is True:
    v = 0.0
Ventilation answered 25/8, 2017 at 2:17 Comment(4)
Are you certain that is_nan() returns a boolean?Peirsen
It returns a tensor of type booleanVentilation
What is the shape of v? is v a scalar?Nigh
It is the cost of optimization. So, yes it is a tensor containing a scalar number.Ventilation
N
8

If v is a 0d tensor, you might use tf.where to test and update the value:

import numpy as np

v = tf.constant(np.nan)                  # initialize a variable as nan  ​
v = tf.where(tf.is_nan(v), 0., v)
​
with tf.Session() as sess:    
    print(sess.run(v))

# 0.0
Nigh answered 25/8, 2017 at 2:45 Comment(0)
F
3

For Tensorflow 2.0

you can you:

import tensorflow as tf

if tf.math.is_nan(v):
    print("v is NaN")

or with numpy

import numpy as np

if np.is_nan(v):
    print("v is NaN")
Famished answered 5/6, 2020 at 10:38 Comment(1)
Very nice they've added this. This is so much cleaner than the original, but I guess it still is nice to know how to use tf.where and tf.is_nan if you need to do this kind of check on a vector (or batch) instead of on just a scalar.Circuity
P
0

You could use tf.is_nan in combination with tf.cond to change values if the tensorflow value is NAN.

Prosthodontist answered 25/8, 2017 at 2:24 Comment(0)
P
0

Libraries like numpy (in this case, tensorflow) often have their own boolean implementations, comparing the memory addresses of a custom boolean type, and CPython's built in using is is going to result in erratic behaviour.

Either just check implicit boolean-ness -> if tf.is_nan(v) or do a equality comparison if tf.is_nan(v) == True.

Peirsen answered 25/8, 2017 at 2:31 Comment(0)
H
0

To make everything a tf operation, I used this to convert a single value to 0 if it's NaN:

value_not_nan = tf.dtypes.cast(tf.math.logical_not(tf.math.is_nan(value)), dtype=tf.float32)
tf.math.multiply_no_nan(value, value_not_nan)
Hindgut answered 27/8, 2020 at 20:40 Comment(0)
S
-4

I hope this can help you. math.is_nan

import math
if math.isnan(float(v)):
    v = 0.0
Scavenge answered 25/8, 2017 at 2:37 Comment(1)
Not a tensorflow operaton, unfortunately useless answerGroundsheet

© 2022 - 2024 — McMap. All rights reserved.