会话图是空的

ISh*_*Sha 4 python-3.x tensorflow

我一直在尝试使用Tensorflow提供的答案迭代tensorflow模型 :即使在关闭Session时内存泄漏? 我有一个注释中提到的相同错误,即"会话图表为空.在调用run()之前向图表添加操作." 我无法弄清楚如何解决它.

Xi *_*Zhu 8

我的建议是确保您的会话知道它正在运行哪个图表.你可以尝试的事情是:

  1. 首先构建图形并将图形传递给会话.

    myGraph = tf.Graph()
    with myGraph.as_default():
        # build your graph here
    
    mySession = tf.Session(graph=myGraph)
    mySession.run(...)
    
    # Or
    
    with tf.Session(graph=myGraph) as mySession:
        mySession.run(...)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果要使用多个with语句,请以嵌套方式使用它.

    with tf.Graph().as_default():
        # build your graph here
        with tf.Session() as mySession:
            mySession.run(...)
    
    Run Code Online (Sandbox Code Playgroud)