我在ml.net中使用c#和新的模型生成器版本16.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
在消费步骤中,您可以创建一个控制台应用程序并从中调用RetrainPipeline方法。这是一个样品。
https://github.com/luisquintanilla/RetrainSample/blob/main/RetrainSample/Program.cs