📜  Python – tensorflow.executing_eagerly()

📅  最后修改于: 2022-05-13 01:54:20.656000             🧑  作者: Mango

Python – tensorflow.executing_eagerly()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

execution_eagerly ()用于检查当前线程中是否启用或禁用了急切执行。默认情况下,急切执行是启用的,因此在大多数情况下它将返回 true。这将在以下情况下返回 false:

  • 如果它在tensorflow 中执行。函数tf.init_scopetf.config.experimental_run_functions_eagerly(True)之前未调用。
  • 在 tensorflow.dataset 的转换函数中执行。
  • tensorflow.compat.v1.disable_eager_execution() 被调用。

示例 1:

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


Python3
# 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()


输出:

res:  True

示例 2:此示例检查 tensorflow 的急切执行。有和没有 init_scope 的函数。

Python3

# 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()

输出:

res 1: True
res 2: False