【发布时间】:2019-12-03 16:21:03
【问题描述】:
import tensorflow as tf
x1 = tf.constant([1,2,3,4])
x2 = tf.constant([5,6,7,8])
result = tf.multiply(x1, x2)
with tf.compat.v1.Session() as sess:
output = sess.run(result)
print(output)
由于我是机器学习的新手,我尝试使用 tensorflow 实现此代码,但出现以下错误:
RuntimeError:会话图为空。向图中添加操作 在调用 run() 之前
我该如何解决这个问题?
从this 线程我将代码更改为:
import tensorflow as tf
x1 = tf.constant([1,2,3,4])
x2 = tf.constant([5,6,7,8])
result = tf.multiply(x1, x2)
g = tf.Graph()
with g.as_default():
assert result.graph is g
sess = tf.compat.v1.Session(graph=g)
with tf.compat.v1.Session() as sess:
output = sess.run(result)
print(output)
它也给了我以下错误:
AttributeError: 当急切执行时,Tensor.graph 毫无意义 启用
【问题讨论】:
-
在 google colab 中运行代码可以正常工作,这是您的所有代码吗?
-
@DavidS 是的,就是这样
标签: python tensorflow