当急切执行被禁用时,我需要在tensorflow 2下运行tensorflow模型。例如,假设我的模型构建如下:
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此时,如何启动模型的执行并打印c的结果值?在tensorflow 1中,这可以使用会话和run()方法来完成,但我不知道如何在tensorflow 2中本机完成。
发布于 2021-01-19 11:47:55
您可以使用tf.compat.v1.Session。我已经在你的例子中添加了。以下是代码
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
sess = tf.compat.v1.Session()
with sess.as_default():
o = sess.run(c)
print(o)https://stackoverflow.com/questions/65778424
复制相似问题