Returns a context manager that specifies control dependencies. Use with the `with` keyword to specify that all operations constructed within the context should have control dependencies on `control_inputs`. For example: ```python with g.control_dependencies([a, b, c]): #
(self, control_inputs)
| 4637 | controller.add_op(op) |
| 4638 | |
| 4639 | def control_dependencies(self, control_inputs): |
| 4640 | """Returns a context manager that specifies control dependencies. |
| 4641 | |
| 4642 | Use with the `with` keyword to specify that all operations constructed |
| 4643 | within the context should have control dependencies on |
| 4644 | `control_inputs`. For example: |
| 4645 | |
| 4646 | ```python |
| 4647 | with g.control_dependencies([a, b, c]): |
| 4648 | # `d` and `e` will only run after `a`, `b`, and `c` have executed. |
| 4649 | d = ... |
| 4650 | e = ... |
| 4651 | ``` |
| 4652 | |
| 4653 | Multiple calls to `control_dependencies()` can be nested, and in |
| 4654 | that case a new `Operation` will have control dependencies on the union |
| 4655 | of `control_inputs` from all active contexts. |
| 4656 | |
| 4657 | ```python |
| 4658 | with g.control_dependencies([a, b]): |
| 4659 | # Ops constructed here run after `a` and `b`. |
| 4660 | with g.control_dependencies([c, d]): |
| 4661 | # Ops constructed here run after `a`, `b`, `c`, and `d`. |
| 4662 | ``` |
| 4663 | |
| 4664 | You can pass None to clear the control dependencies: |
| 4665 | |
| 4666 | ```python |
| 4667 | with g.control_dependencies([a, b]): |
| 4668 | # Ops constructed here run after `a` and `b`. |
| 4669 | with g.control_dependencies(None): |
| 4670 | # Ops constructed here run normally, not waiting for either `a` or `b`. |
| 4671 | with g.control_dependencies([c, d]): |
| 4672 | # Ops constructed here run after `c` and `d`, also not waiting |
| 4673 | # for either `a` or `b`. |
| 4674 | ``` |
| 4675 | |
| 4676 | *N.B.* The control dependencies context applies *only* to ops that |
| 4677 | are constructed within the context. Merely using an op or tensor |
| 4678 | in the context does not add a control dependency. The following |
| 4679 | example illustrates this point: |
| 4680 | |
| 4681 | ```python |
| 4682 | # WRONG |
| 4683 | def my_func(pred, tensor): |
| 4684 | t = tf.matmul(tensor, tensor) |
| 4685 | with tf.control_dependencies([pred]): |
| 4686 | # The matmul op is created outside the context, so no control |
| 4687 | # dependency will be added. |
| 4688 | return t |
| 4689 | |
| 4690 | # RIGHT |
| 4691 | def my_func(pred, tensor): |
| 4692 | with tf.control_dependencies([pred]): |
| 4693 | # The matmul op is created in the context, so a control dependency |
| 4694 | # will be added. |
| 4695 | return tf.matmul(tensor, tensor) |
| 4696 | ``` |