ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more


Decorator that allow a function/method to run in graph and in eager modes.

When applied in graph mode it calls the function and return its outputs. When applied in eager mode it returns a lambda function that when called returns the outputs.

def loss_fn(x):
  v = tf.get_variable('v', initializer=tf.ones_initializer(), shape=())
  return v + x

with context.graph_mode():
  loss_op = loss_fn(inputs)
  loss_value =

with context.eager_mode():
  loss = loss_fn(inputs)
  # Now loss is a Future callable.
  loss_value = loss()

func_or_method A function or method to decorate.

Either the output ops of the function/method or a Future (lambda function).