Tensorflow take ages for tf.cond and eval() - python code (sorry but i asked on Stackoverflow but none answer me) I got a problem with TensorFlow and need your help.
My need is calculating tensordot between a vector: 1x512 named face in my code and a faces data: N x 512 named input_faces_data. The code will return the max_value_index if the max_value &gt;= 0.1.
I printed out time stamp to timing each step of function i use:
tf.tensordot()
tf.math.argmax()
tf.cond()
tf.Session()
.eval() -&gt; return last value

My questions:

Why tf.tensordot() and tf.math.argmax() take just 1ms or 5ms with any length of faces data array (3.000 or 1.000.0000 - my examples) but time cost a lot with .eval and tf.cond()?
Why the duration of tf.cond() and .eval() is more longer with longer face data array?

I'm using TensorFlow 1.13.1 and my GPU is GTX 2080 (11GiB).
My Python code:
sess = tf.Session()
with tf.device(Config.GPU.GPU_DEVICE):
    start = time.time()
    dot_array = tf.tensordot(input_faces_data, face, axes=1)
    print(&quot;Data length {}&quot;.format(len(faces_data)))
    print(&quot;Compatition time {}&quot;.format(time.time()-start))
    start_max_index = time.time()
    max_index = tf.math.argmax(dot_array)
    print(&quot;get max_value_index time {}&quot;.format(time.time()-start_max_index))
    start_condition_time = time.time()
    new_max_index = tf.cond(dot_array[max_index] &gt; tf.constant(0.1),
                            lambda: max_index,lambda: tf.constant(-1,dtype=tf.int64))
    print(&quot;tf.cond time {}&quot;.format(time.time()-start_condition_time))
    temp_max_index = -1
    start_seesion = time.time()
    with sess:
        print(&quot;Session time {}&quot;.format(time.time() - start_seesion))
        start_eval_time = time.time()
        temp_max_index = new_max_index.eval()
        print(&quot;Eval time {}&quot;.format(time.time() - start_eval_time))
    print(&quot;Total time {}, max_index {}&quot;.format(time.time()-start,temp_max_index))

And my outputs:
The reason why tf.tensordot(), tf.math.argmax() doesn't change running regardless of the input size is because they don't make the operations, but rather declare it.
It is similar to passing a partial function using functools.partial, the function is not really executed, it just determines it's inputs (or part of it).
from functools import partial
def tensordot(a,b):
    return partial(lambda a,b: a*b,a,b)

While running eval will run the previously mounted model
dot = tensordot(a,b) #dot here is a function
dot() # &lt;- done inside eval

Actually, on tensorflow these &quot;partials&quot; are objects and when you eval one of then, the previously declared objects that it depends of will be evaluated