tensorflow如何处理np.nan?

时间:2018-03-13 12:44:20

标签: python-3.x tensorflow nan

请考虑以下代码,

x = tf.constant([[[1, np.nan, np.nan], [4, 3, -1]], [[10, np.nan, 3], [20,5,-7]], [[5, np.nan, 3], [np.nan,15,-17]]])
x_max = tf.reduce_max(x, reduction_indices=[0])
with tf.Session() as sess:
    print (np.shape(sess.run(x)))
    print (sess.run(x))
    print (sess.run(x_max))

输出如下:

(3, 2, 3)
[[[  1.  nan  nan]
  [  4.   3.  -1.]]

 [[ 10.  nan   3.]
  [ 20.   5.  -7.]]

 [[  5.  nan   3.]
  [ nan  15. -17.]]]
[[ 10. -inf   3.]
 [ 20.  15.  -1.]]

现在我的问题是tensorflow如何处理 np.nan ,如 numpy.nanmax 或类似的?

1 个答案:

答案 0 :(得分:0)

引用此link(赠送给Yaroslav Bulatov):

  

TensorFlow的不同部分对待它们的方式不同:
  *浮点计算(通常是?)传播它们   * Int转换将它们视为0   * Int计算失败,TensorFlow的Python部分经常会在“NaN”上引发错误,即尝试向直方图添加NaN摘要将失败   异常。

以下是一些浮点运算的示例:

a = tf.constant([1.0, np.nan])
b = tf.constant(np.nan)
r = tf.reduce_min(a)
m = a * b
with tf.Session() as sess:
    print(sess.run(r)) # prints 1.0
    print(sess.run(m)) # array([nan, nan], dtype=float32)