Returns a `ConcreteFunction` specialized to inputs and execution context. Args: *args: inputs to specialize on. **kwargs: inputs to specialize on.
(self, *args, **kwargs)
| 1859 | return graph_function |
| 1860 | |
| 1861 | def get_concrete_function(self, *args, **kwargs): |
| 1862 | """Returns a `ConcreteFunction` specialized to inputs and execution context. |
| 1863 | |
| 1864 | Args: |
| 1865 | *args: inputs to specialize on. |
| 1866 | **kwargs: inputs to specialize on. |
| 1867 | """ |
| 1868 | if self.input_signature: |
| 1869 | if kwargs: |
| 1870 | raise ValueError("Cannot define a TensorFlow function from a Python " |
| 1871 | "function with keyword arguments when " |
| 1872 | "input_signature is provided.") |
| 1873 | if args: |
| 1874 | # If args are provided, they must match the input signature. |
| 1875 | if not is_same_structure(self.input_signature, args): |
| 1876 | raise ValueError("Structure of Python function inputs does not match " |
| 1877 | "input_signature.") |
| 1878 | flat_inputs = nest.flatten(args, expand_composites=True) |
| 1879 | if any(not isinstance(arg, (ops.Tensor, tensor_spec.TensorSpec)) |
| 1880 | for arg in flat_inputs): |
| 1881 | raise ValueError("When input_signature is provided, all inputs to " |
| 1882 | "the Python function must be Tensors or " |
| 1883 | "tf.TensorSpec objects.") |
| 1884 | if any(not spec.is_compatible_with(other) |
| 1885 | for spec, other in zip(self.flat_input_signature, flat_inputs)): |
| 1886 | raise ValueError("Python inputs incompatible with input_signature: " |
| 1887 | "inputs (%s), input_signature (%s)" % |
| 1888 | (str(args), str(self.input_signature))) |
| 1889 | args, kwargs = None, None |
| 1890 | graph_function, args, kwargs = self._maybe_define_function(args, kwargs) |
| 1891 | if self.input_signature: |
| 1892 | args = self.input_signature |
| 1893 | kwargs = {} |
| 1894 | seen_names = set() |
| 1895 | captured = object_identity.ObjectIdentitySet( |
| 1896 | graph_function.graph.internal_captures) |
| 1897 | # pylint: disable=protected-access |
| 1898 | graph_function._arg_keywords = [] |
| 1899 | prefix_counts = {} |
| 1900 | # pylint: enable=protected-access |
| 1901 | num_positional = 0 |
| 1902 | for arg in graph_function.graph.inputs: |
| 1903 | if arg in captured: |
| 1904 | break |
| 1905 | num_positional += 1 |
| 1906 | user_arg_name = compat.as_str(arg.op.get_attr("_user_specified_name")) |
| 1907 | proposal = user_arg_name |
| 1908 | while proposal in seen_names: |
| 1909 | index = prefix_counts.get(user_arg_name, 1) |
| 1910 | proposal = "{}_{}".format(user_arg_name, index) |
| 1911 | prefix_counts[user_arg_name] = index + 1 |
| 1912 | seen_names.add(proposal) |
| 1913 | graph_function._arg_keywords.append(proposal) # pylint: disable=protected-access |
| 1914 | # Anything can be a positional argument, in the same order as .inputs |
| 1915 | graph_function._num_positional_args = num_positional # pylint: disable=protected-access |
| 1916 | return graph_function |
| 1917 | |
| 1918 | def __get__(self, instance, owner): |