首页 文章

在Go中加载Tensorflow模型时无法预测

提问于
浏览
0

我在Go中加载了Tensorflow模型并且无法获得预测 - 它一直在抱怨形状不匹配 - 一个简单的2d数组 . 在此感谢您的想法,非常感谢您提前 .

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"]()]]

发送的输入张量是[] [] float32 {{1.0},}

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生成的,并在使用SavedModelBuilder导出到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')

编辑 - 修复,从Keras导出到TF(图层名称)是一个问题 . 在这里粘贴出口,希望对其他人有帮助:

def export_to_tf(keras_model_path, export_path, export_version, is_functional=False):

    sess = tf.Session()
    K.set_session(sess)
    K.set_learning_phase(0)

    export_path = os.path.join(export_path, str(export_version))

    model = load_model(keras_model_path)
    config = model.get_config()
    weights = model.get_weights()
    if is_functional == True:
        model = Model.from_config(config)
    else:
        model = Sequential.from_config(config)
    model.set_weights(weights)

    with K.get_session() as sess:
        inputs = [ (model_input.name.split(":")[0], model_input) for model_input in model.inputs]
        outputs = [ (model_output.name.split(":")[0], model_output) for model_output in model.outputs]
        signature = predict_signature_def(inputs=dict(inputs),
                                      outputs=dict(outputs))
        input_descriptor = [ { 'name': item[0], 'shape': item[1].shape.as_list() } for item in inputs]
        output_descriptor = [ { 'name': item[0], 'shape': item[1].shape.as_list() } for item in outputs]
        builder = saved_model_builder.SavedModelBuilder(export_path)
        builder.add_meta_graph_and_variables(
            sess=sess,
            tags=[tag_constants.SERVING],
            signature_def_map={signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature})
        builder.save()

        descriptor = dict()
        descriptor["inputs"] = input_descriptor
        descriptor["outputs"] = output_descriptor
        pprint.pprint(descriptor)

1 回答

  • 0

    你的代码和错误中有些奇怪 . Tensorflow抱怨名称为“theoutput_target”的占位符缺少值,而此占位符从未在您发布的代码中定义 . 相反,您的代码定义了一个名为“theinput”的占位符 .

    另外,我建议您在tensorflow API周围使用更完整且易于使用的包装器:tfgo

相关问题