【发布时间】:2019-07-27 05:17:06
【问题描述】:
我最近通过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
【问题讨论】:
-
我认为您编写了一个简单的烧瓶应用程序来提供结果或使用 lambda 函数实现(如 AWS)来提供此服务。
标签: python tensorflow tensorflow-serving