0
0
RhSolutions-ML/RhSolutions.ML.Lib/RhSolutionsMLBuilder.cs

58 lines
2.3 KiB
C#

using Microsoft.ML;
using Microsoft.ML.Data;
namespace RhSolutions.ML.Lib;
public class RhSolutionsMLBuilder
{
private static string _appPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) ?? ".";
private static string _testDataPath = Path.Combine(_appPath, "..", "..", "..", "..", "TestData", "*");
private static MLContext _mlContext = new MLContext(seed: 0);
private static ITransformer? _trainedModel = null;
public static void RebuildModel()
{
var _trainDataView = _mlContext.Data.LoadFromTextFile<Product>(
Path.Combine(_appPath, "..", "..", "..", "..", "TrainData", "*"), hasHeader: false);
var pipeline = ProcessData();
BuildAndTrainModel(_trainDataView, pipeline, out ITransformer _trainedModel);
SaveModelAsFile(_mlContext, _trainDataView.Schema, _trainedModel);
}
public static MulticlassClassificationMetrics? EvaluateModel()
{
var testDataView = _mlContext.Data.LoadFromTextFile<Product>(_testDataPath, hasHeader: false, separatorChar: ';');
return _mlContext.MulticlassClassification.Evaluate(_trainedModel?.Transform(testDataView));
}
private static IEstimator<ITransformer> ProcessData()
{
var pipeline = _mlContext.Transforms.Conversion.MapValueToKey(inputColumnName: "Type", outputColumnName: "Label")
.Append(_mlContext.Transforms.Text.FeaturizeText(inputColumnName: "Name", outputColumnName: "NameFeaturized"))
.Append(_mlContext.Transforms.Concatenate("Features", "NameFeaturized"))
.AppendCacheCheckpoint(_mlContext);
return pipeline;
}
private static IEstimator<ITransformer> BuildAndTrainModel(IDataView trainingDataView, IEstimator<ITransformer> pipeline, out ITransformer trainedModel)
{
var trainingPipeline = pipeline.Append(_mlContext.MulticlassClassification.Trainers.SdcaMaximumEntropy("Label", "Features"))
.Append(_mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));
trainedModel = trainingPipeline.Fit(trainingDataView);
return trainingPipeline;
}
private static void SaveModelAsFile(MLContext mlContext, DataViewSchema trainingDataViewSchema, ITransformer model)
{
string path = Path.Combine(_appPath, "..", "..", "..", "..", "Models");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
mlContext.Model.Save(model, trainingDataViewSchema,
Path.Combine(path, "model.zip"));
}
}