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

Jonathan Wood

Recent Posts

8-Step AWS to Microsoft Azure Migration Strategy

Microsoft Azure and Amazon Web Services (AWS) are two of the most popular cloud platforms.…

6 days ago

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

2 weeks ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

4 weeks ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

1 month ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

1 month ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

2 months ago