BuildDivisorTransitions application added.
This commit is contained in:
1 parent
97fd041ec0
commit
046cfe7d26
12 files changed
+979
-4
No files matched your search
@@ -0,0 +1,100 @@
|
||||
// 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;
|
||||
|
||||
/// <summary>
|
||||
/// Train a new model with the provided dataset.
|
||||
/// </summary>
|
||||
/// <param name="outputModelPath">File path for saving the model. Should be similar to "C:\YourPath\ModelName.mlnet"</param>
|
||||
/// <param name="inputDataFilePath">Path to the data file for training.</param>
|
||||
/// <param name="separatorChar">Separator character for delimited training file.</param>
|
||||
/// <param name="hasHeader">Boolean if training file has a header.</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load an IDataView from a file path.
|
||||
/// </summary>
|
||||
/// <param name="mlContext">The common context for all ML.NET operations.</param>
|
||||
/// <param name="inputDataFilePath">Path to the data file for training.</param>
|
||||
/// <param name="separatorChar">Separator character for delimited training file.</param>
|
||||
/// <param name="hasHeader">Boolean if training file has a header.</param>
|
||||
/// <returns>IDataView with loaded training data.</returns>
|
||||
public static IDataView LoadIDataViewFromFile(MLContext mlContext, string inputDataFilePath, char separatorChar, bool hasHeader, bool allowQuoting)
|
||||
{
|
||||
return mlContext.Data.LoadFromTextFile<ModelInput>(inputDataFilePath, separatorChar, hasHeader, allowQuoting: allowQuoting);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Save a model at the specified path.
|
||||
/// </summary>
|
||||
/// <param name="mlContext">The common context for all ML.NET operations.</param>
|
||||
/// <param name="model">Model to save.</param>
|
||||
/// <param name="data">IDataView used to train the model.</param>
|
||||
/// <param name="modelSavePath">File path for saving the model. Should be similar to "C:\YourPath\ModelName.mlnet.</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Retrain model using the pipeline generated as part of the training process.
|
||||
/// </summary>
|
||||
/// <param name="mlContext"></param>
|
||||
/// <param name="trainData"></param>
|
||||
/// <returns></returns>
|
||||
public static ITransformer RetrainModel(MLContext mlContext, IDataView trainData)
|
||||
{
|
||||
var pipeline = BuildPipeline(mlContext);
|
||||
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.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user