2016-12-19 124 views
3

我想添加一个分支到以前创建的张量流图。我是按照mrry对这个问题的回答(Tensorflow: How to replace a node in a calculation graph?)做的,我保存了新图的定义。合并张量流图时如何保留占位符名称?

当我导入新图并尝试获取原始图的占位符时,出现以下错误:ValueError: Requested return_element 'pool_3/_reshape:0' not found in graph_def.,但代码在我使用原始图时正常工作。

我如何保持原来的占位符

我的代码引用合并这两个图是

with tf.Session() as sess: 

# Get the b64_graph and its output tensor 
resized_b64_tensor, = (tf.import_graph_def(b64_graph_def, name='', 
      return_elements=[B64_OUTPUT_TENSOR_NAME+":0"])) 

with gfile.FastGFile(model_filename, 'rb') as f: 
    inception_graph_def = tf.GraphDef() 
    inception_graph_def.ParseFromString(f.read()) 

    # Concatenate b64_graph and inception_graph 
    g_1 = tf.import_graph_def(inception_graph_def, name='graph_name', 
       input_map={RESIZED_INPUT_TENSOR_NAME : resized_b64_tensor}) 

    # Save joined graph 
    joined_graph = sess.graph 
    with gfile.FastGFile(output_graph_filename, 'wb') as f: 
    f.write(joined_graph.as_graph_def().SerializeToString()) 
+0

解决:我忘了“graph_name /”添加到张量名称的新图。现在它可以工作 – EffePi

+0

你可以把问题标记为已回答吗? –

回答

3

我已经通过阅读这篇文章Working with multiple graphs in TensorFlow找到了解决办法是间接的。

当图形有一个名称分配时,它被附加到张量的名称和它包含的操作上。特别是,如果我将两个图形连接成一个新图形,则后者的名称会附加到以前的名称上。 因此正确的代码,以获得张量本来

sess.graph.get_tensor_by_name('graph_name/' + 'PreviousGraphName/PreviousTensorName:0')