如何使用golang将shape=[?]的输入字符串馈送到tensorflow模型
列车模型Python代码:如何使用golang将shape=[?]的输入字符串馈送到tensorflow模型,go,tensorflow,tensorflow-serving,Go,Tensorflow,Tensorflow Serving,列车模型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.
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))
}
我得到一个错误:
恐慌:运行模型:必须为占位符张量输入一个值
带有数据类型字符串和形状[?]的“占位符”
[[Node:Placeholder=Placeholder\u output\u shapes=[[?]],dtype=DT\u STRING,shape=[?],
_device=“/job:localhost/replica:0/task:0/cpu:0”]]
如何用golang表示shape=[?]
tensor?或者我需要更改python培训脚本的输入格式
UPD:
这个字符串“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
该错误告诉您
必须为占位符张量“占位符”输入一个值。
:这意味着只有为该占位符输入一个值,才能生成图形
在python代码中,在以下行输入:
input_dict["inputs"]: ("I went and saw this movie last night",)
实际上,input\u dict[“inputs”]
的计算结果是:
相反,在Go代码中,您正在寻找一个名为save_1/StringJoin/inputs_1
的张量,它不是占位符
遵循的规则是:在Python和Go中使用相同的输入
因此,要解决这个问题,您只需从图中提取名为占位符的占位符(就像python中的占位符一样),然后使用它
m.Graph.Operation("Placeholder").Output(0): t,
另外,我建议您在tensorflow API周围使用一个更完整、更易于使用的包装器:错误告诉您必须为占位符tensor'placeholder'输入一个值。
:这意味着在为占位符输入一个值之前,无法构建图形
在python代码中,在以下行输入:
input_dict["inputs"]: ("I went and saw this movie last night",)
实际上,input\u dict[“inputs”]
的计算结果是:
相反,在Go代码中,您正在寻找一个名为save_1/StringJoin/inputs_1
的张量,它不是占位符
遵循的规则是:在Python和Go中使用相同的输入
因此,要解决这个问题,您只需从图中提取名为占位符的占位符(就像python中的占位符一样),然后使用它
m.Graph.Operation("Placeholder").Output(0): t,
另外,我建议您在tensorflow API周围使用一个更完整、更易于使用的包装器:还有一件事。我读了TF文档,发现了这个
它有助于找到正确的输入/输出键,例如响应:
The given SavedModel SignatureDef contains the following input(s): inputs['inputs'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: Placeholder:0
PS/作为聚焦的答案发布还有一件事。我读了TF文档,发现了这个
它有助于找到正确的输入/输出键,例如响应:
The given SavedModel SignatureDef contains the following input(s): inputs['inputs'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: Placeholder:0
PS/作为聚焦的答案发布您确定保存1/StringJoin/inputs\u 1
与输入dict[“inputs”]
相同吗?我用“保存1/StringJoin/inputs\u 1”的信息更新了主题。我尝试了其他键,但结果是相同的-错误。能否显示打印的输出(input_dict[“inputs”])
?所有input_dict:{u'inputs':}
Hm。。如果将Go-feed dict从'm.Graph.Operation(“save_1/StringJoin/inputs_1”).Output(0):t,`更改为'm.Graph.Operation(“占位符”).Output(0):t,`,会发生什么?您确定save_1/StringJoin/inputs_1
与input_-dict[“inputs”]
的值相同吗?我用关于“save_1/StringJoin/inputs_1”的信息更新了主题。我尝试了其他键,但结果是相同的-错误。能否显示打印的输出(input_dict[“inputs”])
?所有input_dict:{u'inputs':}
Hm。。如果将Go-feed dict从`m.Graph.Operation(“save_1/StringJoin/inputs_1”).Output(0):t`更改为`m.Graph.Operation(“占位符”).Output(0):t,`,会发生什么?