张量流图执行顺序

use*_*322 1 tensorflow

考虑代码:

#tensorflow graph
input  = tf.some_place_holder
func1  = tf.some_function .....(input)  
func2  = tf.some_function .....(func1)  
....

#code 1 
res1, res2 = sess.run([ func1, func2 ],feed_dict_input)

#code 2 
res1 = sess.run([ func1 ],feed_dict_input)  
res2 = sess.run([ func2 ],feed_dict_input)  
Run Code Online (Sandbox Code Playgroud)

如果我运行代码2,func1会运行两次吗?即func1首先运行以获取res1并再次运行获取res2.

tensorflow是否足够智能以计算依赖关系func1和func2,以便将函数称为minium时间?

mrr*_*rry 5

为了具体起见,我假设func1,并func2在你的例子是tf.Tensor对象.

  • 在"代码1"中,值func1将被计算一次:相同的值将返回给用户并用于计算func2.

  • 在"代码2"中,值func1将被计算两次:每次调用一次sess.run().

TensorFlow不会在调用之间缓存中间张量值tf.Session.run().原因很简单:在典型的神经网络工作负载(训练或推理)中,大多数中间值在图形运行之间变得无效,因为它们是输入的函数(从一步到另一步变化)和当前状态(在训练期间发生变化).如果要保存值以供以后使用,则必须将其显式分配给a tf.Variable,或将其存储在某些其他有状态对象中,例如a tf.FIFOQueue.