Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从保存的Graph.pb获取会话的图形对象_Python_Tensorflow - Fatal编程技术网

Python 如何从保存的Graph.pb获取会话的图形对象

Python 如何从保存的Graph.pb获取会话的图形对象,python,tensorflow,Python,Tensorflow,我已经创建了一个tensorflow图。例如,我可以用 with tf.gfile.FastGFile(modelFullPath, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') 它将protobuffer文件中定义的图形作为当前的默认图形。如果我现在创建一个会话,此图将用作当前图

我已经创建了一个tensorflow
。例如,我可以用

with tf.gfile.FastGFile(modelFullPath, 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')
它将protobuffer文件中定义的图形作为当前的默认图形。如果我现在创建一个会话,此图将用作当前图

尝试将序列化的
graph_def
对象保存到变量中,并按如下方式启动
会话

with tf.Session(graph=graph_def) as sess:
以预期的错误结束

TypeError: graph must be a tf.Graph, but got <class 'tensorflow.core.framework.graph_pb2.GraphDef'>

和创建会话,而无需从
graph.pb
文件重新加载图形?

您可以创建自己的图形,并将其设置为导入操作的默认值:

将tensorflow导入为tf
graph1=tf.Graph()
graph2=tf.Graph()
使用graph1.as_default():
tf.import_graph_def(graph_def1)#graph_def1加载到某处
使用graph2.as_default():
tf.import_graph_def(graph_def2)#graph_def2加载到某处
session1=tf.Session(图=graph1)
session2=tf.Session(图=graph2)

我如何从pb文件创建自己的图形?您能告诉我您是如何做到这一点的吗?@haramahadevaki正如公认的答案所建议的那样。谢谢。。。速度有点慢…有什么办法可以提高速度吗?
with tf.Session(graph=my_graph) as sess: