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, 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 f(x):
if x > 0:
y = x * x
else:
y = -x
return y
converted_f = to_graph(f)
x = tf.constant(2)
converted_f(x) # converted_foo is like a TensorFlow Op.
<tf.Tensor: shape=(), dtype=int32, numpy=4>
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
.
For a tutorial, see the
tf.function and AutoGraph guide.
For more detailed information, see the
AutoGraph reference documentation.
Args |
entity
|
Python callable or class to convert.
|
recursive
|
Whether to recursively convert any functions that the converted
function may call.
|
experimental_optional_features
|
None , a tuple of, or a single
tf.autograph.experimental.Feature value.
|
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. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.autograph.to_graph\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/autograph/impl/api.py#L707-L776) |\n\nConverts a Python entity into a TensorFlow graph. \n\n tf.autograph.to_graph(\n entity, recursive=True, 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 f(x):\n if x \u003e 0:\n y = x * x\n else:\n y = -x\n return y\n\n converted_f = to_graph(f)\n x = tf.constant(2)\n converted_f(x) # converted_foo is like a TensorFlow Op.\n \u003ctf.Tensor: shape=(), dtype=int32, numpy=4\u003e\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\nFor a tutorial, see the\n[tf.function and AutoGraph guide](https://www.tensorflow.org/guide/function).\nFor more detailed information, see the\n[AutoGraph reference documentation](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/autograph/g3doc/reference/index.md).\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| `experimental_optional_features` | `None`, a tuple of, or a single [`tf.autograph.experimental.Feature`](../../tf/autograph/experimental/Feature) value. |\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"]]