44
55using  System ; 
66using  System . IO ; 
7+ using  System . Linq ; 
78using  Microsoft . Data . DataView ; 
89using  Microsoft . ML ; 
910using  Microsoft . ML . Auto ; 
@@ -18,31 +19,34 @@ public class AutoTrainBinaryClassification
1819        private  static string  TestDataPath  =  $ "{ BaseDatasetsLocation } /wikipedia-detox-250-line-test.tsv"; 
1920        private  static string  ModelPath  =  $ "{ BaseDatasetsLocation } /SentimentModel.zip"; 
2021        private  static string  LabelColumn  =  "Sentiment" ; 
22+         private  static uint  ExperimentTime  =  60 ; 
2123
2224        public  static void  Run ( ) 
2325        { 
2426            MLContext  mlContext  =  new  MLContext ( ) ; 
2527
2628            // STEP 1: Infer columns 
27-             var  columnInference  =  mlContext . AutoInference ( ) . InferColumns ( TrainDataPath ,  LabelColumn ) ; 
29+             var  columnInference  =  mlContext . Auto ( ) . InferColumns ( TrainDataPath ,  LabelColumn ) ; 
2830
2931            // STEP 2: Load data 
30-             TextLoader  textLoader  =  mlContext . Data . CreateTextLoader ( columnInference . TextLoaderArgs ) ; 
31-             IDataView  trainDataView  =  textLoader . Read ( TrainDataPath ) ; 
32-             IDataView  testDataView  =  textLoader . Read ( TestDataPath ) ; 
32+             var  textLoader  =  mlContext . Data . CreateTextLoader ( columnInference . TextLoaderArgs ) ; 
33+             var  trainDataView  =  textLoader . Read ( TrainDataPath ) ; 
34+             var  testDataView  =  textLoader . Read ( TestDataPath ) ; 
3335
3436            // STEP 3: Auto featurize, auto train and auto hyperparameter tune 
35-             Console . WriteLine ( $ "Invoking new  AutoML binary classification experiment...") ; 
36-             var  runResults  =  mlContext . AutoInference ( ) 
37-                 . CreateBinaryClassificationExperiment ( 60 ) 
37+             Console . WriteLine ( $ "Running  AutoML binary classification experiment for  { ExperimentTime }  seconds ...") ; 
38+             var  runResults  =  mlContext . Auto ( ) 
39+                 . CreateBinaryClassificationExperiment ( ExperimentTime ) 
3840                . Execute ( trainDataView ,  LabelColumn ) ; 
3941
4042            // STEP 4: Print metric from the best model 
4143            var  best  =  runResults . Best ( ) ; 
42-             Console . WriteLine ( $ "Accuracy of best model from validation data: { best . Metrics . Accuracy } ") ; 
44+             Console . WriteLine ( $ "Total models produced: { runResults . Count ( ) } ") ; 
45+             Console . WriteLine ( $ "Best model's trainer: { best . TrainerName } ") ; 
46+             Console . WriteLine ( $ "Accuracy of best model from validation data: { best . ValidationMetrics . Accuracy } ") ; 
4347
4448            // STEP 5: Evaluate test data 
45-             IDataView  testDataViewWithBestScore  =  best . Model . Transform ( testDataView ) ; 
49+             var  testDataViewWithBestScore  =  best . Model . Transform ( testDataView ) ; 
4650            var  testMetrics  =  mlContext . BinaryClassification . EvaluateNonCalibrated ( testDataViewWithBestScore ,  label :  LabelColumn ) ; 
4751            Console . WriteLine ( $ "Accuracy of best model on test data: { testMetrics . Accuracy } ") ; 
4852
@@ -51,7 +55,7 @@ public static void Run()
5155                best . Model . SaveTo ( mlContext ,  fs ) ; 
5256
5357            Console . WriteLine ( "Press any key to continue..." ) ; 
54-             Console . ReadLine ( ) ; 
58+             Console . ReadKey ( ) ; 
5559        } 
5660    } 
5761} 
0 commit comments