Eager execution provides an imperative interface to TensorFlow. With eager
execution enabled, TensorFlow functions execute operations immediately (as
opposed to adding to a graph to be executed later in a tf.compat.v1.Session)
and
return concrete values (as opposed to symbolic references to a node in a
computational graph).
For example:
tf.compat.v1.enable_eager_execution()# After eager execution is enabled, operations are executed as they are# defined and Tensor objects hold concrete values, which can be accessed as# numpy.ndarray`s through the numpy() method.asserttf.multiply(6,7).numpy()==42
Eager execution cannot be enabled after TensorFlow APIs have been used to
create or execute graphs. It is typically recommended to invoke this function
at program startup and not in a library (as most libraries should be usable
both with and without eager execution).
(Optional.) Policy controlling how operations requiring
inputs on a specific device (e.g., a GPU 0) handle inputs on a different
device (e.g. GPU 1 or CPU). When set to None, an appropriate value will
be picked automatically. The value picked may change between TensorFlow
releases.
Valid values:
DEVICE_PLACEMENT_EXPLICIT: raises an error if the
placement is not correct.
DEVICE_PLACEMENT_WARN: copies the tensors which are not
on the right device but logs a warning.
DEVICE_PLACEMENT_SILENT: silently copies the tensors.
Note that this may hide performance problems as there is no notification
provided when operations are blocked on the tensor being copied between
devices.
DEVICE_PLACEMENT_SILENT_FOR_INT32: silently copies
int32 tensors, raising errors on the other ones.
execution_mode
(Optional.) Policy controlling how operations dispatched are
actually executed. When set to None, an appropriate value will be picked
automatically. The value picked may change between TensorFlow releases.
Valid values:
SYNC: executes each operation synchronously.
ASYNC: executes each operation asynchronously. These
operations may return "non-ready" handles.
Raises
ValueError
If eager execution is enabled after creating/executing a
TensorFlow graph, or if options provided conflict with a previous call
to this function.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.compat.v1.enable_eager_execution\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/ops.py#L4873-L4948) |\n\nEnables eager execution for the lifetime of this program. \n\n tf.compat.v1.enable_eager_execution(\n config=None, device_policy=None, execution_mode=None\n ) -\u003e None\n\n\u003cbr /\u003e\n\nMigrate to TF2\n--------------\n\n\u003cbr /\u003e\n\n| **Caution:** This API was designed for TensorFlow v1. Continue reading for details on how to migrate from this API to a native TensorFlow v2 equivalent. See the [TensorFlow v1 to TensorFlow v2 migration guide](https://www.tensorflow.org/guide/migrate) for instructions on how to migrate the rest of your code.\n\nThis function is not necessary if you are using TF2. Eager execution is\nenabled by default.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nDescription\n-----------\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [TensorFlow Constrained Optimization Example Using CelebA Dataset](https://www.tensorflow.org/responsible_ai/fairness_indicators/tutorials/Fairness_Indicators_TFCO_CelebA_Case_Study) - [TensorFlow Distributions: A Gentle Introduction](https://www.tensorflow.org/probability/examples/TensorFlow_Distributions_Tutorial) |\n\nEager execution provides an imperative interface to TensorFlow. With eager\nexecution enabled, TensorFlow functions execute operations immediately (as\nopposed to adding to a graph to be executed later in a [`tf.compat.v1.Session`](../../../tf/compat/v1/Session))\nand\nreturn concrete values (as opposed to symbolic references to a node in a\ncomputational graph).\n\n#### For example:\n\n tf.compat.v1.enable_eager_execution()\n\n # After eager execution is enabled, operations are executed as they are\n # defined and Tensor objects hold concrete values, which can be accessed as\n # numpy.ndarray`s through the numpy() method.\n assert tf.multiply(6, 7).numpy() == 42\n\nEager execution cannot be enabled after TensorFlow APIs have been used to\ncreate or execute graphs. It is typically recommended to invoke this function\nat program startup and not in a library (as most libraries should be usable\nboth with and without eager execution).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `config` | (Optional.) A [`tf.compat.v1.ConfigProto`](../../../tf/compat/v1/ConfigProto) to use to configure the environment in which operations are executed. Note that [`tf.compat.v1.ConfigProto`](../../../tf/compat/v1/ConfigProto) is also used to configure graph execution (via [`tf.compat.v1.Session`](../../../tf/compat/v1/Session)) and many options within [`tf.compat.v1.ConfigProto`](../../../tf/compat/v1/ConfigProto) are not implemented (or are irrelevant) when eager execution is enabled. |\n| `device_policy` | (Optional.) Policy controlling how operations requiring inputs on a specific device (e.g., a GPU 0) handle inputs on a different device (e.g. GPU 1 or CPU). When set to None, an appropriate value will be picked automatically. The value picked may change between TensorFlow releases. Valid values: \u003cbr /\u003e - DEVICE_PLACEMENT_EXPLICIT: raises an error if the placement is not correct. - DEVICE_PLACEMENT_WARN: copies the tensors which are not on the right device but logs a warning. - DEVICE_PLACEMENT_SILENT: silently copies the tensors. Note that this may hide performance problems as there is no notification provided when operations are blocked on the tensor being copied between devices. - DEVICE_PLACEMENT_SILENT_FOR_INT32: silently copies int32 tensors, raising errors on the other ones. |\n| `execution_mode` | (Optional.) Policy controlling how operations dispatched are actually executed. When set to None, an appropriate value will be picked automatically. The value picked may change between TensorFlow releases. Valid values: - SYNC: executes each operation synchronously. - ASYNC: executes each operation asynchronously. These operations may return \"non-ready\" handles. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| `ValueError` | If eager execution is enabled after creating/executing a TensorFlow graph, or if options provided conflict with a previous call to this function. |\n\n\u003cbr /\u003e"]]