6

(edited w.r.t. @quirk's answer)

I was reading some tensorflow-code online and saw this statements:

threshold = tf.select(input > RLSA_THRESHOLD, positive, negative)

source: https://github.com/Raverss/tensorflow-RLSA-NMS/blob/master/source.py#L31

positive Is a tensor with just 1's, negative also of the same size with 0's and input is some heatmap(/tensor) of the same size (all of type tf.float32).

The code snippet seems reasonably advanced for me to assume that the authors would have just used tf.cast(input > RLSA_THRESHOLD, tf.float32) if there was no specific reason for the tf.select(...) expression. Especially since this would have eliminated the need for the variables positive and negative, and would save memory, as they are just expensively redundant ways of storing 0 and 1.

Is the aforementioned tf.select(...) expression equivalent to tf.cast(input > RLSA_THRESHOLD, tf.float32)? If not, why not?

Note: I usually use Keras, and am sorry if I'm touching something very trivial here.

1

2 Answers 2

3

Umm, RTD(Read the docs)!

tf.select selects elements from positive or negative tensors based on the boolness of the elements in the condition tensor.

tf.select(condition, t, e, name=None)
Selects elements from t or e, depending on condition.
The t, and e tensors must all have the same shape, and the output will also have that shape.

(from the official docs.)

So in your case:

threshold = tf.select(input > RLSA_THRESHOLD, positive, negative)

input > RLSA_THRESHOLD will be a tensor of bool or logical values (0 or 1 symbolically), which will help choose a value from either the positive vector or the negative vector.

For example, say you have a RLSA_THRESHOLD of 0.5 and your input vector is a 4-dimensional vector of real continuous values ranging from 0 to 1. Your positive and negative vectors are essentially [1, 1, 1, 1] and [0, 0, 0, 0], respectively. input is [0.8, 0.2, 0.5, 0.6].

threshold will be [1, 0, 0, 1].

NOTE: positive and negative could be any kind of tensor as long as the dimensions agree with the condition tensor. Had positive and negative been, say, [2, 4, 6, 8] and [1, 3, 5, 7] respectively, your threshold would have been [2, 3, 5, 8].


The code snippet seems reasonably advanced for me to assume that the authors would have just used input > RLSA_THRESHOLD if there was no specific reason for the tf.select.

There is a very good reason for that. input > RLSA_THRESHOLD would simply return a tensor of logical (boolean) values. Logical values do not mix well with numerical values. You cannot use them for any realistic numerical computation. Had the positive and/or negative tensors been real valued, you might have required your threshold tensor to also have real values, in case you planned to use them further along.


Is the tf.select equivalent to input > RLSA_THRESHOLD? If not, why not?

No they are not. One is a function, the other is a tensor.

I am going to give you the benefit of doubt and assume you meant to ask:

Is the threshold equivalent to input > RLSA_THRESHOLD? If not, why not?

No they are not. As explained above, input > RLSA_THRESHOLD is a logical tensor with a data type of bool. threshold, on the other hand, is a tensor with the same data type as positive and negative.

NOTE: You can always cast your logical tensors to numerical (or any other supported data type) tensors using any of the casting methods available in .

6
  • 1
    Thank you for explaining that functions and tensors differ, and for giving me the benefit of the doubt of already understanding that :) I admit that I could have been a bit clearer in my question. Since positive and negative are tensors with resp. 1's and 0's (as noted), the tf.select construct seems equivalent to >, except for (as you stated) the result type. Therefore I was wondering whether there is a reason to use tf.select instead of tf.cast. I will edit the question to explicitly state this.
    – Herbert
    Commented Jan 6, 2017 at 15:26
  • @Herbert I don't judge. ;)
    – Quirk
    Commented Jan 6, 2017 at 15:27
  • I still have to admit that I could have played around with Tensorflow longer, it's just that I have rarely used it and new things can feel scary. Reading the tf.select documentation suggested that tf.cast was a more efficient alternative for this use case, and this confused me, assuming the authors of the code knew what they were doing.
    – Herbert
    Commented Jan 6, 2017 at 15:36
  • The TF library mutates very rapidly. So many breaking API changes. There is a tiny chance that tf.cast was not around or did what it does now, when that example was written. Or maybe the author was not well versed with their own API. Sometimes simplicity trumps lucidity in code. Sometimes it is the other way round.
    – Quirk
    Commented Jan 6, 2017 at 16:01
  • 5
    As of Release 1.0 tf.select is gone, now use tf.where. github.com/tensorflow/tensorflow/blob/… Commented Apr 19, 2017 at 7:42
2

Best way you can understand it is by trying it out yourself:

In [86]: s = tf.InteractiveSession()

In [87]: inputs = tf.random_uniform([10], 0., 1.)

In [88]: positives = tf.ones([10])

In [89]: negatives = tf.zeros([10])    

In [90]: s.run([inputs, tf.select(inputs > .5, positives, negatives)])
Out[90]: 
[array([ 0.13187623,  0.77344072,  0.29853749,  0.29245567,  0.53489852,
         0.34861541,  0.15090156,  0.40595055,  0.34910154,  0.24349082], dtype=float32),
 array([ 0.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.], dtype=float32)]

For every value > 0.5 in tensor inputs you'll get a 1. at the same index, otherwise the value is 0..

The result of inputs > .5 is a tensor of booleans (True for values that meet the condition, False otherwise).

In [92]: s.run(inputs > .5)
Out[92]: array([ True, False,  True,  True,  True,  True,  True,  True, False,  True], dtype=bool)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.