Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

python - Replace nan values in tensorflow tensor

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Prints (using TensorFlow 0.12.1):

[ 0.  1.]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...