创建操作后添加控件依赖? [英] Add control dependency after operations are created?

查看:25
本文介绍了创建操作后添加控件依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在创建两个操作后在两个操作之间创建控制依赖关系?我意识到使用 tf.control_dependencies 可以让一个操作在执行之前等待另一个操作,但是依赖操作必须在 tf.control_dependencies 上下文中创建.我想先独立构建这两个操作,然后添加依赖项.

Is it possible to create a control dependency between two ops after they've both been created? I realize that with tf.control_dependencies it's possible to make one op wait on another before executing, but the dependent op has to be created within the tf.control_dependencies context. I'd like to construct the two ops independently first and then add the dependency.

推荐答案

这可能是一个令人失望的答案,但不可能向 TensorFlow Operation 创建之后.张量和操作一旦创建就不可变.

This might be a disappointing answer, but it is not possible to add a control dependency (or any other input) to a TensorFlow Operation after it has been created. Tensors and operations are immutable once they have been created.

一种可能性是在适当的控制依赖上下文中克隆应该运行的第二个操作.假设您有两个 Operation 对象,op_firstop_second,并且您希望 op_first 之前运行>op_second:

One possibility would be to clone the op that should run second, in the appropriate control dependencies context. Let's say that you have two Operation objects, op_first and op_second, and you want op_first to run before op_second:

def first_before_second(op_first, op_second):
    """Sequence `op_first` before `op_second`.

    Given two operations, returns a pair of operations with the same behavior
    except that the first returned operation will execute before the second
    returned operation.
    """
    with tf.control_dependencies([op_first]):
        g = tf.get_default_graph()
        op_second_clone = g.create_op(op_second.type,
                                      [in_t for in_t in op_second.inputs],
                                      [out_t.dtype for out_t in op_second.outputs],
                                      attrs=op_second.node_def.attr,
                                      op_def=op_second.op_def)

    return op_first, op_second_clone

类似的修改也可以处理 Tensor 对象,但这留给读者作为练习.

A similar modification could make this deal with Tensor objects as well, but that's left as an exercise for the reader.

这篇关于创建操作后添加控件依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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