首页 > 解决方案 > ML.Net PredictionEnginePool 和多输入

问题描述

我环顾四周,似乎推荐的在 API 服务中实现预测引擎的方法是使用 PredictionEnginePool。我目前在 ConfigureServices() 中有这样的设置。

.ConfigureServices(services => {
     services.AddPredictionEnginePool<Input, Output>()
     .FromFile("TrainedModels/my_model.zip");
     })

并像这样消费:

var predEngine = http.RequestServices.GetRequiredService<PredictionEnginePool<Input, Output>>();            
var prediction = predEngine.Predict(input);

现在我需要的是允许我的端点使用数组数据输入。到目前为止,我所看到的是通过使用ML.Net Multiple Predictions中的管道和 IDataview/transforms

IDataView predictions = predictionPipeline.Transform(inputData);

但是,在我没有管道的情况下使用 PredictionEnginePool 时如何做到这一点?任何想法表示赞赏,应该还有其他人经历过这个。谢谢!

标签: c#ml.net

解决方案


这是您的处理程序在使用 PredictionEnginePool 进行多个预测时的样子。

static async Task PredictHandler(HttpContext http)
{
    var predEnginePool = http.RequestServices.GetRequiredService<PredictionEnginePool<Input,Output>>();

    var input = await JsonSerializer.DeserializeAsync<IEnumerable<Input>>(http.Request.Body);

    var response = input.Select((x) => predEnginePool.Predict(x));

    await http.Response.WriteAsJsonAsync(response);
}

假设您的请求正文采用LINQ操作IEnumerable<Input>,请应用该Predict方法。在这种情况下,将是一个.PredictionEnginePoolSelectresponseIEnumerable<Output>


推荐阅读