machinelearning-samples icon indicating copy to clipboard operation
machinelearning-samples copied to clipboard

How to reuse trained model and Prediction Function in multiple method

Open AlpeshValvi123 opened this issue 7 years ago • 2 comments

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")
}

AlpeshValvi123 avatar Nov 05 '19 14:11 AlpeshValvi123

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:

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/

CESARDELATORRE avatar Nov 05 '19 17:11 CESARDELATORRE

Closing the issue for now. Re-open if needed. Thanks,

CESARDELATORRE avatar Nov 05 '19 17:11 CESARDELATORRE