替换 tensorflow 张量中的 nan 值 [英] Replace nan values in tensorflow tensor

查看:91
本文介绍了替换 tensorflow 张量中的 nan 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 tensorflow 中研究卷积神经网络,但遇到了问题.问题是我通过 tfrecords 读取的输入图像包含一定数量的 nan 值.造成这种情况的原因是图像代表了一个深度图,其中包含一些无限值,在将其编码到 tfrecord 中,然后解码以将其馈送到网络的过程中,这些无限值变成了 nan 值.

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 values. The cause of this is the image represents a depthmap which has some infinite values in it, and in the process of encoding it in the tfrecord and then decoding to feed it to the net these infinite values become nan values.

现在,由于在我的情况下,在将原始图像编码到 tfrecors 之前替换其无限值不是一种选择,因此我可以通过任何方式替换图像张量中的 nan 值作为在我输入之前执行的操作到网上了吗?

Now, since in my situation replacing the infinite values in the original image before encoding it in the tfrecors is not an option, there is any way I can replace the nan values in my image tensor as an operation to do before I feed it to the net?

推荐答案

tf.where 和 tf.is_nan 的组合应该可以工作:

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())

打印(使用 TensorFlow 0.12.1):

Prints (using TensorFlow 0.12.1):

[ 0.  1.]

这篇关于替换 tensorflow 张量中的 nan 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆