1

I am using c# with ml.net with the new The model builder ver16.6.1. The model builder generates code, one of them is MLModel.training.cs

I need to retrain my model as part of the application, how I can call this function to retrain my model from Main?

public partial class MLModel1
{
    public static ITransformer RetrainPipeline(MLContext context, IDataView trainData)
    {
        var pipeline = BuildPipeline(context);
        var model = pipeline.Fit(trainData);

        return model;
    }

    /// <summary>
    /// build the pipeline that is used from model builder. Use this function to retrain model.
    /// </summary>
    /// <param name="mlContext"></param>
    /// <returns></returns>
    public static IEstimator<ITransformer> BuildPipeline(MLContext mlContext)
    {
        // Data process configuration with pipeline data transformations
        var pipeline = mlContext.Transforms.Text.FeaturizeText(@"grade2", @"grade2")      
                                .Append(mlContext.Transforms.Text.FeaturizeText(@"grade3", @"grade3"))      
                                .Append(mlContext.Transforms.Text.FeaturizeText(@"grade4", @"grade4"))      
                                .Append(mlContext.Transforms.Text.FeaturizeText(@"grade5", @"grade5"))      
                                .Append(mlContext.Transforms.Text.FeaturizeText(@"grade6", @"grade6"))      
                                .Append(mlContext.Transforms.Text.FeaturizeText(@"grade7", @"grade7"))      
                                .Append(mlContext.Transforms.Text.FeaturizeText(@"grade8", @"grade8"))      
                                .Append(mlContext.Transforms.Text.FeaturizeText(@"grade9", @"grade9"))      
                                .Append(mlContext.Transforms.Text.FeaturizeText(@"grade10", @"grade10"))      
                                
                                .Append(mlContext.Transforms.Concatenate(@"Features", new []{@"grade2",@"grade3",@"grade4",@"grade5",@"grade6",@"grade7",@"grade8",@"grade9",@"grade10"}))      
                                .Append(mlContext.Transforms.Conversion.MapValueToKey(@"supv", @"supv"))      
                                .Append(mlContext.Transforms.NormalizeMinMax(@"Features", @"Features"))      
                                .Append(mlContext.MulticlassClassification.Trainers.OneVersusAll(binaryEstimator:mlContext.BinaryClassification.Trainers.LbfgsLogisticRegression(l1Regularization:0.263541282856117F,l2Regularization:0.237613799320853F,labelColumnName:@"supv",featureColumnName:@"Features"), labelColumnName: @"supv"))      
                                .Append(mlContext.Transforms.Conversion.MapKeyToValue(@"PredictedLabel", @"PredictedLabel"));

        return pipeline;
    }enter code here
Gattan
  • 11
  • 3

1 Answers1

1

In the consume step you can create a console application and call the RetrainPipeline method from there. Here's a sample.

https://github.com/luisquintanilla/RetrainSample/blob/main/RetrainSample/Program.cs