我在Go中加载了Tensorflow模型并且无法获得预测 - 它一直在抱怨形状不匹配 - 一个简单的2d数组。非常感谢这里的想法,非常感谢你。
add
正在发送的输入张量是[] [] float32 {{1.0},}
Error running the session with input, err: You must feed a value for placeholder tensor 'theoutput_target' with dtype float
[[Node: theoutput_target = Placeholder[_output_shapes=[[?,?]], dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
,模型通过Keras生成,并在使用SavedModelBuilder后导出到TF:
a := [][]float32{ {1.0}, }
tensor, terr := tf.NewTensor(a)
if terr != nil {
fmt.Printf("Error creating input tensor: %s\n", terr.Error())
return
}
result, runErr := model.Session.Run(
map[tf.Output]*tf.Tensor{
model.Graph.Operation("theinput").Output(0): tensor,
},
[]tf.Output{
model.Graph.Operation("theoutput_target").Output(0),
},
nil,
)
编辑 - 修复,从Keras导出到TF(图层名称)是一个问题。在这里粘贴出口,希望对其他人有用:
layer_name_input = "theinput"
layer_name_output = "theoutput"
def get_encoder():
model = Sequential()
model.add(Dense(5, input_dim=1))
model.add(Activation("relu"))
model.add(Dense(5, input_dim=1))
return model
inputs = Input(shape=(1, ), name=layer_name_input)
encoder = get_encoder()
model = encoder(inputs)
model = Activation("relu")(model)
objective = Dense(1, name=layer_name_output)(model)
model = Model(inputs=[inputs], outputs=objective)
model.compile(loss='mean_squared_error', optimizer='sgd')
答案 0 :(得分:0)
你的代码和错误中有些奇怪。 Tensorflow抱怨名称为“theoutput_target”的占位符缺少值,而此占位符从未在您发布的代码中定义。相反,您的代码定义了一个名为“输入”的占位符。
另外,我建议您在tensorflow API周围使用更完整且易于使用的包装器:tfgo