Python 启用“急切执行”时,Tensor.graph没有意义

Python 启用“急切执行”时,Tensor.graph没有意义,python,tensorflow,Python,Tensorflow,tensorflow-gpu2.0 我不知道为什么。我是tensorflow的初学者我试过tensorflow 1.14.0版 执行以下操作 a = tf.compat.v1.constant(5.0) b = tf.compat.v1.constant(6.0) sum1 = a + b g = tf.compat.v1.Graph() with g.as_default(): # Define operations and tensors in `g`. hello = t

tensorflow-gpu2.0
我不知道为什么。我是tensorflow的初学者

我试过tensorflow 1.14.0版

执行以下操作

a = tf.compat.v1.constant(5.0)
b = tf.compat.v1.constant(6.0)

sum1 = a + b
g = tf.compat.v1.Graph()
with g.as_default():
    # Define operations and tensors in `g`.
    hello = tf.compat.v1.constant('hello')
    assert hello.graph is g

sess = tf.compat.v1.Session(graph=g)

print(sess.run(sum1))


请尝试以下代码

pip install tensorflow==1.14..0

导入tensorflow后,需要禁用急切执行,如下所示:

import tensorflow as tf
a = tf.compat.v1.constant(5.0)
b = tf.compat.v1.constant(6.0)

sum1 = a + b
g = tf.compat.v1.Graph()
with g.as_default():
# Define operations and tensors in `g`.
    hello = tf.compat.v1.constant('hello')
    assert hello.graph is g
sess = tf.compat.v1.Session()
#sess = tf.compat.v1.Session(graph=g)
print(sess.run(sum1))

这对我有用。

我不知道你想做什么!但我做了一些猜测,这就是我得出的结果

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
我相信它可以与Tensorflow 2.x一起使用,如果您仍然得到渴望的异常,那么您应该添加:

import tensorflow as tf

g = tf.compat.v1.Graph()
with g.as_default():
    a = tf.compat.v1.constant(5.0)
    b = tf.compat.v1.constant(6.0)
    sum1 = tf.add(a, b) # instead of sum1 = a + b
    
    hello = tf.compat.v1.constant('hello')
    assert hello.graph is g

sess = tf.compat.v1.Session(graph=g)

print(sess.run(sum1))
以前

tf.compat.v1.disable_eager_execution()

你能详细说明你面临的问题吗?这是相当模糊的请通过一些教程(如)让自己熟悉TensorFlow。在版本2.x中,默认情况下是启用的,因此,正如错误所指出的,您不能使用图形和会话。您可以禁用急切执行并返回到1.x方式,但是如果您开始使用TensorFlow,您可能会学习2.x并使用它。将TensorFlow.compat.v1作为tf和tf导入。禁用_v2_behavior()尝试一下并检查一次。
assert hello.graph is g