张量流tf.集团忽略依赖关系?

2024-06-24 13:39:50 发布

您现在位置:Python中文网/ 问答频道 /正文

earlier question开始,似乎{}确实忽略了依赖关系。下面是一个简单的独立示例(我用TensorFlow 1.1在Python2.7上运行过):

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)

预期产量:

^{pr2}$

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

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

Tags: incoreforloggingtftensorflowaswith
2条回答

{{ops{I}与实际cd1}之间隐式创建的问题}。Tensorflow似乎只确保依赖列表中的操作已经被执行,但是前面其他操作的顺序并不是固定的。在上面的示例中,依赖项是在control_flow_ops.with_dependencies创建的伪标识操作上创建的:

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

相当于:

^{pr2}$

因此,这里的依赖关系是在tf.identity操作之间创建的,而不是{}操作之间创建的。tf.Print操作可以按任何顺序运行,严格的顺序只在{}操作上。我不认为用control_flow_ops.with_dependencies来实现期望的行为是不可能的。取而代之的是使用with tf.control_dependencies(正如op已经建议的那样):

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)

正确使用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版本失败。在

相关问题 更多 >