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

查看:33
本文介绍了如何在会话中运行多个图形 - 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. 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 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 normal 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天全站免登陆