OH_
OH_

Reputation: 41

How to compare two tensors in tensorflow (not element wise but tensor wise)?

How to compare two tensors in tensorflow (not element wise but tensor wise), for example:

x = [[1, 2, 3, 4], [0, 0, 0, 0]] 
y = [0, 0, 0, 0]

expected:

not_equal(x, y) --> [True, False]

The tf.not_equal(x, y) returns:

[[True, True, True, True], [False, False, False, False]]

Upvotes: 2

Views: 1944

Answers (2)

Prabhakar Tayenjam
Prabhakar Tayenjam

Reputation: 149

In tensorflow 2, you can convert the tensors to numpy array first and compare them using np.array_equal.

t1 = t1.numpy()
t2 = t2.numpy()
res = np.array_equal(t1, t2)

Upvotes: 2

OH_
OH_

Reputation: 41

This is how I implemented it:

y = tf.constant([0, 0, 0, 0])
not_equal_t = tf.not_equal(y, x)
reduce_t = tf.reduce_all(not_equal_t, axis=2) --> This will return the [True, False] that I looked for

# The rest will return the values 
where_t = tf.where(reduce_t)
gather_t = tf.gather_nd(x, where_t)

Upvotes: 1

Related Questions