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
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
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")
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 You could use tf.is_nan in combination with tf.cond to change values if the tensorflow value is NAN.
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
.
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)
I hope this can help you. math.is_nan
import math
if math.isnan(float(v)):
v = 0.0
© 2022 - 2024 — McMap. All rights reserved.
v
? isv
a scalar? – Nigh