Converts a Python entity into a TensorFlow graph. 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,
(entity, recursive=True, experimental_optional_features=None)
| 548 | |
| 549 | @tf_export('autograph.to_graph', v1=[]) |
| 550 | def to_graph(entity, recursive=True, experimental_optional_features=None): |
| 551 | """Converts a Python entity into a TensorFlow graph. |
| 552 | |
| 553 | Also see: `tf.autograph.to_code`, `tf.function`. |
| 554 | |
| 555 | Unlike `tf.function`, `to_graph` is a low-level transpiler that converts |
| 556 | Python code to TensorFlow graph code. It does not implement any caching, |
| 557 | variable management or create any actual ops, and is best used where greater |
| 558 | control over the generated TensorFlow graph is desired. Another difference |
| 559 | from `tf.function` is that `to_graph` will not wrap the graph into a |
| 560 | TensorFlow function or a Python callable. Internally, `tf.function` uses |
| 561 | `to_graph`. |
| 562 | |
| 563 | _Example Usage_ |
| 564 | |
| 565 | ```python |
| 566 | def foo(x): |
| 567 | if x > 0: |
| 568 | y = x * x |
| 569 | else: |
| 570 | y = -x |
| 571 | return y |
| 572 | |
| 573 | converted_foo = to_graph(foo) |
| 574 | |
| 575 | x = tf.constant(1) |
| 576 | y = converted_foo(x) # converted_foo is a TensorFlow Op-like. |
| 577 | assert is_tensor(y) |
| 578 | ``` |
| 579 | |
| 580 | Supported Python entities include: |
| 581 | * functions |
| 582 | * classes |
| 583 | * object methods |
| 584 | |
| 585 | Functions are converted into new functions with converted code. |
| 586 | |
| 587 | Classes are converted by generating a new class whose methods use converted |
| 588 | code. |
| 589 | |
| 590 | Methods are converted into unbound function that have an additional first |
| 591 | argument called `self`. |
| 592 | |
| 593 | Args: |
| 594 | entity: Python callable or class to convert. |
| 595 | recursive: Whether to recursively convert any functions that the converted |
| 596 | function may call. |
| 597 | experimental_optional_features: `None`, a tuple of, or a single |
| 598 | `tf.autograph.experimental.Feature` value. Controls the use of optional |
| 599 | features in the conversion process. |
| 600 | |
| 601 | Returns: |
| 602 | Same as `entity`, the converted Python function or class. |
| 603 | |
| 604 | Raises: |
| 605 | ValueError: If the entity could not be converted. |
| 606 | """ |
| 607 | try: |
no test coverage detected