Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare tensor inside tensorflow?

My ultimate goal is to judge placeholder value.

Now I can judge a placeholder by using the regular python comparison expressions. Then, you know, it returns a tensor.

temp_tensor = a_placeholder > 0

Then for example , in nn_ops.py

temp1 = constant_op.constant(True)
temp2 = constant_op.constant(False)

how to compare temp1 and temp2? Or whether temp1 and temp2 are equal.

like image 541
DunkOnly Avatar asked Dec 13 '16 09:12

DunkOnly


People also ask

How do you compare tensor values?

We can compare two tensors by using the torch. eq() method. This method compares the corresponding elements of tensors. It has to return rue at each location where both tensors have equal value else it will return false.

How do you evaluate a tensor in TensorFlow?

The easiest[A] way to evaluate the actual value of a Tensor object is to pass it to the Session. run() method, or call Tensor. eval() when you have a default session (i.e. in a with tf. Session(): block, or see below).

How do you know if two tensors are equal in TensorFlow?

To check if two tensors are equal, one can use tf. equal .

How do you add two tensors in TensorFlow?

add() Function. The tf. add() function returns the addition of two tf. Tensor objects element wise.


2 Answers

Considering that tf.equal(temp1, temp2) returns tensor (e.g. [[True], [False]]) it is usefulless if you want to find an answer "is this tensor equal to another tensor", and you don't want compare elements. What you might want is

if sess.run(tf.reduce_all(tf.equal(temp1, temp2))):
    print('temp1 is equal temp2') 
else:
    print('temp1 is not equal temp2') 
like image 133
Ivan Borshchov Avatar answered Oct 22 '22 18:10

Ivan Borshchov


You should use the tf.equal function. Following the official docs, tf.equal() accepts two tensors and does the operation element wise. Something like this should work,

result = tf.equal(temp1, temp2)

Note, result will have the same dimension as temp1 and temp2 and filled with boolean values.

like image 30
martianwars Avatar answered Oct 22 '22 17:10

martianwars