Check if NaN in Tensorflow
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
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")