doulai5585 2017-09-23 09:32
浏览 79
已采纳

如何使用golang将shape = [?]的输入字符串馈送到tensorflow模型

Python code of train model:

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

In python predictions work fine. Example of client:

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)

But golang client does not work:

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))
}

I got error:

panic: run model: You must feed a value for placeholder tensor 'Placeholder' with dtype string and shape [?] [[Node: Placeholder = Placeholder_output_shapes=[[?]], dtype=DT_STRING, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]]

How can I present shape=[?] tensor with golang? Or I need to change input format of python training script?

UPD:

This string "save_1/StringJoin/inputs_1" I received after running this python-code:

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

Output:

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
  • 写回答

2条回答 默认 最新

  • douping1993 2017-09-25 10:01
    关注

    The error tells you You must feed a value for placeholder tensor 'Placeholder': this means that the graph can't be built until you feed a value for that placeholder.

    In your python code, your feed it at the line:

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

    In fact, input_dict["inputs"] is evaluated to: <tf.Tensor 'Placeholder:0' shape=(?,) dtype=string>.

    In your Go code, instead, you're looking for a tensor named save_1/StringJoin/inputs_1 that's not the placeholder.

    The rule to follow is: use the same input both in Python & Go.

    To solve the problem, thus, you just have to extract from the graph the placeholder named Placeholder (just like in python) and then use it.

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

    Also, I suggest you to use a more complete and easy to use wrapper around the tensorflow API: tfgo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。