【问题标题】:How to feed input string with shape=[?] to tensorflow model using golang如何使用 golang 将 shape=[?] 的输入字符串提供给 tensorflow 模型
【发布时间】:2017-10-23 23:24:13
【问题描述】:

训练模型的Python代码:

input_schema = dataset_schema.from_feature_spec({
    REVIEW_COLUMN: tf.FixedLenFeature(shape=[], dtype=tf.string),
    LABEL_COLUMN: tf.FixedLenFeature(shape=[], dtype=tf.int64)
})

在 python 中,预测工作正常。客户端示例:

loaded_model = tf.saved_model.loader.load(sess, ["serve"], '/tmp/model/export/Servo/1506084916')
input_dict, output_dict =_signature_def_to_tensors(loaded_model.signature_def['default_input_alternative:None'])
start = datetime.datetime.now()
out = sess.run(output_dict, feed_dict={input_dict["inputs"]: ("I went and saw this movie last night",)})
print(out)
print("Time all: ", datetime.datetime.now() - start)

但是golang客户端不行:

m, err := tf.LoadSavedModel("/tmp/model/export/Servo/1506084916", []string{"serve"}, &tf.SessionOptions{})
if err != nil {
    panic(fmt.Errorf("load model: %s", err))
}

data := "I went and saw this movie last night"
t, err := tf.NewTensor([]string{data})
if err != nil {
    panic(fmt.Errorf("tensor err: %s", err))
}
fmt.Printf("tensor: %v", t.Shape())

output, err = m.Session.Run(
    map[tf.Output]*tf.Tensor{
        m.Graph.Operation("save_1/StringJoin/inputs_1").Output(0): t,
    }, []tf.Output{
        m.Graph.Operation("linear/binary_logistic_head/predictions/classes").Output(0),
    }, nil,
)
if err != nil {
    panic(fmt.Errorf("run model: %s", err))
}

我收到错误:

恐慌:运行模型:您必须为占位符张量提供一个值 带有 dtype 字符串和形状的“占位符”[?] [[节点:占位符=Placeholder_output_shapes=[[?]],dtype=DT_STRING,shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]]

如何使用 golang 呈现 shape=[?] 张量?或者我需要更改python训练脚本的输入格式?

更新:

这个字符串"save_1/StringJoin/inputs_1"在运行这个python代码后收到:

for n in sess.graph.as_graph_def().node:
    if "inputs" in n.name:
        print(n.name)

输出:

transform/transform/inputs/review/Placeholder 
transform/transform/inputs/review/Identity 
transform/transform/inputs/label/Placeholder 
transform/transform/inputs/label/Identity 
transform/transform_1/inputs/review/Placeholder 
transform/transform_1/inputs/review/Identity 
transform/transform_1/inputs/label/Placeholder 
transform/transform_1/inputs/label/Identity 
save_1/StringJoin/inputs_1 
save_2/StringJoin/inputs_1

【问题讨论】:

  • 你确定save_1/StringJoin/inputs_1input_dict["inputs"]是同一个值吗?
  • 我用关于“save_1/StringJoin/inputs_1”的信息更新了主题。我尝试了其他键,但结果相同 - 错误。
  • 你能显示print(input_dict["inputs"])的输出吗?
  • 所有输入字典:{u'inputs': <tf.Tensor 'Placeholder:0' shape=(?,) dtype=string>}
  • Hm.. 如果你将 Go feed dict 从 ` m.Graph.Operation("save_1/StringJoin/inputs_1").Output(0): t,` 更改为 ` m.Graph 会发生什么.Operation("Placeholder").Output(0): t,` ?

标签: go tensorflow tensorflow-serving


【解决方案1】:

错误告诉您You must feed a value for placeholder tensor 'Placeholder':这意味着在您为该占位符输入值之前无法构建图表。

在您的 python 代码中,您在以下行输入它:

input_dict["inputs"]: ("I went and saw this movie last night",)

事实上,input_dict["inputs"] 被评估为:<tf.Tensor 'Placeholder:0' shape=(?,) dtype=string>

相反,在您的 Go 代码中,您正在寻找一个名为 save_1/StringJoin/inputs_1 的张量,它不是占位符。

要遵循的规则是:在 Python 和 Go 中使用相同的输入。

因此,要解决此问题,您只需从图中提取名为 Placeholder 的占位符(就像在 python 中一样)然后使用它。

m.Graph.Operation("Placeholder").Output(0): t,

另外,我建议你使用更完整、更易于使用的 tensorflow API 包装器:tfgo

【讨论】:

    【解决方案2】:

    还有一件事。我阅读了 TF 文档并找到了这个 topic

    有助于找到正确的输入/输出键,响应示例:

    The given SavedModel SignatureDef contains the following input(s): inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: Placeholder:0
    

    PS/作为关注的答案发布

    【讨论】:

      猜你喜欢
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      相关资源
      最近更新 更多