使用 SignatureDefs 为 TensorFlow Serving 保存模型,其中 api 端点映射到特定方法?

Save a model for TensorFlow Serving with api endpoint mapped to certain method using SignatureDefs?

我最近经历了 this tutorial。我有教程中的训练模型,我想用 docker 为它提供服务,这样我就可以向它发送任意字符串并从模型中获取预测结果。

我还通过 this tutorial 了解如何使用 docker 服务。但是我不明白模型是如何保存的并具有接受输入参数的能力。例如:

    curl -d '{"instances": [1.0, 2.0, 5.0]}' \
        -X POST http://localhost:8501/v1/models/half_plus_two:predict

half_plus_two 模型如何知道如何处理 instances 参数?

在文本生成教程中,有一个名为 generate_text 的方法可以处理生成预测。

    def generate_text(model, start_string):
        # Evaluation step (generating text using the learned model)

        # Number of characters to generate
        num_generate = 1000

        # Converting our start string to numbers (vectorizing) 
        input_eval = [char2idx[s] for s in start_string]
        input_eval = tf.expand_dims(input_eval, 0)

        # Empty string to store our results
        text_generated = []

        # Low temperatures results in more predictable text.
        # Higher temperatures results in more surprising text.
        # Experiment to find the best setting.
        temperature = 1.0

        # Here batch size == 1
        model.reset_states()
        for i in range(num_generate):
            predictions = model(input_eval)
            # remove the batch dimension
            predictions = tf.squeeze(predictions, 0)

            # using a multinomial distribution to predict the word returned by the model
            predictions = predictions / temperature
            predicted_id = tf.multinomial(predictions, num_samples=1)[-1,0].numpy()

            # We pass the predicted word as the next input to the model
            # along with the previous hidden state
            input_eval = tf.expand_dims([predicted_id], 0)

            text_generated.append(idx2char[predicted_id])

        return (start_string + ''.join(text_generated)) 

如何为文本生成教程中的训练模型提供服务,并将模型 api 的输入参数映射到独特的方法,例如 generate_text?例如:

    curl -d '{"start_string": "ROMEO: "}' \
        -X POST http://localhost:8501/v1/models/text_generation:predict

注意:完整而广泛地回答这个问题需要深入研究服务架构、它的 APIs 以及它们如何与模型的签名交互。我将跳过所有这些以使答案的长度保持在可接受的范围内,但如有必要,我总是可以扩展过于晦涩的部分(如果是这种情况,请发表评论)。

How does the half_plus_two model know what to do with the instances param?

由于几个未提及的原因堆积在一起,使这个例子很方便,如果只是 IMO 有点误导的话。

1) instances参数从何而来?Predict API对RESTful的定义API具有预定义的请求格式,以其两种可能形式之一采用一个 instances 参数。

2) instances 参数映射到什么? 我们不知道。对于只有一个输入的 SignatureDefs,instances 在非常具体的调用格式中 直接映射到输入而无需指定输入的密钥 (see section "Specifying input tensors in row format" in the API specs)。

因此,发生的情况是:您向仅定义了一个输入的模型发出 POST 请求。 TF Serving 获取该输入并将其提供给模型,运行它直到它具有模型签名的 "outputs" 部分中定义的张量的所有值,并且 returns 你是一个 JSON 对象"outputs" 列表中的每个键都有 key:result 项。

How can I serve the trained model from the text generation tutorial and have input parameters to the model api mapped to unique methods such as generate_text?

你不能(至少不能直接将函数映射到服务方法)。 Serving 基础架构公开了一些预定义方法(regresspredictclassify),这些方法知道如何解释签名以生成模型的 运行 特定子图所请求的输出.这些子图必须包含在 SavedModel 中,因此例如使用 tf.py_func 将不起作用。

你最好的机会是尝试将文本生成描述为 TF 子图(即仅使用 TF 操作)并编写一个单独的 SignatureDef,它将起始字符串和 num_generate 作为输入。