Python 从冻结的Tensorflow估计器图中获取输入名称?

Python 从冻结的Tensorflow估计器图中获取输入名称?,python,tensorflow,Python,Tensorflow,如果图形的输入被传递到占位符中: input\u layer=tf.placeholder(tf.float32,[…],name=“inputs”) 具有此input\u层的冻结图将有一个名为“inputs”的输入节点。如何知道冻结估计图的输入节点的名称?它是模型功能的第一层吗?它是模型函数的features参数的字典键的名称吗 当我打印冻结后生成的图形def的节点时,我得到了以下候选输入层名称: # Generated by the numpy_input_fn enqueue_input/

如果图形的输入被传递到占位符中:

input\u layer=tf.placeholder(tf.float32,[…],name=“inputs”)

具有此
input\u层的冻结图将有一个名为“inputs”的输入节点。如何知道冻结估计图的输入节点的名称?它是模型功能的第一层吗?它是模型函数的features参数的字典键的名称吗

当我打印冻结后生成的图形def的节点时,我得到了以下候选输入层名称:

# Generated by the numpy_input_fn
enqueue_input/random_shuffle_queue 
random_shuffle_queue_DequeueMany/n
random_shuffle_queue_DequeueMany

# This is probably the input
inputs/shape
inputs

# More nodes here
...
更新

这是图表

更多更新

我使用保存的模型和估计器查看了指南,并得出以下代码:

input_graph_def = graph.as_graph_def(add_shapes=True)
input_layer = graph.get_operation_by_name('input_layer').outputs[0]
input_shape = input_layer.get_shape().as_list()[1:]
run_params['input_shape'] = input_shape
feature_spec = {'x': tf.FixedLenFeature(input_shape, input_layer.dtype)}

estimator = tf.estimator.Estimator(model_fn=_predict_model_fn,
                                   params=run_params,
                                   model_dir=checkpoint_dir)

def _serving_input_receiver_fn():
    return tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)()

exported_model_path = estimator.export_savedmodel(checkpoint_dir, _serving_input_receiver_fn)
但是,当我运行此操作时,遇到以下错误:

File "... my module", line ..., in ...
    exported_model_path = estimator.export_savedmodel(checkpoint_dir, _serving_inp
  File "...\tensorflow\python\estimator\estimator.py", line 598, in export_savedmodel
    serving_input_receiver.receiver_tensors_alternatives)
  File "...\tensorflow\python\estimator\export\export.py", line 199, in build_all_signature_defs
    '{}'.format(type(export_outputs)))
ValueError: export_outputs must be a dict and not<class 'NoneType'>
显然,必须在
EstimatorSpec
中提供
export\u output
参数,以便在决定导出模型时返回。这样,
\u predict\u model\u fn
就有了这个返回语句,并将参数添加到
\u create\u model\u fn

return _create_model_fn(mode, predictions=predictions,
                            export_outputs={
                                "outputs": tf.estimator.export.PredictOutput(outputs)
                            })

没有办法从图中分辨出哪一个是输入张量还是输出张量

你应该使用这些函数。它的一部分是生成模型的签名,该签名准确地表示哪个张量是输入,哪个张量是输出


您可以采用相同的模型并使用不同的签名导出它。例如,一个获取协议缓冲区并返回概率,另一个获取字符串并提供空间嵌入

浏览一下源代码和API的使用方式,它看起来不像
tf.estimator.estimator
那样使用占位符。您仍然可以使用带有
feed\u dict
传递图中任何张量的模型来替换feed的值,但我认为没有可靠的方法来获得正确的张量来替换。一个不是真正的解决方案,但可能的解决方法是,检查您是否能够理解它,以确定您需要在哪个节点中注入值。(我的意思是,我认为没有可靠的方法可以从直接冻结为
GraphDef
的估计器中获取输入张量;此外,还有关于如何保存经过训练的模型和使用SavedModel格式进行预测的指南)我确实知道
tf.estimator.estimator
不使用占位符。我只是把它们放在一些关于如何获得输入张量的比较中。在tensorboard上可视化它似乎是个好主意,我将在稍后发布图表。@RocketPingu我怀疑在预测模式下调用模型时,你不会返回任何东西。可能会添加_predic我在返回的
estimator Spec
中提供了一个
export\u output
,它起了作用。我将用代码更新我的帖子,并接受这个答案,因为它确实引导了我。在我上面给出的代码中,输入的名称是否为“x”?@RocketPingu否,您指定
tf.estimator.export.build\u解析see在您的服务输入接收器中输入输入接收器fn
。这将在图形中添加一些步骤,以获取字符串输入并将其解析为特征(包括用作x的内容)。我认为输入将是“输入示例”或类似的内容。因此,添加到图形中的占位符步骤的输入名称将是
输入示例
return _create_model_fn(mode, predictions=predictions,
                            export_outputs={
                                "outputs": tf.estimator.export.PredictOutput(outputs)
                            })