``tensorflow.executing_eagerly()`` 语法与代码演示


发布日期 : 2022-06-11 03:59:48 UTC

访问量: 9 次浏览

Python – tensorflow.executing_eagerly()

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

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

  • 如果它是在tensorflow.function_and_ tf.init_scope或tf.config.experimental_run_functions_eagerly(True)内执行的,则之前没有调用。
  • 在tensorflow.dataset的转换函数内执行。
  • tensorflow.compat.v1.disable\_eager\_execution() 被调用。

语法:
tensorflow.executing_eagerly()

参数:
这不接受任何参数。

返回:
如果启用了急切执行,则返回真,否则将返回假。

示例 1:

# Importing the library
import tensorflow as tf

# Checking eager execution
res = tf.executing_eagerly()

# Printing the result
print('res: ', res)

输出:

res: True

实例2:
本例检查了有无 init\_scopetensorflow.function 的急切执行。

# 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