如何在一个会话中运行多个图形-Tensorflow API [英] How to run multiple graphs in a Session - Tensorflow API

查看:293
本文介绍了如何在一个会话中运行多个图形-Tensorflow API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tensorflow API 提供了一些预训练的模型,并允许我们用任何数据集训练他们.

Tensorflow API has provided few pre-trained models and allowed us to trained them with any dataset.

我想知道如何在一个tensorflow会话中初始化和使用多个图形.我想在两个图中导入两个训练有素的模型,并利用它们进行对象检测,但是我迷失于试图在一个会话中运行多个图的问题.

I would like to know how to initialize and use multiple graphs in one tensorflow session. I want to import two trained models in two graphs and utilize them for object detection, but I am lost in trying to run multiple graphs in one session.

有没有一种特定的方法可以在一个会话中处理多个图形?

Is there any particular method to work with multiple graphs in one session?.

另一个问题是,即使我为2个不同的图形创建了两个不同的会话并尝试使用它们,最终还是在第一个实例化会话的第二个中获得了相似的结果.

Another issue is, even if I create two different sessions for 2 different graphs and try to work with them, I end up getting similar result in the second one as of first instantiated session .

推荐答案

每个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).

这篇关于如何在一个会话中运行多个图形-Tensorflow API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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