tf.equal()

equal(
    x,
    y,
    name=None
)

对输入的 x 和 y 两个 Tensor 逐元素(element-wise)做 (x == y) 逻辑比较,返回 bool 类型的 Tensor

参数

  • x 只支持以下类型:halffloat32float64uint8int8int16int32int64complex64quint8qint8qint32stringboolcomplex128
  • y 的类型必须与 x 相同
  • name 给这个操作取一个名称,可选

返回

  • bool 类型的 Tensor

特性

  • 支持 broadcasting,详见 Numpy 文档

示例

基本用法:x 和 y 拥有相同的 shape

1
2
3
4
5
6
import tensorflow as tf
 
a = tf.constant([1, 2], tf.int32)
b = tf.constant([2, 2], tf.int32)
with tf.Session() as sess:
    print(sess.run(tf.equal(a, b))) # 输出 [False  True]

broadcasting 用法:x 和 y 不同 shape

1
2
3
4
5
x = tf.constant(["hehe", "haha", "hoho", "kaka"], tf.string)
y = tf.constant("hoho", tf.string)
with tf.Session() as sess:
    print(sess.run(tf.equal(x, y)))
# 输出 [False False  True False]

注意观察上面这个栗子,实际解决了在一个数组中查找某个元素索引(index)的问题,这个特性配合 tf.cast 在生成 one-hot 向量时将会特别有用。

1
2
3
4
5
6
7
a = tf.constant([[1], [2]], tf.int32)
b = tf.constant([[2, 1]], tf.int32)
with tf.Session() as sess:
    print(sess.run(tf.equal(a, b)))
# 输出
# [[False  True]
#  [ True False]]

tf.equal(A, B)是对比这两个矩阵或者向量的相等的元素,如果是相等的那就返回True,反正返回False,返回的值的矩阵维度和A是一样的

import tensorflow as tf
import numpy as np

A = [[1,3,4,5,6]]
B = [[1,3,4,3,2]]

with tf.Session() as sess:
print(sess.run(tf.equal(A, B)))

输出:
[[ True  True  True False False]]
---------------------
作者:UESTC_C2_403
来源:CSDN
原文:https://blog.csdn.net/uestc_c2_403/article/details/72232924
版权声明:本文为博主原创文章,转载请附上博文链接!

posted on 2018-12-19 10:43  AI大道理  阅读(1868)  评论(0编辑  收藏  举报

导航