How to reuse trained model and Prediction Function in multiple method
How to reused train model and PredictionFunction in multiple method? Please look at below eg.
Method1(){
MLContext mlContext = new MLContext();
//step1
TrainTestData splitDataView = LoadData(mlContext);
//step2
ITransformer model = BuildAndTrainModel(mlContext, splitDataView.TrainSet);
Evaluate(mlContext, model, splitDataView.TestSet);
//step3
PredictionEngine<SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);
//step4
SentimentData sampleStatement = new SentimentData
{
SentimentText = comment
};
//step5
var resultPrediction = predictionFunction.Predict(sampleStatement);
//step6
result = (Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")
}
Method2(){
//How to re-use step 1, 2, 3
//step4
SentimentData sampleStatement = new SentimentData
{
SentimentText = comment
};
//step5
var resultPrediction = predictionFunction.Predict(sampleStatement);
//step6
result = (Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")
}
A trained model (ITransformer) is thread safe, so it can even be singleton or static and you can re-use it. However, the PredictionEngine is NOT thread safe, so it shouldn't be re-used across different threads. That's why we created the PredictionEnginePool you must use in multi-thread apps when using .NET Core like ASP.NET Core apps.
For further details on how to deploy a model with the PredictionEnginePool, read the following resources: Tutorials:
- Deploy a model into an ASP.NET Core Web API
- Deploy a model into an Azure Function
- Sample App: ASP.NET Core Web API using an ML.NET model
For further background information on why the PredictionEnginePool is recommended, read this blog post which was the origin of our PredictionEnginePool: https://devblogs.microsoft.com/cesardelatorre/how-to-optimize-and-run-ml-net-models-on-scalable-asp-net-core-webapis-or-web-apps/
Closing the issue for now. Re-open if needed. Thanks,