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.
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(); | |
} | |
} |
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
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
26 comments