summaryrefslogtreecommitdiff
path: root/RhSolutions.ML.Lib
diff options
context:
space:
mode:
authorSerghei Cebotari <serghei@cebotari.ru>2023-09-20 13:46:55 +0300
committerSerghei Cebotari <serghei@cebotari.ru>2023-09-20 13:46:55 +0300
commitcde4fca02029cf3552cb63c050d3002bab68828f (patch)
tree35f1c25e6c0089708f607dd11a81682209ce4123 /RhSolutions.ML.Lib
parent0c85e000b401c1bab36d07b82c4a0b727562c42a (diff)
Solution organize
Diffstat (limited to 'RhSolutions.ML.Lib')
-rw-r--r--RhSolutions.ML.Lib/Product.cs16
-rw-r--r--RhSolutions.ML.Lib/RhSolutions.ML.Lib.csproj13
-rw-r--r--RhSolutions.ML.Lib/RhSolutionsMLBuilder.cs42
3 files changed, 71 insertions, 0 deletions
diff --git a/RhSolutions.ML.Lib/Product.cs b/RhSolutions.ML.Lib/Product.cs
new file mode 100644
index 0000000..99040fc
--- /dev/null
+++ b/RhSolutions.ML.Lib/Product.cs
@@ -0,0 +1,16 @@
+using Microsoft.ML.Data;
+namespace RhSolutions.ML;
+
+public class Product
+{
+ [LoadColumn(0)]
+ public string? Name { get; set; }
+ [LoadColumn(1)]
+ public string? Type { get; set; }
+}
+
+public class TypePrediction
+{
+ [ColumnName("PredictedLabel")]
+ public string? Type { get; set; }
+} \ No newline at end of file
diff --git a/RhSolutions.ML.Lib/RhSolutions.ML.Lib.csproj b/RhSolutions.ML.Lib/RhSolutions.ML.Lib.csproj
new file mode 100644
index 0000000..928fc99
--- /dev/null
+++ b/RhSolutions.ML.Lib/RhSolutions.ML.Lib.csproj
@@ -0,0 +1,13 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net7.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.ML" Version="2.0.1" />
+ </ItemGroup>
+
+</Project>
diff --git a/RhSolutions.ML.Lib/RhSolutionsMLBuilder.cs b/RhSolutions.ML.Lib/RhSolutionsMLBuilder.cs
new file mode 100644
index 0000000..be2e2a6
--- /dev/null
+++ b/RhSolutions.ML.Lib/RhSolutionsMLBuilder.cs
@@ -0,0 +1,42 @@
+using Microsoft.ML;
+
+namespace RhSolutions.ML.Lib;
+
+public class RhSolutionsMLBuilder
+{
+ private static string _appPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) ?? ".";
+
+ private static MLContext _mlContext = new MLContext(seed: 0);
+
+ public static void RebuildModel()
+ {
+ var _trainDataView = _mlContext.Data.LoadFromTextFile<Product>(
+ Path.Combine(_appPath, "..", "..", "..", "..", "Data", "*"), hasHeader: false);
+ var pipeline = ProcessData();
+ BuildAndTrainModel(_trainDataView, pipeline, out ITransformer trainedModel);
+ SaveModelAsFile(_mlContext, _trainDataView.Schema, trainedModel);
+ }
+ 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)
+ {
+ mlContext.Model.Save(model, trainingDataViewSchema,
+ Path.Combine(_appPath, "..", "..", "..", "..", "Models", "model.zip"));
+ }
+}