#MLNET – Guardando y reutilizando modelos con Machine Learning .Net

Buenas!

Hoy post rápido que el código se explica por si solo.

En primer lugar, basado en los posts anteriores, el proceso de definir y entrenar el modelo. Lo importante sucede en líneas 15 y 16, donde se graba el modelo.

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

Y luego otro proyecto, donde se utiliza el modelo grabado. Atención a las líneas 18 a 21, a partir de allí la forma de utilizar el modelo es el mismo.

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

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.