Python – tensorflow.executing_eagerly()

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. 

executing_eagerly() is used check if eager execution is enabled or disabled in current thread. By default eager execution is enabled so in most cases it will return true. This will return false in following cases:

  • If it is executing inside tensorflow.function and tf.init_scope or tf.config.experimental_run_functions_eagerly(True)  is not called previously.
  • Executing inside a transformation function for tensorflow.dataset.
  • tensorflow.compat.v1.disable_eager_execution() is called.

Syntax: tensorflow.executing_eagerly()

Parameters: This doesn’t accept any parameters.

Returns: It returns true is eager execution is enabled otherwise it will return false.



Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing the library
import tensorflow as tf
  
# Checking eager execution
res = tf.executing_eagerly()
  
# Printing the result
print('res: ', res)

chevron_right


Output:

res:  True

Example 2: This example checks eager execution for tensorflow.function with and without init_scope.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing the library
import tensorflow as tf
  
@tf.function
def gfg():
  with tf.init_scope():
    # Checking eager execution inside init_scope
    res = tf.executing_eagerly()
    print("res 1:", res)
  
  # Checking eager execution outside init_scope
  res = tf.executing_eagerly()
  print("res 2:", res)
gfg()

chevron_right


Output:

res 1: True
res 2: False


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.