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 >= 0.1.

I printed out time stamp to timing each step of function i use:

.eval() -> 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("Data length {}".format(len(faces_data)))
    print("Compatition time {}".format(time.time()-start))
    start_max_index = time.time()
    max_index = tf.math.argmax(dot_array)
    print("get max_value_index time {}".format(time.time()-start_max_index))
    start_condition_time = time.time()
    new_max_index = tf.cond(dot_array[max_index] > tf.constant(0.1),
                            lambda: max_index,lambda: tf.constant(-1,dtype=tf.int64))
    print("tf.cond time {}".format(time.time()-start_condition_time))
    temp_max_index = -1
    start_seesion = time.time()
    with sess:
        print("Session time {}".format(time.time() - start_seesion))
        start_eval_time = time.time()
        temp_max_index = new_max_index.eval()
        print("Eval time {}".format(time.time() - start_eval_time))
    print("Total time {}, max_index {}".format(time.time()-start,temp_max_index))

And my outputs:

1.000.000 3.000


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() # <- done inside eval

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

| improve this answer | |
  • $\begingroup$ Thank you for your help! @Pedro Henrique Monforte. $\endgroup$ – Bui Thanh Lam Aug 4 at 6:50
  • $\begingroup$ If an answer solves your problem consider upvoting/accepting it. Also, on Stack Exchange sites it is not proper to right comments such as "thank you" or "that was helpful" since voting accomplishes that and adding it as a comment would simply fill the comment section with spam. $\endgroup$ – Pedro Henrique Monforte Aug 5 at 2:11

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