TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks.
zero_fraction() is used to find fraction of zeros in value.
Syntax: tensorflow.math.zero_fraction(value, name)
Parameters:
- value: It’s a tensor of numeric type.
- name(optional): It defines the name for the operation.
Returns: It returns fraction of zeros in value, with dtype float32.
Example 1:
Python3
# importing the libraryimport tensorflow as tf# Initializing the input tensora = tf.constant([ 5, 0, 9, 15], dtype = tf.float64)# Printing the input tensorprint('a: ', a)# Calculating resultres = tf.math.zero_fraction(a)# Printing the resultprint('Result: ', res) |
Output:
a: tf.Tensor([ 5. 0. 9. 15.], shape=(4, ), dtype=float64) Result: tf.Tensor(0.25, shape=(), dtype=float32)
Example 2:
Python3
# importing the libraryimport tensorflow as tf# Initializing the input tensora = tf.constant([ 5 + 3j, 0 + 0j, 9-6j, 0 + 0j], dtype = tf.complex128)# Printing the input tensorprint('a: ', a)# Calculating resultres = tf.math.zero_fraction(a)# Printing the resultprint('Result: ', res) |
Output:
a: tf.Tensor([5.+3.j 0.+0.j 9.-6.j 0.+0.j], shape=(4, ), dtype=complex128) Result: tf.Tensor(0.5, shape=(), dtype=float32)
