Replace nan values in tensorflow tensor

后端 未结 4 2110
说谎
说谎 2021-01-04 00:02

I\'m working on a convolutional neural network in tensorflow and I have a problem. The problem is the input image I read through tfrecords contains a certain number of nan v

相关标签:
4条回答
  • 2021-01-04 00:12

    Clip by value made NaN infinity and where was overkill for one variable. 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)
    
    0 讨论(0)
  • 2021-01-04 00:20

    If someone is looking for the solution in Tensorflow 2.0, the adapted code of Allen Lavoie is :

    import tensorflow as tf
    with tf.compat.v1.Session():
        has_nans = tf.constant([float('NaN'), 1.])
        print(tf.where(tf.math.is_nan(has_nans), tf.zeros_like(has_nans), has_nans).eval())
    
    0 讨论(0)
  • 2021-01-04 00:24

    A combination of tf.where and tf.is_nan should work:

    import tensorflow as tf
    with tf.Session():
        has_nans = tf.constant([float('NaN'), 1.])
        print(tf.where(tf.is_nan(has_nans), tf.zeros_like(has_nans), has_nans).eval())
    

    Prints (using TensorFlow 0.12.1):

    [ 0.  1.]
    
    0 讨论(0)
  • 2021-01-04 00:36

    A much easier approach, compatible with TF2.0, is to just use tf.clip_by_value, which mirrors np.clip and removes NaNs (see here):

    no_nans = tf.clip_by_value(has_nans, -1e12, 1e12)
    

    Some caveats: 1) this also removes infs 2) Depending on your application you may need to set the clip value to a high value to avoid losing info.

    0 讨论(0)
提交回复
热议问题