将所有NAN值转换为TensForflow中的零值

发布于 2025-02-11 13:05:38 字数 441 浏览 2 评论 0原文

我正在尝试将所有nan值转换为零。我无法正确执行它!

以下是代码:也可在COLAB上使用:

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。])

I am trying to convert all nan values to zero in my final results. I am not able to execute it properly!

The following is the code: also availvable on 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() should give array([0., 0.])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2025-02-18 13:05:38

更改:

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

到:

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

Change:

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

To:

out = tf.where(tf.math.is_nan(out), 0., out)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文