dotnet / machinelearning-samples

Samples for ML.NET, an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
4.5k stars 2.69k forks source link

How to reuse trained model and Prediction Function in multiple method #736

Closed AlpeshValvi123 closed 5 years ago

AlpeshValvi123 commented 5 years ago

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")
}
CESARDELATORRE commented 5 years ago

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 commented 5 years ago

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