Python ``tensorflow.math.zero_fraction()`` 零元素占比计算函数与实例


发布日期 : 2024-07-08 12:08:37 UTC

访问量: 10 次浏览

Python – tensorflow.math.zero\_fraction()

TensorFlow是谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

zero\_fraction()用于查找数值中零的部分。

语法:
tensorflow.math.softplus(value, name)

参数:

  • value: 它是一个数字类型的张量。
  • name(可选):它定义了该操作的名称。

返回:
它返回值中的零的分数,dtype 为float32。

示例 1:

# importing the library
import tensorflow as tf

# Initializing the input tensor
a = tf.constant([ 5, 0, 9, 15], dtype = tf.float64)

# Printing the input tensor
print('a: ', a)

# Calculating result
res = tf.math.zero_fraction(a)

# Printing the result
print('Result: ', res)

输出:

a: tf.Tensor([ 5. 0. 9. 15.], shape=(4, ), dtype=float64)
Result: tf.Tensor(0.25, shape=(), dtype=float32)

示例 2:

# importing the library
import tensorflow as tf

# Initializing the input tensor
a = tf.constant([ 5 + 3j, 0 + 0j, 9-6j, 0 + 0j], dtype = tf.complex128)

# Printing the input tensor
print('a: ', a)

# Calculating result
res = tf.math.zero_fraction(a)

# Printing the result
print('Result: ', res)

输出:

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)