2017-05-29 142 views
2

earlier question之后,好像tf.group确实忽略了依赖关系。这里有一个简单的独立实例(我已经在Python 2.7版与TensorFlow 1.1运行它):TensorFlow tf.group忽略依赖关系?

import tensorflow as tf 
from tensorflow.python.ops import control_flow_ops 

xs = [tf.constant(x) for x in range(10)] 
xs = [tf.Print(x, [x]) for x in xs] 
dependency = None 
dxs = [] 

for x in xs: 
    if dependency is None: 
     dependency = x 
    else: 
     dependency = control_flow_ops.with_dependencies([dependency], x) 

    dxs.append(dependency) 

print_all_op = tf.group(*dxs) 

with tf.Session() as session: 
    session.run(print_all_op) 

预期输出:

2017-05-29 15:11:53.961221: I tensorflow/core/kernels/logging_ops.cc:79] [0] 
2017-05-29 15:11:53.961236: I tensorflow/core/kernels/logging_ops.cc:79] [1] 
2017-05-29 15:11:53.961255: I tensorflow/core/kernels/logging_ops.cc:79] [2] 
2017-05-29 15:11:53.961237: I tensorflow/core/kernels/logging_ops.cc:79] [3] 
2017-05-29 15:11:53.961262: I tensorflow/core/kernels/logging_ops.cc:79] [4] 
2017-05-29 15:11:53.961263: I tensorflow/core/kernels/logging_ops.cc:79] [5] 
2017-05-29 15:11:53.961268: I tensorflow/core/kernels/logging_ops.cc:79] [6] 
2017-05-29 15:11:53.961272: I tensorflow/core/kernels/logging_ops.cc:79] [7] 
2017-05-29 15:11:53.961274: I tensorflow/core/kernels/logging_ops.cc:79] [8] 
2017-05-29 15:11:53.961221: I tensorflow/core/kernels/logging_ops.cc:79] [9] 

实际输出(每次不同的代码运行):

2017-05-29 15:16:26.279655: I tensorflow/core/kernels/logging_ops.cc:79] [0] 
2017-05-29 15:16:26.279655: I tensorflow/core/kernels/logging_ops.cc:79] [9] 
2017-05-29 15:16:26.279697: I tensorflow/core/kernels/logging_ops.cc:79] [3] 
2017-05-29 15:16:26.279660: I tensorflow/core/kernels/logging_ops.cc:79] [1] 
2017-05-29 15:16:26.279711: I tensorflow/core/kernels/logging_ops.cc:79] [8] 
2017-05-29 15:16:26.279713: I tensorflow/core/kernels/logging_ops.cc:79] [4] 
2017-05-29 15:16:26.279723: I tensorflow/core/kernels/logging_ops.cc:79] [5] 
2017-05-29 15:16:26.279663: I tensorflow/core/kernels/logging_ops.cc:79] [2] 
2017-05-29 15:16:26.279724: I tensorflow/core/kernels/logging_ops.cc:79] [7] 
2017-05-29 15:16:26.279728: I tensorflow/core/kernels/logging_ops.cc:79] [6] 

tf.group文档中没有任何内容可以指示忽略依赖关系的原因。

是否有替代tf.group的确考虑了依赖关系?

切换到使用tf.control_dependencies而不是tensorflow.python.ops.control_flow_ops.with_dependencies没有帮助:

import tensorflow as tf 

xs = [tf.constant(x) for x in range(10)] 
xs = [tf.Print(x, [x]) for x in xs] 
dependency = None 
dxs = [] 

for x in xs: 
    if dependency is None: 
     dependency = x 
    else: 
     with tf.control_dependencies([dependency]): 
      dependency = x 

    dxs.append(dependency) 

print_all_op = tf.group(*dxs) 

with tf.Session() as session: 
    session.run(print_all_op) 

回答

2

我认为问题在于最初的代码创建了由control_flow_ops.with_dependencies隐含创建的虚拟标识操作符之间的依赖关系,而不是实际的tf.Print操作。 Tensorflow似乎只能确保依赖项列表中的操作已经执行,但其他先前操作的顺序不固定。在上面的例子中,在创建所述虚拟身份OPS创建由control_flow_ops.with_dependencies的依赖关系:

dependency = control_flow_ops.with_dependencies([dependency], x) 

这应该是等效于:

 with tf.control_dependencies([dependency]): 
      dependency = tf.identity(x) 

因此,依赖这里是本tf.identity OPS和之间产生而不是tf.Print ops。 tf.Print ops可以以任何顺序运行,严格的排序仅在tf.identity ops上。我不认为有可能通过control_flow_ops.with_dependencies达到预期的行为。相反,人们必须使用with tf.control_dependencies来代替(如已经暗示的那样):

xs = [tf.constant(x) for x in range(10)] 
dependency = None 
dxs = [] 

for x in xs: 
    if dependency is None: 
     dependency = tf.Print(x, [x]) 
    else: 
     with tf.control_dependencies([dependency]): 
      dependency = tf.Print(x, [x]) 

    dxs.append(dependency) 
3

使用tf.control_dependencies正确不解决这个问题:

import tensorflow as tf 

xs = [tf.constant(x) for x in range(10)] 
dependency = None 
dxs = [] 

for x in xs: 
    if dependency is None: 
     dependency = tf.Print(x, [x]) 
    else: 
     with tf.control_dependencies([dependency]): 
      dependency = tf.Print(x, [x]) 

    dxs.append(dependency) 

print_all_op = tf.group(*dxs) 

with tf.Session() as session: 
    session.run(print_all_op) 

注意,Print操作需要在tf.control_dependencies内创建情境管理器。

我仍然不清楚为什么control_flow_ops.with_dependencies版本失败。