import tensorflow.compat.v1 as tf
#import tensorflow as tf
tf.disable_eager_execution()
#2.0版本与1.0版本不兼容,使用2.0版本时应使用上面的程序完成从1.x到2.x的迁移
a = tf.constant(4)
b = tf.constant(7)
c = a*b
sess = tf.compat.v1.Session()
#使用1.0的迁移
print(sess.run(c))
sess.close()#28
第一种情况,在使用Session提供了Operation执行和Tensor求值的环境,接收后需要手动关闭,不然变量没办法释放;
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
#2.0版本与1.0版本不兼容,使用2.0版本时应使用上面的程序完成从1.x到2.x的迁移
a = tf.constant(4)
b = tf.constant(7)
c = a*b
with tf.compat.v1.Session() as sess:
print (sess.run(c))#28
第二种,可以使用with.tf.Session()来执行,无需关闭可以自动释放