tensorflow2.x运行tensorflow1.x的代码

tensorflow2.x兼容tensorflow1.x版本,分为两布:
1、import tensorflow.compat.v1 as tf 代替 import tensorflow as tf
2、执行tf.disable_eager_execution()
代码实例:

# import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()

v1 = tf.constant([1, 2, 3, 4])
v2 = tf.constant([2, 1, 5, 3])
v_add = tf.add(v1, v2)
# print(v_add)
with tf.Session() as sess:
    print(sess.run(v_add))

运行结果:

[3 3 8 7]

你可能感兴趣的:(tensorflow)