// This file was auto-generated by ML.NET Model Builder. using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.ML; using Microsoft.ML.Data; using Microsoft.ML.Trainers; using Microsoft.ML.Trainers.FastTree; namespace EvenPerfectBitScanner { public partial class MLModel1 { public const string RetrainFilePath = @"C:\Users\marek\source\repos\even-perfect-numbers-scanner\BuildDivisorTransitions\bin\Release\net8.0\NextKForecastTraning.csv"; public const char RetrainSeparatorChar = ','; public const bool RetrainHasHeader = true; public const bool RetrainAllowQuoting = false; /// /// Train a new model with the provided dataset. /// /// File path for saving the model. Should be similar to "C:\YourPath\ModelName.mlnet" /// Path to the data file for training. /// Separator character for delimited training file. /// Boolean if training file has a header. public static void Train(string outputModelPath, string inputDataFilePath = RetrainFilePath, char separatorChar = RetrainSeparatorChar, bool hasHeader = RetrainHasHeader, bool allowQuoting = RetrainAllowQuoting) { var mlContext = new MLContext(); var data = LoadIDataViewFromFile(mlContext, inputDataFilePath, separatorChar, hasHeader, allowQuoting); var model = RetrainModel(mlContext, data); SaveModel(mlContext, model, data, outputModelPath); } /// /// Load an IDataView from a file path. /// /// The common context for all ML.NET operations. /// Path to the data file for training. /// Separator character for delimited training file. /// Boolean if training file has a header. /// IDataView with loaded training data. public static IDataView LoadIDataViewFromFile(MLContext mlContext, string inputDataFilePath, char separatorChar, bool hasHeader, bool allowQuoting) { return mlContext.Data.LoadFromTextFile(inputDataFilePath, separatorChar, hasHeader, allowQuoting: allowQuoting); } /// /// Save a model at the specified path. /// /// The common context for all ML.NET operations. /// Model to save. /// IDataView used to train the model. /// File path for saving the model. Should be similar to "C:\YourPath\ModelName.mlnet. public static void SaveModel(MLContext mlContext, ITransformer model, IDataView data, string modelSavePath) { // Pull the data schema from the IDataView used for training the model DataViewSchema dataViewSchema = data.Schema; using (var fs = File.Create(modelSavePath)) { mlContext.Model.Save(model, dataViewSchema, fs); } } /// /// Retrain model using the pipeline generated as part of the training process. /// /// /// /// public static ITransformer RetrainModel(MLContext mlContext, IDataView trainData) { var pipeline = BuildPipeline(mlContext); var model = pipeline.Fit(trainData); return model; } /// /// build the pipeline that is used from model builder. Use this function to retrain model. /// /// /// public static IEstimator BuildPipeline(MLContext mlContext) { // Data process configuration with pipeline data transformations var pipeline = mlContext.Transforms.ReplaceMissingValues(new []{new InputOutputColumnPair(@"p", @"p"),new InputOutputColumnPair(@"divisor", @"divisor"),new InputOutputColumnPair(@"lastDigit", @"lastDigit"),new InputOutputColumnPair(@"k", @"k"),new InputOutputColumnPair(@"lastMersenneDigit", @"lastMersenneDigit")}) .Append(mlContext.Transforms.Concatenate(@"Features", new []{@"p",@"divisor",@"lastDigit",@"k",@"lastMersenneDigit"})) .Append(mlContext.Regression.Trainers.FastTree(new FastTreeRegressionTrainer.Options(){NumberOfLeaves=4,MinimumExampleCountPerLeaf=9,NumberOfTrees=8,MaximumBinCountPerFeature=317,FeatureFraction=0.9843433146312323,LearningRate=0.3857390791447961,LabelColumnName=@"isNextKHigher",FeatureColumnName=@"Features",DiskTranspose=false})); return pipeline; } } }