Tensorflow 是否为每个 eval() 调用重新运行? [英] Does Tensorflow rerun for each eval() call?

查看:23
本文介绍了Tensorflow 是否为每个 eval() 调用重新运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Tensorflow 和 Python 中,我通过在计算结束时观察多个变量来做以下事情来理解 Tensorflow 和调试代码.

In Tensorflow and Python, I am doing the following sort of thing to understand Tensorflow and debug code by observing multiple variables at the end of a computation.

with tf.Session():
    print "var1 ="
    print (var1.eval({x:myInputs, y:myOutputs}))
    print "var2 ="
    print (var2.eval({x:myInputs, y:myOutputs}))

Tensorflow 是否为每个 eval() 调用重新运行整个图形计算?仅仅为了打印出一个变量(张量)而重新运行整个图似乎效率低下.如果发生这种情况,有没有办法运行图形/过程/评估一次,然后打印出每个变量的值,而无需重新运行整个图形?

Does Tensorflow rerun the entire graph computation for each eval() call? Seems inefficient to rerun the entire graph just to print out one variable (tensor). If that is what is happening, is there a way to run the graph/process/eval once and then print out the values of each variable, without rerunning the entire graph?

推荐答案

当你调用 Tensor.eval(),TensorFlow (i) 计算出它需要运行的整个图的哪个子图来产生该张量的值,然后 (ii) 运行它整个图表.

When you call Tensor.eval(), TensorFlow (i) works out what subgraph of the whole graph it needs to run to produce the value of that tensor, then (ii) runs that entire graph.

使用 Session.run() 一次获取多个张量的值.例如,您可以按如下方式重写代码以运行该图一次:

It is often more efficient to use Session.run() to fetch the values of multiple tensors at once. For example, you could rewrite your code as follows to run the graph once:

with tf.Session() as sess:
    val1, val2 = sess.run([var1, var2], {x:myInputs, y:myOutputs})
    print "var1 =", val1
    print "var2 =", val2

这篇关于Tensorflow 是否为每个 eval() 调用重新运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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