// Method to load single row of data to try a single prediction // You can change this code and create your own sample data here (Hardcoded or from any source) private static SentimentModelInput CreateSingleDataSample(MLContext mlContext, string dataFilePath) { // Read dataset to get a single row for trying a prediction IDataView dataView = mlContext.Data.LoadFromTextFile <SentimentModelInput>( path: dataFilePath, hasHeader: false, separatorChar: '\t', allowQuoting: false, allowSparse: false); // Here (ModelInput object) you could provide new test data, hardcoded or from the end-user application, instead of the row from the file. SentimentModelInput sampleForPrediction = mlContext.Data.CreateEnumerable <SentimentModelInput>(dataView, false) .First(); return(sampleForPrediction); }
public async Task <IActionResult> PostFile([FromBody] SentenceModel sentenceModel) { MLContext mlContext = new MLContext(); // Training code used by ML.NET CLI and AutoML to generate the model //ModelBuilder.CreateModel(); ITransformer mlModel = mlContext.Model.Load(MODEL_FILEPATH, out DataViewSchema inputSchema); var predEngine = mlContext.Model.CreatePredictionEngine <SentimentModelInput, SentimentModelOutput>(mlModel); // Create sample data to do a single prediction with it var modelInput = new SentimentModelInput(); modelInput.Sentence = sentenceModel.Sentence; // Try a single prediction var predictionResult = predEngine.Predict(modelInput); Console.WriteLine(predictionResult); return(Ok(new { predictionResult.Score, predictionResult.Prediction })); }
static void Main(string[] args) { MLContext mlContext = new MLContext(); // Training code used by ML.NET CLI and AutoML to generate the model //ModelBuilder.CreateModel(); ITransformer mlModel = mlContext.Model.Load(GetAbsolutePath(MODEL_FILEPATH), out DataViewSchema inputSchema); var predEngine = mlContext.Model.CreatePredictionEngine <SentimentModelInput, SentimentModelOutput>(mlModel); // Create sample data to do a single prediction with it SentimentModelInput sampleData = CreateSingleDataSample(mlContext, DATA_FILEPATH); // Try a single prediction SentimentModelOutput predictionResult = predEngine.Predict(sampleData); Console.WriteLine($"Single Prediction --> Actual value: {sampleData.Label} | Predicted value: {predictionResult.Prediction}"); Console.WriteLine("=============== End of process, hit any key to finish ==============="); Console.ReadKey(); }