Build performant graph-based functions
Convert TensorFlow-heavy Python code into graph execution for optimization and portability.
TensorFlow · Decorator
Compiles a Python function into a callable TensorFlow graph. The returned polymorphic callable can dispatch among specialized concrete graphs for different argument types or shapes.
tf.function wraps a Python function in a polymorphic callable that trace-compiles TensorFlow operations into one or more specialized graphs. Calls dispatch to a suitable concrete graph; input_signature can constrain accepted Tensor shapes and dtypes, while AutoGraph converts data-dependent Python control flow when enabled.
tf.function(func=None, input_signature=None, autograph=True, jit_compile=None, reduce_retracing=False, experimental_implements=None, experimental_autograph_options=None, experimental_attributes=None, experimental_relax_shapes=None, experimental_compile=None, experimental_follow_type_hints=None) -> tf.types.experimental.PolymorphicFunction| Name | Type | Description |
|---|---|---|
func | callable or None | The function to be compiled; if None, returns a decorator that accepts the function. |
input_signature | nested sequence of tf.TensorSpec or None | A possibly nested sequence of TensorSpec objects specifying supplied Tensor shapes and dtypes. |
autograph | bool | Whether AutoGraph should be applied to func before tracing. |
experimental_autograph_options | tuple or None | Options passed to AutoGraph. |
jit_compile | bool or None | Whether to use XLA compilation. |
reduce_retracing | bool | Whether to reduce retracing. |
experimental_implements | str or None | Optional name of an implementation associated with the function. |
experimental_attributes | dict or None | Additional attributes for the generated function. |
experimental_relax_shapes | bool or None | Deprecated option controlling relaxed shape tracing. |
experimental_compile | bool or None | Deprecated option controlling experimental compilation. |
experimental_follow_type_hints | bool or None | Deprecated option related to Python type hints. |
tf.types.experimental.PolymorphicFunction
autograph option controls whether AutoGraph transforms the Python function before graph tracing.import tensorflow as tf
@tf.function
def add(a, b):
return a + b
result = add(tf.constant(2), tf.constant(3))
print(result.numpy()) # 5The decorator traces the function into a TensorFlow graph and returns a callable that can be invoked with Tensor arguments.
Convert TensorFlow-heavy Python code into graph execution for optimization and portability.
Declare accepted Tensor shapes and dtypes before calls are traced.
Express Tensor-dependent Python branches and loops as graph operations.
tf.function return and why can it represent more than one graph?It transforms func into a tf.types.experimental.PolymorphicFunction that executes a TensorFlow graph produced by tracing the function's TensorFlow operations. The callable can contain multiple specialized concrete graphs for differing argument types or shapes.
Follow-up: How can graph selection be constrained? Supply an input_signature consisting of TensorSpec objects describing the supplied Tensor shapes and dtypes.
input_signature?input_signature is a possibly nested sequence of tf.TensorSpec objects. It specifies the shapes and dtypes of Tensor arguments accepted by the function, helping control tracing.
Follow-up: What happens when no signature is supplied? The polymorphic callable can create or select specializations based on argument types and shapes.
autograph parameter affect tracing?With autograph=True, AutoGraph is applied before tracing, so Python control flow that depends on Tensor values can be converted into graph operations. With autograph=False, data-dependent Python control flow is not allowed.
Follow-up: What setting is needed for Tensor-dependent branches or loops? Keep autograph=True, the default.
tf.types.experimental.PolymorphicFunctiontf.types.experimental.ConcreteFunctiontf.TensorSpectf.Variabletf.Graphtf.config.run_functions_eagerly