且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在一个会话中运行多个图形-Tensorflow API

更新时间:2023-12-02 20:33:40

每个Session只能有一个Graph.话虽如此,根据您的具体尝试,您有两种选择.

Each Session can only have a single Graph. That being said, depending on what you're specifically trying to do, you have a couple options.

第一种选择是创建两个单独的会话,并将一个图形加载到每个会话中,如中所述这里的文档.您提到使用这种方法从每个会话中都获得了意想不到的相似结果,但是如果没有更多细节,则很难弄清楚具体情况是什么问题.我怀疑是将相同的图形加载到每个会话中,还是当您尝试分别运行每个会话时,同一会话被运行了两次,但是没有更多详细信息,这很难说.

The first option is to create two separate sessions and load one graph into each session, as explained in the documentation here. You mentioned you were getting unexpectedly similar results from each session with that approach, but without more details it's hard to figure out what the problem is in your case specifically. I would suspect either the same graph was loaded to each session or when you try to run the each session separately the same session is being run twice, but without more details it's hard to tell.

第二个选项是将两个图都加载为主会话图的子图.您可以在图内创建两个范围,并为要在该范围内加载的每个图构建图.然后,您可以将它们视为独立的图,因为它们之间没有任何联系.当运行正常的图形全局函数时,您需要指定这些函数适用的范围.例如,当使用优化器对子图之一进行更新时,您将需要使用

The second option is to load both graphs as subgraphs of the main session graph. You can create two scopes within the graph, and build the graph for each of the graphs you want to load within that scope. Then you can just treat them as independent graphs since there are no connections between them. When running normally graph global functions, you'll need to specify which scope those functions are applying to. For example, when preforming an update on one of the subgraphs with its optimizer, you'll need to get only the trainable variables for that subgraph's scope using something like what is shown in this answer.

除非您明确需要这两个图能够在TensorFlow图内以某种方式进行交互,否则我建议采用第一种方法,这样您就无需跳过子图所需的额外箍(例如需要过滤您在任何给定时刻使用的范围,以及图全局事物在两者之间共享的可能性.

Unless you explicitly need the two graphs to be able to interact in someway within the TensorFlow graph, I would recommend the first approach so that you don't need to jump through the extra hoops having the subgraphs will require (such as needing to filter which scope your working with at any given moment, and the possibility of graph global things being shared between the two).