Python ``tensorflow.math.equal()`` 逐元素相等比较函数与实例


发布日期 : 2024-09-11 17:25:38 UTC

访问量: 10 次浏览

Python – tensorflow.math.equal()

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

equal()是用来进行元素的等价比较。它在应用比较之前执行参数广播。

语法:
tensorflow.math.equal( x, y, name)

参数:

  • x:它可以是一个张量或稀疏张量或索引片。
  • y:它可以是张量或稀疏张量或索引片。
  • name(可选):它定义了该操作的名称。

返回:它返回一个布尔张量。

例子1:在这个例子中进行了广播。

# importing the library
import tensorflow as tf

# Initializing the input tensor
a = tf.constant([6, 8, 12, 2], dtype = tf.float64)
b = (2)

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

# Performing equality comparison
res = tf.math.equal(x = a, y = b)

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

输出:

a: tf.Tensor([ 6. 8. 12. 2.], shape=(4, ), dtype=float64)
b: 2
Result: tf.Tensor([False False False True], shape=(4, ), dtype=bool)

示例 2:

# importing the library
import tensorflow as tf

# Initializing the input tensor
a = tf.constant([6, 8, 12, 4], dtype = tf.float64)
b = tf.constant([2, 8, 12, 7],  dtype = tf.float64)

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

# Performing equality comparison
res = tf.math.equal(x = a, y = b)

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

输出:

a: tf.Tensor([ 6. 8. 12. 4.], shape=(4, ), dtype=float64)
b: tf.Tensor([ 2. 8. 12. 7.], shape=(4, ), dtype=float64)
Result: tf.Tensor([False True True False], shape=(4, ), dtype=bool)