tf.autograph.to_graph
Stay organized with collections
Save and categorize content based on your preferences.
Converts a Python entity into a TensorFlow graph.
tf.autograph.to_graph(
entity, recursive=True, arg_values=None, arg_types=None,
experimental_optional_features=None
)
Also see: tf.autograph.to_code
, tf.function
.
Unlike tf.function
, to_graph
is a low-level transpiler that converts
Python code to TensorFlow graph code. It does not implement any caching,
variable management or create any actual ops, and is best used where greater
control over the generated TensorFlow graph is desired. Another difference
from tf.function
is that to_graph
will not wrap the graph into a
TensorFlow function or a Python callable. Internally, tf.function
uses
to_graph
.
Example Usage
def foo(x):
if x > 0:
y = x * x
else:
y = -x
return y
converted_foo = to_graph(foo)
x = tf.constant(1)
y = converted_foo(x) # converted_foo is a TensorFlow Op-like.
assert is_tensor(y)
Supported Python entities include:
- functions
- classes
- object methods
Functions are converted into new functions with converted code.
Classes are converted by generating a new class whose methods use converted
code.
Methods are converted into unbound function that have an additional first
argument called self
.
Args |
entity
|
Python callable or class to convert.
|
recursive
|
Whether to recursively convert any functions that the converted
function may call.
|
arg_values
|
Deprecated.
|
arg_types
|
Deprecated.
|
experimental_optional_features
|
None , a tuple of, or a single
tf.autograph.experimental.Feature value. Controls the use of optional
features in the conversion process.
|
Returns |
Same as entity , the converted Python function or class.
|
Raises |
ValueError
|
If the entity could not be converted.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.autograph.to_graph\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 2 version](/api_docs/python/tf/autograph/to_graph) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/autograph/impl/api.py#L621-L690) |\n\nConverts a Python entity into a TensorFlow graph.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.autograph.to_graph`](/api_docs/python/tf/compat/v1/autograph/to_graph)\n\n\u003cbr /\u003e\n\n tf.autograph.to_graph(\n entity, recursive=True, arg_values=None, arg_types=None,\n experimental_optional_features=None\n )\n\nAlso see: [`tf.autograph.to_code`](../../tf/autograph/to_code), [`tf.function`](../../tf/function).\n\nUnlike [`tf.function`](../../tf/function), `to_graph` is a low-level transpiler that converts\nPython code to TensorFlow graph code. It does not implement any caching,\nvariable management or create any actual ops, and is best used where greater\ncontrol over the generated TensorFlow graph is desired. Another difference\nfrom [`tf.function`](../../tf/function) is that `to_graph` will not wrap the graph into a\nTensorFlow function or a Python callable. Internally, [`tf.function`](../../tf/function) uses\n`to_graph`.\n\n*Example Usage* \n\n def foo(x):\n if x \u003e 0:\n y = x * x\n else:\n y = -x\n return y\n\n converted_foo = to_graph(foo)\n\n x = tf.constant(1)\n y = converted_foo(x) # converted_foo is a TensorFlow Op-like.\n assert is_tensor(y)\n\nSupported Python entities include:\n\n- functions\n- classes\n- object methods\n\nFunctions are converted into new functions with converted code.\n\nClasses are converted by generating a new class whose methods use converted\ncode.\n\nMethods are converted into unbound function that have an additional first\nargument called `self`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `entity` | Python callable or class to convert. |\n| `recursive` | Whether to recursively convert any functions that the converted function may call. |\n| `arg_values` | Deprecated. |\n| `arg_types` | Deprecated. |\n| `experimental_optional_features` | `None`, a tuple of, or a single [`tf.autograph.experimental.Feature`](../../tf/autograph/experimental/Feature) value. Controls the use of optional features in the conversion process. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Same as `entity`, the converted Python function or class. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|---------------------------------------|\n| `ValueError` | If the entity could not be converted. |\n\n\u003cbr /\u003e"]]