代码之家  ›  专栏  ›  技术社区  ›  Anshuman Sinha

将tensforflow中的所有nan值转换为零

  •  0
  • Anshuman Sinha  · 技术社区  · 2 年前

    我正在尝试转换所有 nan 最终结果中的值为零。我无法正确执行它!

    代码如下:也可在colab上获得: link

    import tensorflow as tf
    import numpy as np
    
    ts = tf.constant([[0,0]]) 
    tx = tf.constant([[0,1]]) 
    
    out = ts / (ts + fx)
    
    out.numpy() # array([nan,  0.])
    
    tf.math.is_nan(out).numpy() # array([ True, False]
    
    out.numpy()[(tf.math.is_nan(out).numpy())] = 0
    out.numpy() #array([nan,  0.])
    

    out.numpy() 应该给 array([0., 0.])

    1 回复  |  直到 2 年前
        1
  •  3
  •   Djinn    2 年前

    更改:

    out.numpy()[(tf.math.is_nan(out).numpy())] = 0
    

    收件人:

    out = tf.where(tf.math.is_nan(out), 0., out)