是否有可能在两个操作创建后创建控件依赖关系?我意识到,tf.control_dependencies在执行之前可以让一个操作等待另一个操作,但必须在tf.control_dependencies上下文中创建依赖操作.我想首先独立构造两个ops,然后添加依赖项.
这可能是一个令人失望的答案,但是Operation在创建TensorFlow 之后,无法向TensorFlow添加控件依赖项(或任何其他输入).一旦创建了张量和操作,它们就是不可变的.
一种可能性是在适当的控制依赖关系上下文中克隆应该运行第二个的op.比方说,你有两个Operation对象,op_first并且op_second,你想op_first之前运行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
Run Code Online (Sandbox Code Playgroud)
类似的修改也可以使这个处理Tensor对象,但这留给读者练习.
| 归档时间: |
|
| 查看次数: |
3315 次 |
| 最近记录: |