python - 在 tensorflow2.3.0 上急切执行
问题描述
我正在上 TensorFlow 的初学者课程。我安装的版本是2.3.0。由于课程的 TensorFlow 版本与我安装的不同,因此我遇到了急切执行的问题。有谁知道我如何执行急切的执行?
举个例子,
import tensorflow.compat.v1 as tf
x = tf.constant([3,5,7])
y = tf.constant([1,2,3])
z1 = tf.add(x,y)
z2 = x*y
z3 = z2-z1
print(z2)
with tf.compat.v1.Session() as sess:
a1, a2, a3 = sess.run([z1,z2,z3])
print(a1)
print(a2)
print(a3)
我得到作为急切执行 Tensor("mul_6:0", shape=(3,), dtype=int32) 的输出
解决方案
如果您想急切执行 - 以常规方式导入 tf,而不是tensorflow.compat.v1
. 那么根本不需要使用session
。只需输入公式并打印结果:
import tensorflow as tf
x = tf.constant([3,5,7])
y = tf.constant([1,2,3])
z1 = tf.add(x,y)
z2 = x*y
z3 = z2-z1
print(z1)
print(z2)
print(z3)
tf.Tensor([ 4 7 10], shape=(3,), dtype=int32)
tf.Tensor([ 3 10 21], shape=(3,), dtype=int32)
tf.Tensor([-1 3 11], shape=(3,), dtype=int32)
推荐阅读
- android - 当我的应用程序处于调试状态时,一个好的做法是否有一个不检查安全问题的参数?
- sql - SQL json_array_get - 搜索对象而不是位置
- bash - 当我重新安装 postgresql helm 图表时,我的数据库不断被删除
- symfony5 - 创建一个服务只是为了帮助您的测试?
- php - Monolog : 大文件加载问题
- installation - 你如何构建所有的 LLVM?
- css - CSS 不适用于帖子页面,但适用于其他页面
- javascript - 在 Material UI 中访问页面大小
` - python - 有没有办法将这些 python 数据框修改组合成一行
- reactjs - 如何模拟两次开玩笑的函数