defmy_func(a):print("Python side effect")returna+aa_fn=tf.function(my_func)
# A side effect the first time the function is traceda_fn(tf.constant(1))Pythonsideeffect<tf.Tensor:shape=(),dtype=int32,numpy=2>
# No further side effect, as the traced function is calleda_fn(tf.constant(2))<tf.Tensor:shape=(),dtype=int32,numpy=4>
# Now, switch to eager runningtf.config.run_functions_eagerly(True)# Side effect, as the function is called directlya_fn(tf.constant(2))Pythonsideeffect<tf.Tensor:shape=(),dtype=int32,numpy=4>
# Turn this back offtf.config.run_functions_eagerly(False)
[null,null,["Last updated 2023-03-17 UTC."],[],[],null,["# tf.config.run_functions_eagerly\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.9.3/tensorflow/python/eager/def_function.py#L413-L455) |\n\nEnables / disables eager execution of [`tf.function`](../../tf/function)s.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.config.run_functions_eagerly`](https://www.tensorflow.org/api_docs/python/tf/config/run_functions_eagerly)\n\n\u003cbr /\u003e\n\n tf.config.run_functions_eagerly(\n run_eagerly\n )\n\nCalling [`tf.config.run_functions_eagerly(True)`](../../tf/config/run_functions_eagerly) will make all\ninvocations of [`tf.function`](../../tf/function) run eagerly instead of running as a traced graph\nfunction.\n\nThis can be useful for debugging. \n\n def my_func(a):\n print(\"Python side effect\")\n return a + a\n a_fn = tf.function(my_func)\n\n # A side effect the first time the function is traced\n a_fn(tf.constant(1))\n Python side effect\n \u003ctf.Tensor: shape=(), dtype=int32, numpy=2\u003e\n\n # No further side effect, as the traced function is called\n a_fn(tf.constant(2))\n \u003ctf.Tensor: shape=(), dtype=int32, numpy=4\u003e\n\n # Now, switch to eager running\n tf.config.run_functions_eagerly(True)\n # Side effect, as the function is called directly\n a_fn(tf.constant(2))\n Python side effect\n \u003ctf.Tensor: shape=(), dtype=int32, numpy=4\u003e\n\n # Turn this back off\n tf.config.run_functions_eagerly(False)\n\n| **Note:** This flag has no effect on functions passed into tf.data transformations as arguments. tf.data functions are never executed eagerly and are always executed as a compiled Tensorflow Graph.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|--------------------------------------------|\n| `run_eagerly` | Boolean. Whether to run functions eagerly. |\n\n\u003cbr /\u003e"]]