tensorflow 中的 eval() 和 run() [英] eval() and run() in tensorflow

查看:59
本文介绍了tensorflow 中的 eval() 和 run()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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}))

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

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.

在某些情况下,我看到过

And in some cases, I have seen like

sess.run(variable, feed_dict)

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

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

谢谢!!

推荐答案

如果你只有一个默认会话,它们基本上是一样的.

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

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

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

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

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

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

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

张量和操作的区别:

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

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

注意:Tensor 类将来会被 Output 取代.目前这两个是彼此的别名.

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

这篇关于tensorflow 中的 eval() 和 run()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆