我用ml.net模型构建器创建了MLModel,它与“消费”控制台应用程序和用于输入的硬编码值一起使用。我想转到下一步并使用Web API馈送值。我正在使用在Web api中使用mlmodel的教程示例的修改版本。当我将邮递员与webapi一起使用时,出现以下错误;
System.InvalidOperationException:由于列类型为'String','无法将类型'Single'上的映射应用于列'PG'
mlmodel从模型构建器成功构建,并且默认代码从模型构建器生成。在模型上运行带有硬编码值的消费控制台应用程序即可。
这是模型输入的相关代码
// ********************************************* ************************************************ // * * // *这是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:由于列类型为'String','无法将类型'Single'上的映射应用于列'PG'
在线
JobClassPrediction prediction =
_predictionEnginePool.Predict(input);
没有编译错误并且使用控制台应用程序时,json输入的格式是什么?
谢谢