Callable object encapsulating a function definition and its gradient. `ConcreteFunction` is a callable that encapsulates a function definition and is differentiable under `tf.GradientTape` objects.
| 1003 | |
| 1004 | |
| 1005 | class ConcreteFunction(object): |
| 1006 | """Callable object encapsulating a function definition and its gradient. |
| 1007 | |
| 1008 | `ConcreteFunction` is a callable that encapsulates a function definition and |
| 1009 | is differentiable under `tf.GradientTape` objects. |
| 1010 | """ |
| 1011 | |
| 1012 | def __init__(self, func_graph, attrs=None, signature=None, |
| 1013 | shared_func_graph=True): |
| 1014 | """Initialize a `ConcreteFunction`. |
| 1015 | |
| 1016 | Args: |
| 1017 | func_graph: An instance of FuncGraph: the function body to wrap. |
| 1018 | attrs: (optional) dict mapping names of attributes to their AttrValue |
| 1019 | values. Attributes in `attrs` will be included in this function's |
| 1020 | definition. |
| 1021 | signature: a nested sequence of `TensorSpec` objects specifying the input |
| 1022 | signature of this function. |
| 1023 | shared_func_graph: If False, the ConcreteFunction takes ownership of |
| 1024 | `func_graph` and will break reference cycles when it is deleted. This |
| 1025 | makes the FuncGraph inoperable. |
| 1026 | |
| 1027 | Raises: |
| 1028 | ValueError: If number of input_placeholders is not equal to the number |
| 1029 | of function inputs. |
| 1030 | """ |
| 1031 | self._arg_keywords = None |
| 1032 | self._num_positional_args = None |
| 1033 | self._func_graph = func_graph |
| 1034 | self._captured_inputs = self._func_graph.external_captures |
| 1035 | self._captured_closures = self._func_graph.deferred_external_captures |
| 1036 | self._output_shapes = tuple( |
| 1037 | output.shape for output in self._func_graph.outputs) |
| 1038 | attrs = _parse_func_attrs(attrs or {}) |
| 1039 | self._signature = signature |
| 1040 | |
| 1041 | if shared_func_graph: |
| 1042 | self._garbage_collector = None |
| 1043 | else: |
| 1044 | self._garbage_collector = ConcreteFunctionGarbageCollector( |
| 1045 | func_graph) |
| 1046 | |
| 1047 | # Pairs of forward and backward functions used for computing gradients. |
| 1048 | # |
| 1049 | # These each get a reference to the FuncGraph deleter since they use the |
| 1050 | # FuncGraph directly. |
| 1051 | self._delayed_rewrite_functions = _DelayedRewriteGradientFunctions( |
| 1052 | func_graph, attrs, self._garbage_collector) |
| 1053 | self._first_order_tape_functions = _FirstOrderTapeGradientFunctions( |
| 1054 | func_graph, attrs, self._garbage_collector) |
| 1055 | self._higher_order_tape_functions = _HigherOrderTapeGradientFunctions( |
| 1056 | func_graph, attrs, self._garbage_collector) |
| 1057 | |
| 1058 | def __call__(self, *args, **kwargs): |
| 1059 | """Executes the wrapped function. |
| 1060 | |
| 1061 | Args: |
| 1062 | *args: Tensors or Variables. Positional arguments are only accepted when |
no outgoing calls
no test coverage detected