【问题标题】:how tensorflow deals with np.nan?tensorflow 如何处理 np.nan?
【发布时间】:2018-08-21 17:34:29
【问题描述】:

请考虑以下代码,

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 或类似的?

【问题讨论】:

标签: python-3.x tensorflow nan


【解决方案1】:

引用此link(感谢Yaroslav Bulatov):

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

这里是一些浮点操作的例子:

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)

【讨论】:

  • 所以在浮点计算中,e.f.使用 tf.reduce_min 它将传播!像numpy.nanmax?如果我理解正确?
  • 其实tf.reduce_min 不会传播它们。请参阅我编辑的答案。
  • 什么是m = a B v!当你再次打印 r 时,它会打印 1.0 这意味着它会忽略 np.nan!
  • 对不起,m = a*b
猜你喜欢
  • 2022-06-29
  • 2022-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多