Save and Read Models in ML.NET

Often times you’ll be iterating on your model to try to get it to perform well with new data, so you’ll be training on it for each of those iterations. However, once you feel like you have a model that you believe is good to use, what do you do next?

In this post, we’ll go over how to save your ML.NET models so you don’t need to go through training it each time you want to use it. We will also go over how to read in your models to be used for production environments that can take advantage of them.

You can find the code on GitHub. The saving of the model code is on the BasicPipeline project, while the loading of the model is on the LoadModel project.

Saving an ML.NET Model

Saving a model is fairly simple in ML.NET. To do so you would need to create a pipeline. We’ll use the one we created in our previous post:

var dataset = MLNetUtilities.GetDataPathByDatasetName(“SalaryData.csv”);

var pipeline = new LearningPipeline

{

  new TextLoader(dataset).CreateFrom(useHeader: true, separator: ‘,’),

  new ColumnConcatenator(“Features”, “YearsExperience”),

  new GeneralizedAdditiveModelRegressor()

};

var model = pipeline.Train<SalaryData, SalaryPrediction>();

Now that we have our model trained, in order to save it just use the WriteAsync method on the model and provide the location to save it to as the parameter.

model.WriteAsync(MLNetUtilities.GetModelFilePath(“model.zip”));

In the examples, it shows to save the file as a zip file so we do the same here. We can verify, after running this, that it saves successfully.

Reading an ML.NET Model

Reading a model for prediction is just as simple as saving it. In order to load it, you would need to use the ReadAsync method on the PredictionModel class. We would still need to use the input and output classes to specify the generic method so it knows what types to load the model from.

var model = await PredictionModel.ReadAsync<SalaryData, SalaryPrediction>(modelPath);

Now that the model is loaded, we can call the Predict method on it. This method takes in the input class as a parameter.

var prediction = model.Predict(new SalaryData { YearsExperience = 8 });

This will return back a SalaryPrediction class that we specified in the earlier post and we can use that to give us a prediction value.

Console.WriteLine($“Prediction for 8 years - {prediction.PredictedSalary}”);

In this short post, we looked at how you can save your ML.NET models to the file system so they are used for production systems for predictions. We also looked at how the model can be loaded back into memory so predictions can be made on it. This process can be used to create custom client applications such as applications for the web, mobile, or desktop.

Need Help With a Machine Learning or Data Science Project?

Data Science Consulting  Data Science Training

Stay Informed

Sign up for the latest blogs, events, and insights.

We deliver solutions that accelerate the value of Azure.
Ready to experience the full power of Microsoft Azure?

Atmosera is thrilled to announce that we have been named GitHub AI Partner of the Year.

X