Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Decorator

tf.function

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.

How it works

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.

Syntax

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

Parameters

NameTypeDescription
funccallable or NoneThe function to be compiled; if None, returns a decorator that accepts the function.
input_signaturenested sequence of tf.TensorSpec or NoneA possibly nested sequence of TensorSpec objects specifying supplied Tensor shapes and dtypes.
autographboolWhether AutoGraph should be applied to func before tracing.
experimental_autograph_optionstuple or NoneOptions passed to AutoGraph.
jit_compilebool or NoneWhether to use XLA compilation.
reduce_retracingboolWhether to reduce retracing.
experimental_implementsstr or NoneOptional name of an implementation associated with the function.
experimental_attributesdict or NoneAdditional attributes for the generated function.
experimental_relax_shapesbool or NoneDeprecated option controlling relaxed shape tracing.
experimental_compilebool or NoneDeprecated option controlling experimental compilation.
experimental_follow_type_hintsbool or NoneDeprecated option related to Python type hints.

Returns

tf.types.experimental.PolymorphicFunction

Key points

  • The decorator converts ordinary Python functions into callable graph-based TensorFlow functions.
  • The result is a polymorphic callable whose execution runs a TensorFlow graph.
  • One wrapped function can hold several concrete specializations for different input types or shapes.
  • Each invocation dispatches to the specialization matching the call's argument types and values.
  • An input signature declares the expected Tensor structure, shapes, and dtypes.
  • The autograph option controls whether AutoGraph transforms the Python function before graph tracing.

Common mistakes

  • Keep AutoGraph enabled when Python branches or loops depend on Tensor values; disabling it prevents that data-dependent control flow from being staged into the graph.
  • Stateful objects are supported, but ordinary Python side effects and mutable state should not be treated as per-call graph operations.

Example: Compile a function into a TensorFlow graph

import tensorflow as tf

@tf.function
def add(a, b):
    return a + b

result = add(tf.constant(2), tf.constant(3))
print(result.numpy())  # 5

The decorator traces the function into a TensorFlow graph and returns a callable that can be invoked with Tensor arguments.

When to use it

Build performant graph-based functions

Convert TensorFlow-heavy Python code into graph execution for optimization and portability.

Control tracing with an input signature

Declare accepted Tensor shapes and dtypes before calls are traced.

Stage control flow with AutoGraph

Express Tensor-dependent Python branches and loops as graph operations.

Interview questions

QWhat does 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.

QWhat is the purpose of 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.

QHow does the 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.PolymorphicFunction
  • tf.types.experimental.ConcreteFunction
  • tf.TensorSpec
  • tf.Variable
  • tf.Graph
  • tf.config.run_functions_eagerly

Source: tf.function | TensorFlow v2.16.1. Checked 2026-10-15.