22using System ;
33using System . IO ;
44using System . Linq ;
5- using Microsoft . Data . DataView ;
65using Microsoft . ML ;
7- using Microsoft . ML . Data ;
8- using Microsoft . ML . Transforms ;
96// </SnippetAddUsings>
107
118namespace GitHubIssueClassification
@@ -55,7 +52,7 @@ static void Main(string[] args)
5552 // </SnippetCallBuildAndTrainModel>
5653
5754 // <SnippetCallEvaluate>
58- Evaluate ( ) ;
55+ Evaluate ( _trainingDataView . Schema ) ;
5956 // </SnippetCallEvaluate>
6057
6158 // <SnippetCallPredictIssue>
@@ -95,7 +92,7 @@ public static IEstimator<ITransformer> BuildAndTrainModel(IDataView trainingData
9592 // Use the multi-class SDCA algorithm to predict the label using features.
9693 //Set the trainer/algorithm and map label to value (original readable state)
9794 // <SnippetAddTrainer>
98- var trainingPipeline = pipeline . Append ( _mlContext . MulticlassClassification . Trainers . StochasticDualCoordinateAscent ( DefaultColumnNames . Label , DefaultColumnNames . Features ) )
95+ var trainingPipeline = pipeline . Append ( _mlContext . MulticlassClassification . Trainers . SdcaMaximumEntropy ( " Label" , " Features" ) )
9996 . Append ( _mlContext . Transforms . Conversion . MapKeyToValue ( "PredictedLabel" ) ) ;
10097 // </SnippetAddTrainer>
10198
@@ -112,7 +109,7 @@ public static IEstimator<ITransformer> BuildAndTrainModel(IDataView trainingData
112109
113110 // Create prediction engine related to the loaded trained model
114111 // <SnippetCreatePredictionEngine1>
115- _predEngine = _trainedModel . CreatePredictionEngine < GitHubIssue , IssuePrediction > ( _mlContext ) ;
112+ _predEngine = _mlContext . Model . CreatePredictionEngine < GitHubIssue , IssuePrediction > ( _trainedModel ) ;
116113 // </SnippetCreatePredictionEngine1>
117114 // <SnippetCreateTestIssue1>
118115 GitHubIssue issue = new GitHubIssue ( ) {
@@ -135,7 +132,7 @@ public static IEstimator<ITransformer> BuildAndTrainModel(IDataView trainingData
135132
136133 }
137134
138- public static void Evaluate ( )
135+ public static void Evaluate ( DataViewSchema trainingDataViewSchema )
139136 {
140137 // STEP 5: Evaluate the model in order to get the model's accuracy metrics
141138 Console . WriteLine ( $ "=============== Evaluating to get model's accuracy metrics - Starting time: { DateTime . Now . ToString ( ) } ===============") ;
@@ -155,28 +152,24 @@ public static void Evaluate()
155152 Console . WriteLine ( $ "*************************************************************************************************************") ;
156153 Console . WriteLine ( $ "* Metrics for Multi-class Classification model - Test Data ") ;
157154 Console . WriteLine ( $ "*------------------------------------------------------------------------------------------------------------") ;
158- Console . WriteLine ( $ "* MicroAccuracy: { testMetrics . AccuracyMicro : 0.###} ") ;
159- Console . WriteLine ( $ "* MacroAccuracy: { testMetrics . AccuracyMacro : 0.###} ") ;
155+ Console . WriteLine ( $ "* MicroAccuracy: { testMetrics . MicroAccuracy : 0.###} ") ;
156+ Console . WriteLine ( $ "* MacroAccuracy: { testMetrics . MacroAccuracy : 0.###} ") ;
160157 Console . WriteLine ( $ "* LogLoss: { testMetrics . LogLoss : #.###} ") ;
161158 Console . WriteLine ( $ "* LogLossReduction: { testMetrics . LogLossReduction : #.###} ") ;
162159 Console . WriteLine ( $ "*************************************************************************************************************") ;
163160 // </SnippetDisplayMetrics>
164161
165162 // Save the new model to .ZIP file
166163 // <SnippetCallSaveModel>
167- SaveModelAsFile ( _mlContext , _trainedModel ) ;
164+ SaveModelAsFile ( _mlContext , trainingDataViewSchema , _trainedModel ) ;
168165 // </SnippetCallSaveModel>
169166
170167 }
171168
172169 public static void PredictIssue ( )
173170 {
174171 // <SnippetLoadModel>
175- ITransformer loadedModel ;
176- using ( var stream = new FileStream ( _modelPath , FileMode . Open , FileAccess . Read , FileShare . Read ) )
177- {
178- loadedModel = _mlContext . Model . Load ( stream ) ;
179- }
172+ ITransformer loadedModel = _mlContext . Model . Load ( _modelPath , out var modelInputSchema ) ;
180173 // </SnippetLoadModel>
181174
182175 // <SnippetAddTestIssue>
@@ -185,7 +178,7 @@ public static void PredictIssue()
185178
186179 //Predict label for single hard-coded issue
187180 // <SnippetCreatePredictionEngine>
188- _predEngine = loadedModel . CreatePredictionEngine < GitHubIssue , IssuePrediction > ( _mlContext ) ;
181+ _predEngine = _mlContext . Model . CreatePredictionEngine < GitHubIssue , IssuePrediction > ( loadedModel ) ;
189182 // </SnippetCreatePredictionEngine>
190183
191184 // <SnippetPredictIssue>
@@ -198,11 +191,10 @@ public static void PredictIssue()
198191
199192 }
200193
201- private static void SaveModelAsFile ( MLContext mlContext , ITransformer model )
194+ private static void SaveModelAsFile ( MLContext mlContext , DataViewSchema trainingDataViewSchema , ITransformer model )
202195 {
203196 // <SnippetSaveModel>
204- using ( var fs = new FileStream ( _modelPath , FileMode . Create , FileAccess . Write , FileShare . Write ) )
205- mlContext . Model . Save ( model , fs ) ;
197+ mlContext . Model . Save ( model , trainingDataViewSchema , _modelPath ) ;
206198 // </SnippetSaveModel>
207199
208200 Console . WriteLine ( "The model is saved to {0}" , _modelPath ) ;
0 commit comments