Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
217 lines (180 loc) · 10.8 KB
/
Copy pathProgram.cs
File metadata and controls
217 lines (180 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// <SnippetAddUsings>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Data.DataView;
using Microsoft.ML;
using Microsoft.ML.Core.Data;
using Microsoft.ML.Data;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms.Conversions;
using Microsoft.ML.Transforms.Normalizers;
// </SnippetAddUsings>
namespace GitHubIssueClassification
{
class Program
{
// <SnippetDeclareGlobalVariables>
private static string _appPath => Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
private static string _trainDataPath => Path.Combine(_appPath, "..", "..", "..", "Data", "issues_train.tsv");
private static string _testDataPath => Path.Combine(_appPath, "..", "..", "..", "Data", "issues_test.tsv");
private static string _modelPath => Path.Combine(_appPath, "..", "..", "..", "Models", "model.zip");
private static MLContext _mlContext;
private static PredictionEngine<GitHubIssue, IssuePrediction> _predEngine;
private static ITransformer _trainedModel;
static IDataView _trainingDataView;
// </SnippetDeclareGlobalVariables>
static void Main(string[] args)
{
// Create MLContext to be shared across the model creation workflow objects
// Set a random seed for repeatable/deterministic results across multiple trainings.
// <SnippetCreateMLContext>
_mlContext = new MLContext(seed: 0);
// </SnippetCreateMLContext>
// STEP 1: Common data loading configuration
// CreateTextReader<GitHubIssue>(hasHeader: true) - Creates a TextLoader by inferencing the dataset schema from the GitHubIssue data model type.
// .Read(_trainDataPath) - Loads the training text file into an IDataView (_trainingDataView) and maps from input columns to IDataView columns.
Console.WriteLine($"=============== Loading Dataset ===============");
// <SnippetLoadTrainData>
_trainingDataView = _mlContext.Data.CreateTextLoader<GitHubIssue>(hasHeader: true).Read(_trainDataPath);
// </SnippetLoadTrainData>
Console.WriteLine($"=============== Finished Loading Dataset ===============");
// <SnippetSplitData>
// var (trainData, testData) = _mlContext.MulticlassClassification.TrainTestSplit(_trainingDataView, testFraction: 0.1);
// </SnippetSplitData>
// <SnippetCallProcessData>
var pipeline = ProcessData();
// </SnippetCallProcessData>
// <SnippetCallBuildAndTrainModel>
var trainingPipeline = BuildAndTrainModel(_trainingDataView, pipeline);
// </SnippetCallBuildAndTrainModel>
// <SnippetCallEvaluate>
Evaluate();
// </SnippetCallEvaluate>
// <SnippetCallPredictIssue>
PredictIssue();
// </SnippetCallPredictIssue>
}
public static EstimatorChain<ITransformer> ProcessData()
{
Console.WriteLine($"=============== Processing Data ===============");
// STEP 2: Common data process configuration with pipeline data transformations
// <SnippetMapValueToKey>
var pipeline = _mlContext.Transforms.Conversion.MapValueToKey(inputColumnName: "Area", outputColumnName: "Label")
// </SnippetMapValueToKey>
// <SnippetFeaturizeText>
.Append(_mlContext.Transforms.Text.FeaturizeText(inputColumnName: "Title", outputColumnName: "TitleFeaturized"))
.Append(_mlContext.Transforms.Text.FeaturizeText(inputColumnName: "Description", outputColumnName: "DescriptionFeaturized"))
// </SnippetFeaturizeText>
// <SnippetConcatenate>
.Append(_mlContext.Transforms.Concatenate("Features", "TitleFeaturized", "DescriptionFeaturized"))
// </SnippetConcatenate>
//Sample Caching the DataView so estimators iterating over the data multiple times, instead of always reading from file, using the cache might get better performance.
// <SnippetAppendCache>
.AppendCacheCheckpoint(_mlContext);
// </SnippetAppendCache>
Console.WriteLine($"=============== Finished Processing Data ===============");
// <SnippetReturnPipeline>
return pipeline;
// </SnippetReturnPipeline>
}
public static EstimatorChain<KeyToValueMappingTransformer> BuildAndTrainModel(IDataView trainingDataView, EstimatorChain<ITransformer> pipeline)
{
// STEP 3: Create the training algorithm/trainer
// Use the multi-class SDCA algorithm to predict the label using features.
//Set the trainer/algorithm and map label to value (original readable state)
// <SnippetAddTrainer>
var trainingPipeline = pipeline.Append(_mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(DefaultColumnNames.Label, DefaultColumnNames.Features))
.Append(_mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));
// </SnippetAddTrainer>
// STEP 4: Train the model fitting to the DataSet
Console.WriteLine($"=============== Training the model ===============");
// <SnippetTrainModel>
_trainedModel = trainingPipeline.Fit(trainingDataView);
// </SnippetTrainModel>
Console.WriteLine($"=============== Finished Training the model Ending time: {DateTime.Now.ToString()} ===============");
// (OPTIONAL) Try/test a single prediction with the "just-trained model" (Before saving the model)
Console.WriteLine($"=============== Single Prediction just-trained-model ===============");
// Create prediction engine related to the loaded trained model
// <SnippetCreatePredictionEngine1>
_predEngine = _trainedModel.CreatePredictionEngine<GitHubIssue, IssuePrediction>(_mlContext);
// </SnippetCreatePredictionEngine1>
// <SnippetCreateTestIssue1>
GitHubIssue issue = new GitHubIssue() {
Title = "WebSockets communication is slow in my machine",
Description = "The WebSockets communication used under the covers by SignalR looks like is going slow in my development machine.."
};
// </SnippetCreateTestIssue1>
// <SnippetPredict>
var prediction = _predEngine.Predict(issue);
// </SnippetPredict>
// <SnippetOutputPrediction>
Console.WriteLine($"=============== Single Prediction just-trained-model - Result: {prediction.Area} ===============");
// </SnippetOutputPrediction>
// <SnippetReturnModel>
return trainingPipeline;
// </SnippetReturnModel>
}
public static void Evaluate()
{
// STEP 5: Evaluate the model in order to get the model's accuracy metrics
Console.WriteLine($"=============== Evaluating to get model's accuracy metrics - Starting time: {DateTime.Now.ToString()} ===============");
//Load the test dataset into the IDataView
// <SnippetLoadTestDataset>
var testDataView = _mlContext.Data.CreateTextLoader<GitHubIssue>(hasHeader: true).Read(_testDataPath);
// </SnippetLoadTestDataset>
//Evaluate the model on a test dataset and calculate metrics of the model on the test data.
// <SnippetEvaluate>
var testMetrics = _mlContext.MulticlassClassification.Evaluate(_trainedModel.Transform(testDataView));
// </SnippetEvaluate>
Console.WriteLine($"=============== Evaluating to get model's accuracy metrics - Ending time: {DateTime.Now.ToString()} ===============");
// <SnippetDisplayMetrics>
Console.WriteLine($"*************************************************************************************************************");
Console.WriteLine($"* Metrics for Multi-class Classification model - Test Data ");
Console.WriteLine($"*------------------------------------------------------------------------------------------------------------");
Console.WriteLine($"* MicroAccuracy: {testMetrics.AccuracyMicro:0.###}");
Console.WriteLine($"* MacroAccuracy: {testMetrics.AccuracyMacro:0.###}");
Console.WriteLine($"* LogLoss: {testMetrics.LogLoss:#.###}");
Console.WriteLine($"* LogLossReduction: {testMetrics.LogLossReduction:#.###}");
Console.WriteLine($"*************************************************************************************************************");
// </SnippetDisplayMetrics>
// Save the new model to .ZIP file
// <SnippetCallSaveModel>
SaveModelAsFile(_mlContext, _trainedModel);
// </SnippetCallSaveModel>
}
public static void PredictIssue()
{
// <SnippetLoadModel>
ITransformer loadedModel;
using (var stream = new FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
loadedModel = _mlContext.Model.Load(stream);
}
// </SnippetLoadModel>
// <SnippetAddTestIssue>
GitHubIssue singleIssue = new GitHubIssue() { Title = "Entity Framework crashes", Description = "When connecting to the database, EF is crashing" };
// </SnippetAddTestIssue>
//Predict label for single hard-coded issue
// <SnippetCreatePredictionEngine>
_predEngine = loadedModel.CreatePredictionEngine<GitHubIssue, IssuePrediction>(_mlContext);
// </SnippetCreatePredictionEngine>
// <SnippetPredictIssue>
var prediction = _predEngine.Predict(singleIssue);
// </SnippetPredictIssue>
// <SnippetDisplayResults>
Console.WriteLine($"=============== Single Prediction - Result: {prediction.Area} ===============");
// </SnippetDisplayResults>
}
private static void SaveModelAsFile(MLContext mlContext, ITransformer model)
{
// <SnippetSaveModel>
using (var fs = new FileStream(_modelPath, FileMode.Create, FileAccess.Write, FileShare.Write))
mlContext.Model.Save(model, fs);
// </SnippetSaveModel>
Console.WriteLine("The model is saved to {0}", _modelPath);
}
}
}