禁用Tensorflow急切执行

时间:2018-11-22 11:24:12

标签: tensorflow

我正在尝试学习TF并与占位符一起工作。当我尝试创建占位符时,出现错误: clang version 7.0.0 (https://github.com/eosio/clang.git 5647a49355fbcffd0bc940a7b857318f5bc84beb) (https://github.com/eosio/llvm e0f9c15ad4199a628dda25df589cad1634d01c5b) Target: wasm32 Thread model: single InstalledDir: /usr/opt/eosio.cdt/1.3.2/bin "/usr/opt/eosio.cdt/1.3.2/bin/clang-7" -cc1 -triple wasm32 -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hello.cpp -mrelocation-model static -mthread-model single -masm-verbose -mconstructor-aliases -ffreestanding -fuse-init-array -target-cpu generic -fvisibility hidden -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -coverage-notes-file /home/ubuntu/contracts/hello/hello.gcno -resource-dir /usr/opt/eosio.cdt/1.3.2/lib/clang/7.0.0 -D BOOST_DISABLE_ASSERTS -D BOOST_EXCEPTION_DISABLE -I /usr/opt/eosio.cdt/1.3.2/bin/../include/libcxx -I /usr/opt/eosio.cdt/1.3.2/bin/../include/libc -I /usr/opt/eosio.cdt/1.3.2/bin/../include -isysroot /usr/opt/eosio.cdt/1.3.2/bin/../ -internal-isystem /usr/opt/eosio.cdt/1.3.2/bin/..//include/c++/v1 -internal-isystem /usr/opt/eosio.cdt/1.3.2/bin/..//include -O3 --std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/ubuntu/contracts/hello -ferror-limit 19 -fmessage-length 189 -fno-builtin -fno-rtti -fno-threadsafe-statics -fobjc-runtime=gnustep -fno-common -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -load /usr/opt/eosio.cdt/1.3.2/bin/eosio_plugin.so -load /usr/opt/eosio.cdt/1.3.2/bin/LLVMEosioApply.so -mllvm -use-cfl-aa-in- ,因为占位符不能立即执行,因此很有意义。

我的挣扎在于寻找如何关闭渴望执行的行为?

我从来没有首先执行过急切的执行,所以不确定它是如何发生的。与RuntimeError: tf.placeholder() is not compatible with eager execution是否相反?

谢谢!

4 个答案:

答案 0 :(得分:8)

假设您正在使用Tensorflow 2.0预览版本,该版本默认情况下渴望执行。 v1 API中有一个disable_eager_execution(),您可以将其放在代码的前面,例如:

import tensorflow as tf

tf.compat.v1.disable_eager_execution()

另一方面,如果您未使用 2.0预览,请检查是否在某个地方意外启用了急切执行。

答案 1 :(得分:6)

在TensorFlow 2.3中,您可以随时使用以下方法禁用急切模式:

import tensorflow as tf

tf.config.run_functions_eagerly(False)

答案 2 :(得分:5)

您可以禁用TensorFlow v2这样的行为:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

答案 3 :(得分:0)

我假设您正在使用TensorFlow 2.0。在TF2中,默认情况下开启了紧急模式。但是,TensorFlow 2.0.0-alpha0中有一个disable_eager_execution(),但它隐藏得很深,无法从顶级模块名称空间(即tf名称空间)直接访问。

您可以这样调用函数:

import tensorflow as tf
from tensorflow.python.framework.ops import disable_eager_execution

disable_eager_execution()

a = tf.constant(1)
b = tf.constant(2)
c = a + b
print(c)

>>>Tensor("add:0", shape=(), dtype=int32)

print(disable_eager_execution.__doc__) 

>>>Disables eager execution. This function can only be called before any Graphs, Ops, or Tensors have been created. It can be used at the beginning of the program for complex migration projects from TensorFlow 1.x to 2.x.