tensorflow基础enable_eager_execution和disable_eager_execution
tensorflow自从2.0后默认就开启可enable_eager_execution,开启后不会再向之前的tensorflow版本一样进行声明式编程,在这种模式下,我们就和平时普通的命令式编程一样,并且可以即时输出结果,不需要再进行调用Session,然后通过Session.run()进行结果的获取。
这也是tensorflow相比较于卡面版本的改进,也是借鉴了torch的优点,毕竟命令式编程更加符合普通人的编程习惯。
举例几个简单的代码:
d = np.arange(5)
d_tensor = tf.constant(d,dtype=tf.float32)
print(d_tensor)
输出结果为:
tf.Tensor([0. 1. 2. 3. 4.], shape=(5,), dtype=float32)
如果我们设置为disable_eager_execution,同样的代码输出为:
Tensor("Const:0", shape=(5,), dtype=float32)
如果想获得结果
d = np.arange(5)
d_tensor = tf.constant(d,dtype=tf.float32)
sess = tf.compat.v1.Session()
print(sess.run(d_tensor))
sess.close()
输出结果为:
[0. 1. 2. 3. 4.]