Session.run()如何选择运行哪个子图 [英] How Session.run() chooses which sub-graph to run

查看:1554
本文介绍了Session.run()如何选择运行哪个子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解Session.run()如何在Tensorflow流程中工作。我知道Session.run()运行我们给它的fetch参数指定的子图。由于根据子图的哪一部分首先执行,我们可能会得到不同的结果,我试图看看是否确实如此。假设我们比较这段代码的输出:

  import tensorflow as tf 

x = tf.Variable 42)
assign1 = tf.assign(x,13)
assign2 = tf.assign(x,14)

with tf.Session()as sess:$ b $
val1,val2,val3 = sess.run([x,assign1,assign2])
print(val1,val2,val3)

以下代码:

  import tensorflow as tf 

x = tf.Variable(42)
assign2 = tf.assign(x,14)
assign1 = tf.assign(x,13)

with tf.Session()as sess:
sess.run(tf.global_variables_initializer())
val1,val2,val3 = sess.run([x,assign1,assign2 ])
print(val1,val2,val3)

(唯一的变化是分配第二个代码中第一个是14)。我期待看到第一个代码产生了14,14,14的输出,而第二个产生了13,13,13。然而,结果是第一个代码产生了第一个代码产生了13,13,13,而第二次产生了14,14,14。

为什么会发生这种情况?



< hr>

更新:继选择的答案:

我不太清楚什么是独立的,什么不是。例如,在以下代码中:

  x = tf.Variable([1,2,3,4,5]) 
$ b def foo():
tmp_list = []
assign = tf.assign(x [4],100)
for i in range(0,5 ):
tmp_list.append(x [i])
return tmp_list
$ bz = foo()

with tf.Session()as sess:
sess.run(tf.global_variables_initializer())
val1,val2 = sess.run([z,assign])
print(val1,val2)

在我的Session.run()中,我有z和assign。为了得到z,我需要foo()。当foo()运行时, assign = tf.assign(x [4],100)也会运行。然而,输出是 [1,2,3,4,5] 13 ,我不知道13从哪里来。

解决方案

有哪些独立操作是首先执行的,所以你可以在两种情况下得到13或14。你的结果是纯粹的运气。试试这个:



with tf.Session()as sess :
sess.run(tf.global_variables_initializer())
在范围内(100):
val1,val2,val3 = sess.run([x,assign1,assign2])
print(val1,val2,val3)

...你会看到13 14打印出来。






后续问题更新
$ b

z = foo()(它只是 x slices)不依赖于 assign 。请注意, foo 在会话开始前只调用一次。我认为,最好的办法是在张量板上可视化图形。



如果您说...

sess.run(z)

... tensorflow不会运行 assign ,因为它不运行python函数,它会计算张量。张量 x x [i] 不依赖于op assign



依赖性由张量执行的操作定义。所以,如果你有......

  y = x + 2 

...这个依赖项:为了评估 y ,tensorflow必须评估 x 。希望这可以让我们现在更清楚。


I was trying to understand how Session.run() works in Tensorflow flow. I know that Session.run() runs the sub-graph specified by the "fetch" argument we give it. Since depending on which part of the sub-graph is executed first we might get different results, I was trying to see if that is really the case. Suppose we compare the output of this code:

import tensorflow as tf

x = tf.Variable(42)
assign1 = tf.assign(x, 13)
assign2 = tf.assign(x, 14)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    val1, val2, val3 = sess.run([x, assign1, assign2])
    print(val1, val2, val3)

with this code:

import tensorflow as tf

x = tf.Variable(42)
assign2 = tf.assign(x, 14)
assign1 = tf.assign(x, 13)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    val1, val2, val3 = sess.run([x, assign1, assign2])
    print(val1, val2, val3)

(The only change is assigning of 14 comes first in the second code). I was expecting to see that the first code produces output 14, 14, 14 while the second one produced 13, 13, 13.

However, the result is that the first produced 13, 13, 13 while the second on produced 14, 14, 14.

Why does this happen?


Update: Following Chosen Answer:

I don't quite get what is independent and what is not. For example, in the following code:

x = tf.Variable([1, 2, 3, 4, 5])

def foo():
    tmp_list = []
    assign = tf.assign(x[4], 100)
    for i in range(0, 5):
        tmp_list.append(x[i])
    return tmp_list

z = foo()

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    val1, val2 = sess.run([z, assign])
    print(val1, val2)

In my Session.run() I have both z and assign. To get z, I need foo(). When foo() is ran, assign = tf.assign(x[4], 100) is also ran. However, the output is [1, 2, 3, 4, 5] 13 and I have no idea where the 13 comes from.

解决方案

There is no guarantee which of independent ops is performed first, so you can get 13 or 14 in both cases. Your result is pure luck. Try this:

with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  for i in range(100):
    val1, val2, val3 = sess.run([x, assign1, assign2])
    print(val1, val2, val3)

... and you'll see 13 and 14 printed out.


Update for the subsequent question:

z = foo() (which is just list of x slices) does not depend on assign. Note that foo is called just once before the session is started. I think, the best to see it is to visualize the graph in tensorboard.

If you say...

sess.run(z)

... tensorflow won't run assign, because it doesn't run a python function, it evaluates tensors. And tensor x or x[i] does not depend on the op assign.

Dependency is defined by operations that are performed with tensors. So if you had...

y = x + 2

... this is a dependency: in order to evaluate y, tensorflow must evaluate x. Hope this makes it more clear now.

这篇关于Session.run()如何选择运行哪个子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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