Calling sess.run(c)
and c.eval()
, in the same session, provide exactly the same results.
You can mix calls to sess.run
and <tensor>.eval()
in the code, but it makes your code less consistent.
In my opinion, it's better to use always sess.run
, beacuse within a single call you can evaluate more then one tensor.
Instead, if you use <tensor>.eval()
you can evaluate only the specified <tensor>
.
You can see the performance differences running this script:
import time
import tensorflow as tf
a=tf.constant(5.0)
b=tf.constant(6.0)
c=a*b
start = time.time()
with tf.Session() as sess:
sess.run([c]*100)
end_run = time.time() - start
start = time.time()
with tf.Session() as sess:
for _ in range(100):
c.eval()
end_eval = time.time() - start
print('Run: {}\nEval: {}'.format(end_run, end_eval))
Running it with Tensorflow r0.11, on CPU (Intel i3) it gives this result:
Run: 0.009401798248291016
Eval: 0.021142005920410156
As you can see, the execution of a sess.run
call with a list of 100 elements is much fast then the execution of 100 c.eval()
calls.
In fact, Tensorflow evaluates only once the c
tensor in the sess.run
call, and reuse the result to do other computation if needed.