tensorflow基础enable_eager_execution和disable_eager_execution

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.]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值