#MLNET – Write and Load models using Machine Learning .Net

Hi !

Today post fast that the code is self explanatory.

First, based on the previous posts, the process of defining and training the model. The important thing happens on lines 15 and 16, where the model is recorded.

class Program
{
static void Main(string[] args)
{
var agesRangesCsv = "AgeRangeData.csv";
var pipeline = new LearningPipeline();
pipeline.Add(new TextLoader<AgeRangeData>(agesRangesCsv, separator: ","));
pipeline.Add(new Dictionarizer("Label"));
pipeline.Add(new ColumnConcatenator("Features", "AgeStart", "AgeEnd"));
pipeline.Add(new StochasticDualCoordinateAscentClassifier());
pipeline.Add(new PredictedLabelColumnOriginalValueConverter {PredictedLabelColumn = "PredictedLabel"});
var model = pipeline.Train<AgeRangeData, AgeRangePrediction>();
var modelFilePath = "AgeRangeDataModel.zip";
model.WriteAsync(modelFilePath);
Console.ReadLine();
}
}

view raw
MLNetSaveModel.cs
hosted with ❤ by GitHub

And then another project, where the engraved model is used. Attention to lines 18 to 21, from there the way to use the model is the same

class Program
{
private static string _modelFilePath = "AgeRangeDataModel.zip";
private static PredictionModel<AgeRangeData, AgeRangePrediction> _model;
static void Main(string[] args)
{
LoadModel();
var prediction = _model.Predict(new AgeRangeData()
{
AgeStart = 1,
AgeEnd = 2
});
Console.WriteLine($"Predicted age range is: {prediction.PredictedLabels}");
Console.ReadLine();
}
private static async void LoadModel()
{
_model = await PredictionModel.ReadAsync<AgeRangeData, AgeRangePrediction>(_modelFilePath);
}
}

view raw
MLNetLoadModel.cs
hosted with ❤ by GitHub

Happy Coding!

Greetings @ Toronto

El Bruno

References

My Posts

26 thoughts on “#MLNET – Write and Load models using Machine Learning .Net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.