Skip to content Skip to sidebar Skip to footer

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 =

Solution 1:

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') 

Solution 2:

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.

Solution 3:

Try

tf.cond(tf.equal(temp1, temp2), true_fn, false_fn)

Where true_fn and false_fn are functions. For instance you could write something like

tf.cond(tf.equal(temp1, temp2)
       , lambda: print(temp1, ' and ', temp2 , 'are equal.')
       , lambda: print(temp1, ' and ', temp2, 'are NOT equal.'))

Post a Comment for "How To Compare Tensor Inside Tensorflow?"