1

我正在将 c# 与 ml.net 与新的模型生成器 ver16.6.1 一起使用。模型构建器生成代码,其中之一是 MLModel.training.cs

我需要重新训练我的模型作为应用程序的一部分,如何调用此函数从 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
4

1 回答 1

1

在使用步骤中,您可以创建一个控制台应用程序并从那里调用 RetrainPipeline 方法。这是一个示例。

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

于 2021-08-09T22:19:58.403 回答