Can we compare a scalar with a tensor in tensorflow? In this tutorial, we will discuss this topic.
The answer is yes. We can compare a scalar with a tensor in tensorflow, we can get a tensor with boolean type.
We will use an example to show you the reason.
Create a tensor
import tensorflow as tf import numpy as np seq = tf.Variable(np.array([1,2,3,4]), dtype = np.float32, name = 'seq')
Here seq is a tensorflow tensor.
Compare a scalar with a tensor
cond = 2>seq
where 2 is a scalar.
with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(cond))
Then we will find cond is:
[ True False False False]
The shape of cond is the same to seq.
If the shape of seq is (2, 2), how about cond?
seq = tf.reshape(seq, [2, 2]) cond = 2>seq
Then cond will be:
[[ True False] [False False]]
The shape of cond is also the same to seq.