Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

875
Vistas
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?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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.]
over 4 years ago · Santiago Trujillo Denunciar

0

If someone is looking for the solution in Tensorflow 2.0, the adapted code of Allen Lavoie is :

import tensorflow as tf
with tf.compat.v1.Session():
    has_nans = tf.constant([float('NaN'), 1.])
    print(tf.where(tf.math.is_nan(has_nans), tf.zeros_like(has_nans), has_nans).eval())
over 4 years ago · Santiago Trujillo Denunciar

0

A much easier approach, compatible with TF2.0, is to just use tf.clip_by_value, which mirrors np.clip and removes NaNs (see here):

no_nans = tf.clip_by_value(has_nans, -1e12, 1e12)

Some caveats: 1) this also removes infs 2) Depending on your application you may need to set the clip value to a high value to avoid losing info.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda