Python 评估和运行都需要使用吗?
我从这一点上理解了两者之间的区别。 但在大多数在线谈话/代码中,我发现人们都使用以下两种方式:Python 评估和运行都需要使用吗?,python,tensorflow,Python,Tensorflow,我从这一点上理解了两者之间的区别。 但在大多数在线谈话/代码中,我发现人们都使用以下两种方式: import tensorflow as tf a=tf.constant(5.0) b=tf.constant(6.0) c=a*b with tf.Session() as sess: print(sess.run(c)) print(c.eval()) 我不理解这样做的必要性和实用性。在同一个会话中调用sess.run(c)和c.eval(),提供完全相同的结果 您可以在代码中
import tensorflow as tf
a=tf.constant(5.0)
b=tf.constant(6.0)
c=a*b
with tf.Session() as sess:
print(sess.run(c))
print(c.eval())
我不理解这样做的必要性和实用性。在同一个会话中调用sess.run(c)
和c.eval()
,提供完全相同的结果
您可以在代码中混合调用sess.run
和.eval()
,但这会降低代码的一致性
在我看来,最好总是使用sess.run
,因为在一次调用中,您可以计算多个张量
相反,如果使用.eval()
,则只能计算指定的
您可以看到运行此脚本时的性能差异:
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))
在CPU(英特尔i3)上使用Tensorflow r0.11运行它,会得到以下结果:
Run: 0.009401798248291016
Eval: 0.021142005920410156
如您所见,使用100个元素的列表执行sess.run
调用要比执行100个c.eval()
调用快得多
事实上,Tensorflow只对sess.run
调用中的c
张量求值一次,并在需要时重用结果进行其他计算。在同一会话中调用sess.run(c)
和c.eval()
,提供完全相同的结果
您可以在代码中混合调用sess.run
和.eval()
,但这会降低代码的一致性
在我看来,最好总是使用sess.run
,因为在一次调用中,您可以计算多个张量
相反,如果使用.eval()
,则只能计算指定的
您可以看到运行此脚本时的性能差异:
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))
在CPU(英特尔i3)上使用Tensorflow r0.11运行它,会得到以下结果:
Run: 0.009401798248291016
Eval: 0.021142005920410156
如您所见,使用100个元素的列表执行sess.run
调用要比执行100个c.eval()
调用快得多
事实上,Tensorflow只对
sess.run
调用中的c
张量进行一次计算,并在需要时重新使用结果进行其他计算。您的问题的快速答案是否
您只需要Session.run()
或Tensor.eval()
来计算Tensor对象的值。人们通常使用Tensor.eval()
来试验编程模型,即打印出张量值来跟踪流程!比如说,
import tensorflow as tf
a = tf.constant([1, 2], name='a')
b = tf.constant([3, 4], name='b')
c = tf.constant([1, 3], name='c')
d = tf.add_n([a, b])
e = tf.pow(d, c)
printout_d = tf.Print(d, [a, b, d], 'Input for d and the result: ')
printout_e = tf.Print(e, [d, c, e], 'Input for e and the result: ')
with tf.Session() as sess:
sess.run([d, e])
printout_d.eval()
printout_e.eval()
在上面的代码中,您只需
sess.run([d,e])
即可执行图形。但是,在某些情况下,例如调试,您可以受益于printout\u d.eval()
和printout\u e.eval()
您的问题的快速答案是否
您只需要Session.run()
或Tensor.eval()
来计算Tensor对象的值。人们通常使用Tensor.eval()
来试验编程模型,即打印出张量值来跟踪流程!比如说,
import tensorflow as tf
a = tf.constant([1, 2], name='a')
b = tf.constant([3, 4], name='b')
c = tf.constant([1, 3], name='c')
d = tf.add_n([a, b])
e = tf.pow(d, c)
printout_d = tf.Print(d, [a, b, d], 'Input for d and the result: ')
printout_e = tf.Print(e, [d, c, e], 'Input for e and the result: ')
with tf.Session() as sess:
sess.run([d, e])
printout_d.eval()
printout_e.eval()
在上面的代码中,您只需sess.run([d,e])
即可执行图形。但是,在某些情况下,例如调试,您可以受益于printout\u d.eval()
和printout\u e.eval()