MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / to_graph

Function to_graph

tensorflow/python/autograph/impl/api.py:550–618  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

548
549@tf_export('autograph.to_graph', v1=[])
550def 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:

Callers 2

to_graph_v1Function · 0.85
to_codeFunction · 0.85

Calls 4

ConversionErrorClass · 0.85
errorMethod · 0.80
convertMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected