c# - 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 时如何做到这一点?任何想法表示赞赏,应该还有其他人经历过这个。谢谢!
解决方案
这是您的处理程序在使用 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
方法。在这种情况下,将是一个.PredictionEnginePool
Select
response
IEnumerable<Output>
推荐阅读
- entity-framework - 实体框架:无法将找到的对象分配给我的财产
- java - 对不适用于 PDF 的应用程序的隐含意图
- python - 从 2d 创建 3 通道蒙版的 numpy 方法
- python - python shell命令管道grep没有结果
- java - RXJava2中如何每15秒重复一次操作以及如何启动和停止
- php - php解析字符串而不解码
- c# - 如何使用多个可选参数编写 Linq To Entity
- php - Mysqli_real_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (使用密码: NO)
- python - 如何使用 Python 数据类记录类的构造函数?
- java - NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'