我收到一个错误模块"tensorflow"没有属性"get_default_graph"。请帮助我解决此问题。tensorflow:2.4.0VsCodepython 3.6.9
enter code here
import tensorflow as tf
import numpy as np
n1 =tf.constant(1)
n2 = tf.constant(2)
n3 = n1+n2
with tf.compat.v1.Session() as sess:
result1 = sess.run(n3)
print(result1)
print(tf.get_default_graph())
g = tf.Graph()
print(g)
要执行加法运算,您可以在TF 2.x中使用compat.v1.session
执行以下代码。
import tensorflow as tf
mlt= 2*tf.Variable(4.0)
with tf.compat.v1.Session() as sess:
init= tf.compat.v1.global_variables_initializer()
sess.run(init)
print(sess.run(mlt))
在TF2.x中,急切执行默认启用,工作代码如下所示
import tensorflow as tf
gt = 2*tf.Variable(4.0)
print(gt)