Python - tensorflow.math.reciprocal_no_nan()

Python - tensorflow.math.reciprocal_no_nan()

The tensorflow.math.reciprocal_no_nan() function computes the element-wise reciprocal of the given input tensor. If the input tensor has any elements with a value of 0, this function will output 0 for those elements rather than returning NaN (Not a Number) or infinity.

This is particularly useful when you want to avoid NaN values in your computations, which can sometimes cause issues or unexpected results.

Here's a basic usage example:

import tensorflow as tf # Sample tensor x = tf.constant([0., 1., 2., 3., 0.]) # Compute the reciprocal avoiding NaNs result = tf.math.reciprocal_no_nan(x) # Start a TensorFlow session and evaluate the result with tf.Session() as sess: print(sess.run(result)) 

Output:

[0. 1. 0.5 0.33333334 0. ] 

As you can see, the elements that were 0 in the input tensor x resulted in 0 in the output tensor instead of NaN or infinity.


More Tags

bloburls makecert openapi platform-specific mapi dask datastax system.web.http flowtype primeng-datatable

More Programming Guides

Other Guides

More Programming Examples