我已经使用 ml.net 模型构建器创建了一个 MLModel,它可以与“使用”控制台应用程序和硬编码值一起使用。我想进入下一步并使用 web api 提供值。我正在使用他们在 web api 中使用 mlmodel 的教程示例的修改版本。当我将邮递员与 webapi 一起使用时,出现以下错误;
System.InvalidOperationException:'无法将类型为'Single'的映射应用于列'PG',因为它的类型为'String''
mlmodel 从模型构建器成功构建,并且默认代码是从模型构建器生成的。在模型上运行带有硬编码值的消费控制台应用程序是可行的。
这是modelinput的相关代码
//************************************************ ***************************************** //* * //* 这是Microsoft ML.NET CLI(命令行界面)工具自动生成的文件。* //* * //************************************************ ************************************************
using Microsoft.ML.Data;
namespace JobClassMLModelML.Model.DataModels
{
public class ModelInput
{
[ColumnName("EducationLevel"), LoadColumn(0)]
public float EducationLevel { get; set; }
[ColumnName("Experience"), LoadColumn(1)]
public float Experience { get; set; }
[ColumnName("OrgImpact"), LoadColumn(2)]
public float OrgImpact { get; set; }
[ColumnName("ProblemSolving"), LoadColumn(3)]
public float ProblemSolving { get; set; }
[ColumnName("Supervision"), LoadColumn(4)]
public float Supervision { get; set; }
[ColumnName("ContactLevel"), LoadColumn(5)]
public float ContactLevel { get; set; }
[ColumnName("FinancialBudget"), LoadColumn(6)]
public float FinancialBudget { get; set; }
[ColumnName("PG"), LoadColumn(7)]
public string PG { get; set; }
}
}
这里是模型输出
//************************************************ ***************************************** //* * //* 这是Microsoft ML.NET CLI(命令行界面)工具自动生成的文件。* //* * //************************************************ ************************************************
using System;
using Microsoft.ML.Data;
namespace JobClassMLModelML.Model.DataModels
{
public class ModelOutput
{
// ColumnName attribute is used to change the column name from
// its default value, which is the name of the field.
[ColumnName("PredictedLabel")]
public String Prediction { get; set; }
public float[] Score { get; set; }
}
}
我分别在jobclassdata和jobclassprediction的等效webapi类中克隆了modelinput和modeloutput类
有效的控制台应用程序:
using System;
using Microsoft.ML;
using JobClassMLModelML;
using JobClassMLModelML.Model.DataModels;
namespace ConsumeApp
{
class Program
{
static void Main(string[] args)
{ // Load the model
MLContext mlContext = new MLContext();
ITransformer mlModel = mlContext.Model.Load("MLModel.zip", out
var modelInputSchema);
var predEngine =
mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
// Use the code below to add input data
var input = new ModelInput();
// input.
input.ContactLevel = 5;
input.EducationLevel = 5;
input.Experience = 5;
input.FinancialBudget = 5;
input.OrgImpact = 5;
input.ProblemSolving = 5;
input.Supervision =5;
//input.PG = "PG01";
// Try model on sample data
ModelOutput result = predEngine.Predict(input);
Console.WriteLine($"Predicted value: {result.Prediction} ");
}
}
}
这是预测控制器的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.ML;
using JobClassMLModelWebAPI.DataModels;
namespace JobClassMLModelWebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PredictController : ControllerBase
{
private readonly PredictionEnginePool<JobClassData,
JobClassPrediction> _predictionEnginePool;
public PredictController(PredictionEnginePool<JobClassData,
JobClassPrediction> predictionEnginePool)
{
_predictionEnginePool = predictionEnginePool;
}
[HttpGet]
public JobClassData Get(JobClassData jobClassData)
{
return new JobClassData
{
// insert test data here
};
}
[HttpPost]
public ActionResult<string> Post([FromBody] JobClassData input)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
JobClassPrediction prediction =
_predictionEnginePool.Predict(input);
string PredictedJobGrade = prediction.Prediction;
return Ok(PredictedJobGrade);
}
}
}
这是发布的json正文代码
{
"educationLevel": 3,
"experience": 3,
"orgImpact": 3,
"problemSolving": 3,
"supervision": 3,
"contactLevel": 4,
"financialBudget": 4,
"pg" : "PG01"
}
预期结果将是预测的工资等级,例如 PG03。相反,错误是
System.InvalidOperationException:'无法将类型为'Single'的映射应用于列'PG',因为它的类型为'String''
在线
JobClassPrediction prediction =
_predictionEnginePool.Predict(input);
没有编译错误并且控制台应用程序正常工作是json输入的格式吗?
谢谢