简体   繁体   English

Tensorflow中sess.run(c)和c.eval()之间的区别

[英]The difference between sess.run(c) and c.eval() in Tensorflow

When printing out the value of the node " c " in the following example, to me, it seems that there's no difference between print sess.run(c) and print c.eval() . 在以下示例中,当打印出节点“ c ”的值时,在我看来, print sess.run(c)print c.eval()之间没有区别。 Can I assume that sess.run(c) and c.eval() are equivalent? 我可以假设sess.run(c)c.eval()是等效的吗? Or are there any differences? 还是有区别?

import tensorflow as tf

a = tf.Variable(2.0, name="a")
b = tf.Variable(3.0, name="b")
c = tf.add(a, b, name="add")
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print sess.run(c)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print c.eval()

When you call c.eval() on a tensor, you are basically calling tf.get_default_session().run(c) . 当在张量上调用c.eval()时, 基本上是在调用tf.get_default_session().run(c) It is a convenient shortcut. 这是一个方便的快捷方式。

However, Session.run() is much more general. 但是, Session.run()更通用。

  1. It allows you to query several outputs at once: sess.run([a, b, ...]) . 它允许您一次查询多个输出: sess.run([a, b, ...]) When those outputs are related and depend on a state that may change, it is important to get them simultaneously to have a consistent result. 当这些输出相关并且依赖于可能改变的状态时,重要的是要同时获得它们以获得一致的结果。 People are regularly surprised by this [1] , [2] . 人们经常对此感到惊讶[1][2]
  2. Session.run() can take a few parameters that Tensor.eval() does not have, such as RunOptions , that can be useful for debugging or profiling. Session.run()可以使用Tensor.eval()没有的一些参数,例如RunOptions ,这些参数对于调试或分析很有用。
    • Note however that eval() can take a feed_dict . 但是请注意, eval() 可以采用feed_dict
  3. eval() is a property of Tensor s. eval()Tensor的属性。 But Operation s such as global_variables_initializer() on the other hand do not have an eval() but a run() (another convenient shortcut). Operation S,从而为global_variables_initializer()另一方面不具有eval()run()另一个方便的快捷键)。 Session.run() can run both. Session.run()可以同时运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM