Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of tf.select

(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.

like image 726
Herbert Avatar asked Jan 06 '17 12:01

Herbert


People also ask

What is TF select?

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

What is the use of TF function?

You can use tf. function to make graphs out of your programs. It is a transformation tool that creates Python-independent dataflow graphs out of your Python code. This will help you create performant and portable models, and it is required to use SavedModel .

What is TF Ones_like?

Creates a tensor of all ones that has the same shape as the input.

What is TF slice?

This operation extracts a slice of size size from a tensor input_ starting at the location specified by begin . The slice size is represented as a tensor shape, where size[i] is the number of elements of the 'i'th dimension of input_ that you want to slice.


1 Answers

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 tensorflow.

like image 117
Quirk Avatar answered Sep 23 '22 11:09

Quirk