简体   繁体   中英

Converting all nan values to zero in tensforflow

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

Change:

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

To:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM