Add control dependency after creating operations?

Is it possible to create a management dependency between two operations after they have been created? I understand that with the help of tf.control_dependenciesone you can do one action, waiting for another, before executing, but the dependent op must be created in context tf.control_dependencies. First, I would like to build two ops independently, and then add a dependency.

+4
source share
2 answers

This may be a disappointing answer, but it is not possible to add a control dependency (or any other input) to TensorFlow Operationafter it is created. Tensors and operations are immutable after they are created.

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

Tensor, .

+6

, op . , : def first_before_second_num(op_first, op_second): with tf.control_dependencies([op_first]): op_second += 0 return op_second

NoOp a train_step, NoOp: def first_before_second_noop(op_first, op_second): with tf.control_dependencies([op_first]): op_second tf.group(op_second, tf.no_op()) return op_second

0

Source: https://habr.com/ru/post/1627325/


All Articles