Tensorflow:会话图是空的.Python [英] Tensorflow: The Session graph is empty. Python

查看:51
本文介绍了Tensorflow:会话图是空的.Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我使用的是 Tensorflow 2.0

在这些代码行中:

 将 tensorflow 导入为 tf你好 = tf.constant('你好世界')sess = tf.compat.v1.Session()sess.run(hello) <-- 这一行出错

<块引用>

运行时错误:会话图为空.向图中添加操作在调用 run() 之前.

知道如何解决吗?

解决方案

好的,我找到了方法:

g = tf.Graph()使用 g.as_default():# 在 `g` 中定义操作和张量.你好 = tf.constant('你好')断言 hello.graph 是 gsess = tf.compat.v1.Session(graph=g)sess.run(你好)

<块引用>

b'你好'

感谢您抽出宝贵时间!

Hello guys I am using Tensorflow 2.0

and in these lines of code:

import tensorflow as tf
hello = tf.constant('Hello World')
sess = tf.compat.v1.Session()
sess.run(hello) <-- Error in this line

RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

Any idea on how to solve it?

解决方案

ok guys I found the way:

g = tf.Graph() 
with g.as_default():   
  # Define operations and tensors in `g`.   
  hello = tf.constant('hello')   
  assert hello.graph is g

sess = tf.compat.v1.Session(graph=g) 
sess.run(hello)

b'hello'

thank you for your time!

这篇关于Tensorflow:会话图是空的.Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆