tensorflow中的disable_eager_execution和enable_eager_execution问题

tensorflow中的disable_eager_execution和enable_eager_execution问题


在tensorflow中,我们经常遇到一些错误,例如下面的提示

An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
  @tf.function
  def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2
The graph tensor has name: model/lambda/strided_slice:0

在这种情况下一般有两种解决办法,第一个是设置tf.disable_eager_execution(),第二个是设置tf.config.experimental_run_functions_eagerly(True)。
在tensorflow2.0版本及其以上,disable_eager_execution和enable_eager_execution在tf.compat.v1里面; 如果在自己写的函数中有的需要设置disable_eager_execution,有的需要设置enable_eager_execution,那么推荐统一的使用tf.config.experimental_run_functions_eagerly(True)的设置,可以解决同时使用的问题。

你可能感兴趣的:(笔记,python,tensorflow)