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

60 lines
2.4 KiB
C#
Raw Normal View History

2023-09-20 13:46:55 +03:00
using Microsoft.ML;
2024-01-12 00:00:57 +03:00
using Microsoft.ML.Data;
2023-09-20 13:46:55 +03:00
namespace RhSolutions.ML.Lib;
public class RhSolutionsMLBuilder
{
private static string _appPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) ?? ".";
2024-01-12 00:00:57 +03:00
private static string _testDataPath = Path.Combine(_appPath, "..", "..", "..", "..", "TestData", "*");
2023-09-20 13:46:55 +03:00
private static MLContext _mlContext = new MLContext(seed: 0);
public static void RebuildModel()
{
var _trainDataView = _mlContext.Data.LoadFromTextFile<Product>(
2024-01-11 23:42:19 +03:00
Path.Combine(_appPath, "..", "..", "..", "..", "TrainData", "*"), hasHeader: false);
2023-09-20 13:46:55 +03:00
var pipeline = ProcessData();
2024-01-12 00:00:57 +03:00
BuildAndTrainModel(_trainDataView, pipeline, out ITransformer _trainedModel);
SaveModelAsFile(_mlContext, _trainDataView.Schema, _trainedModel);
2023-09-20 13:46:55 +03:00
}
2024-01-12 00:00:57 +03:00
2024-01-12 00:23:58 +03:00
public static MulticlassClassificationMetrics EvaluateModel()
2024-01-12 00:00:57 +03:00
{
var testDataView = _mlContext.Data.LoadFromTextFile<Product>(_testDataPath, hasHeader: false, separatorChar: ';');
2024-01-12 00:23:58 +03:00
MLContext mlContext = new(seed: 0);
string modelPath = Path.Combine(_appPath, "..", "..", "..", "..", "Models", "model.zip");
var trainedModel = mlContext.Model.Load(modelPath, out _);
return _mlContext.MulticlassClassification.Evaluate(trainedModel.Transform(testDataView));
2024-01-12 00:00:57 +03:00
}
2023-09-20 13:46:55 +03:00
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)
{
2023-12-29 00:05:17 +03:00
string path = Path.Combine(_appPath, "..", "..", "..", "..", "Models");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
2023-09-20 13:46:55 +03:00
mlContext.Model.Save(model, trainingDataViewSchema,
2023-12-29 00:05:17 +03:00
Path.Combine(path, "model.zip"));
2023-09-20 13:46:55 +03:00
}
}