A helper for constructing a function. _FuncGraph overrides ops.Graph's create_op() so that we can keep track of all inputs into every op created inside the function. If any input is from other graphs, we keep track of it in self.capture and substitute the input with a place holder. Each
| 680 | |
| 681 | |
| 682 | class _FuncGraph(ops.Graph): |
| 683 | """A helper for constructing a function. |
| 684 | |
| 685 | _FuncGraph overrides ops.Graph's create_op() so that we can keep |
| 686 | track of all inputs into every op created inside the function. If |
| 687 | any input is from other graphs, we keep track of it in self.capture |
| 688 | and substitute the input with a place holder. |
| 689 | |
| 690 | Each captured input's corresponding place holder is converted into a |
| 691 | function argument and the caller passes in the captured tensor. |
| 692 | """ |
| 693 | |
| 694 | def __init__(self, name, capture_by_value, whitelisted_stateful_ops, |
| 695 | capture_resource_var_by_value, *args, **kwargs): |
| 696 | super(_FuncGraph, self).__init__(*args, **kwargs) |
| 697 | self._capture_by_value = capture_by_value |
| 698 | self._whitelisted_stateful_ops = whitelisted_stateful_ops |
| 699 | self._capture_resource_var_by_value = capture_resource_var_by_value |
| 700 | self._building_function = True |
| 701 | self._outer_graph = ops.get_default_graph() |
| 702 | self._vscope = vs.get_variable_scope() |
| 703 | self._old_custom_getter = self._vscope.custom_getter |
| 704 | |
| 705 | # The name of the function. |
| 706 | self.name = name |
| 707 | # Placeholder tensors representing the inputs to this function. The tensors |
| 708 | # are in this _FuncGraph. |
| 709 | self.inputs = [] |
| 710 | # Tensors that will be returned this function. The tensors are in this |
| 711 | # _FuncGraph. |
| 712 | self.outputs = [] |
| 713 | # Maps external tensor -> internal tensor (e.g. input placeholder). |
| 714 | self._captured = object_identity.ObjectIdentityDictionary() |
| 715 | # The external tensors that have been captured as inputs and must be passed |
| 716 | # to this function (empty if capturing by value, otherwise these are the |
| 717 | # keys of _captured). |
| 718 | self.extra_inputs = [] |
| 719 | # Input placeholders that been added for captured values (empty if capturing |
| 720 | # by value). |
| 721 | self.extra_args = [] |
| 722 | # Captured variables. |
| 723 | # TODO(skyewm): is this needed? |
| 724 | self.extra_vars = [] |
| 725 | |
| 726 | # pylint: disable=g-doc-return-or-yield |
| 727 | |
| 728 | @tf_contextlib.contextmanager |
| 729 | def container(self, container_name): |
| 730 | """Returns a context manager that specifies the resource container to use. |
| 731 | |
| 732 | Overridden from `tf.Graph` to update both the init_scope container |
| 733 | and the present inner container. This is necessary to make sure setting |
| 734 | containers applies correctly both to created variables and to stateful |
| 735 | ops. |
| 736 | |
| 737 | Args: |
| 738 | container_name: container name string. |
| 739 |
no outgoing calls
no test coverage detected