Python tensorflow 中的 eval() 和 run()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38987466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:44:26  来源:igfitidea点击:

eval() and run() in tensorflow

pythonpython-2.7tensorflow

提问by Ramesh-X

I'm referring to Deep MNIST for Experts tutorialgiven by the tensorflow. I have a problem in Train and Evaluatepart of that tutorial. There they have given a sample code as follows.

我指的是tensorflow给出的Deep MNIST for Experts 教程。我在该教程的训练和评估部分遇到了问题。他们在那里给出了一个示例代码,如下所示。

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv),reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.initialize_all_variables())
for i in range(20000):
  batch = mnist.train.next_batch(50)
  if i%100 == 0:
    train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
    print("step %d, training accuracy %g"%(i, train_accuracy))
  train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

print("test accuracy %g"%accuracy.eval(feed_dict={x: mnist.test.images,
                       y_: mnist.test.labels, keep_prob: 1.0}))

So in these code segment they have used accuracy.eval()at one time. And other time train_step.run(). As I know of both of them are tensor variables.

所以在这些代码段中他们曾经使用accuracy.eval()过一次。和其他时间train_step.run()。据我所知,它们都是张量变量。

And in some cases, I have seen like

在某些情况下,我看到像

sess.run(variable, feed_dict)

So my question is what are the differences between these 3 implementations. And how can I know what to use when..?

所以我的问题是这 3 个实现之间有什么区别。我怎么知道什么时候用..?

Thank You!!

谢谢你!!

回答by fwalch

If you have only one default session, they are basically the same.

如果您只有一个默认会话,则它们基本相同。

From https://github.com/tensorflow/tensorflow/blob/v1.12.0/tensorflow/python/framework/ops.py#L2351:

https://github.com/tensorflow/tensorflow/blob/v1.12.0/tensorflow/python/framework/ops.py#L2351

op.run() is a shortcut for calling tf.get_default_session().run(op)

op.run() 是调用 tf.get_default_session().run(op) 的快捷方式

From https://github.com/tensorflow/tensorflow/blob/v1.12.0/tensorflow/python/framework/ops.py#L691:

https://github.com/tensorflow/tensorflow/blob/v1.12.0/tensorflow/python/framework/ops.py#L691

t.eval() is a shortcut for calling tf.get_default_session().run(t)

t.eval() 是调用 tf.get_default_session().run(t) 的快捷方式

Difference between Tensor and Operation:

张量和运算的区别:

Tensor: https://www.tensorflow.org/api_docs/python/tf/Tensor

张量:https: //www.tensorflow.org/api_docs/python/tf/Tensor

Operation: https://www.tensorflow.org/api_docs/python/tf/Operation

操作:https: //www.tensorflow.org/api_docs/python/tf/Operation

Note: the Tensor class will be replaced by Output in the future. Currently these two are aliases for each other.

注意:Tensor 类将来会被 Output 替换。目前这两个是彼此的别名。

回答by idnavid

The difference is in Operations vs. Tensors. Operations use run() and Tensors use eval().

区别在于操作与张量。操作使用 run(),张量使用 eval()。

There seems to be a reference to this question in TensorFlow FAQ: https://www.tensorflow.org/programmers_guide/faq#running_a_tensorflow_computation

TensorFlow FAQ 中似乎有对这个问题的引用:https://www.tensorflow.org/programmers_guide/faq#running_a_tensorflow_computation

The section addresses the following question: What is the difference between Session.run() and Tensor.eval()?

该部分解决了以下问题:Session.run() 和 Tensor.eval() 之间有什么区别?