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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Happy Coding!
Greetings @ Toronto
El Bruno
References
My Posts