3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System ;
6
- using System . Collections . Generic ;
7
6
using System . Diagnostics ;
8
7
using System . IO ;
9
8
using System . Linq ;
10
- using System . Runtime . ExceptionServices ;
11
9
using System . Threading ;
12
- using System . Threading . Tasks ;
13
10
using Microsoft . ML . AutoML ;
14
11
using Microsoft . ML . CLI . CodeGenerator . CSharp ;
15
12
using Microsoft . ML . CLI . Data ;
16
13
using Microsoft . ML . CLI . ShellProgressBar ;
17
14
using Microsoft . ML . CLI . Utilities ;
18
15
using Microsoft . ML . Data ;
19
16
using NLog ;
20
- using NLog . Targets ;
21
17
22
18
namespace Microsoft . ML . CLI . CodeGenerator
23
19
{
@@ -81,7 +77,7 @@ public void GenerateCode()
81
77
// i.e there is no common class/interface to handle all three tasks together.
82
78
83
79
ExperimentResult < BinaryClassificationMetrics > binaryExperimentResult = default ;
84
- ExperimentResult < MulticlassClassificationMetrics > multiExperimentResult = default ;
80
+ ExperimentResult < MulticlassClassificationMetrics > multiclassExperimentResult = default ;
85
81
ExperimentResult < RegressionMetrics > regressionExperimentResult = default ;
86
82
if ( verboseLevel > LogLevel . Trace )
87
83
{
@@ -111,20 +107,22 @@ public void GenerateCode()
111
107
112
108
if ( verboseLevel > LogLevel . Trace && ! Console . IsOutputRedirected )
113
109
{
110
+ Exception ex = null ;
114
111
using ( var pbar = new FixedDurationBar ( wait , "" , options ) )
115
112
{
116
113
pbar . Message = Strings . WaitingForFirstIteration ;
117
114
Thread t = default ;
118
115
switch ( taskKind )
119
116
{
117
+ // TODO: It may be a good idea to convert the below Threads to Tasks or get rid of this progress bar all together and use an existing one in opensource.
120
118
case TaskKind . BinaryClassification :
121
- t = new Thread ( ( ) => binaryExperimentResult = automlEngine . ExploreBinaryClassificationModels ( context , trainData , validationData , columnInformation , new BinaryExperimentSettings ( ) . OptimizingMetric , pbar ) ) ;
119
+ t = new Thread ( ( ) => SafeExecute ( ( ) => automlEngine . ExploreBinaryClassificationModels ( context , trainData , validationData , columnInformation , new BinaryExperimentSettings ( ) . OptimizingMetric , pbar ) , out ex , out binaryExperimentResult , pbar ) ) ;
122
120
break ;
123
121
case TaskKind . Regression :
124
- t = new Thread ( ( ) => regressionExperimentResult = automlEngine . ExploreRegressionModels ( context , trainData , validationData , columnInformation , new RegressionExperimentSettings ( ) . OptimizingMetric , pbar ) ) ;
122
+ t = new Thread ( ( ) => SafeExecute ( ( ) => automlEngine . ExploreRegressionModels ( context , trainData , validationData , columnInformation , new RegressionExperimentSettings ( ) . OptimizingMetric , pbar ) , out ex , out regressionExperimentResult , pbar ) ) ;
125
123
break ;
126
124
case TaskKind . MulticlassClassification :
127
- t = new Thread ( ( ) => multiExperimentResult = automlEngine . ExploreMultiClassificationModels ( context , trainData , validationData , columnInformation , new MulticlassExperimentSettings ( ) . OptimizingMetric , pbar ) ) ;
125
+ t = new Thread ( ( ) => SafeExecute ( ( ) => automlEngine . ExploreMultiClassificationModels ( context , trainData , validationData , columnInformation , new MulticlassExperimentSettings ( ) . OptimizingMetric , pbar ) , out ex , out multiclassExperimentResult , pbar ) ) ;
128
126
break ;
129
127
default :
130
128
logger . Log ( LogLevel . Error , Strings . UnsupportedMlTask ) ;
@@ -147,6 +145,10 @@ public void GenerateCode()
147
145
pbar . Message = originalMessage ;
148
146
}
149
147
}
148
+ if ( ex != null )
149
+ {
150
+ throw ex ;
151
+ }
150
152
}
151
153
}
152
154
else
@@ -160,7 +162,7 @@ public void GenerateCode()
160
162
regressionExperimentResult = automlEngine . ExploreRegressionModels ( context , trainData , validationData , columnInformation , new RegressionExperimentSettings ( ) . OptimizingMetric ) ;
161
163
break ;
162
164
case TaskKind . MulticlassClassification :
163
- multiExperimentResult = automlEngine . ExploreMultiClassificationModels ( context , trainData , validationData , columnInformation , new MulticlassExperimentSettings ( ) . OptimizingMetric ) ;
165
+ multiclassExperimentResult = automlEngine . ExploreMultiClassificationModels ( context , trainData , validationData , columnInformation , new MulticlassExperimentSettings ( ) . OptimizingMetric ) ;
164
166
break ;
165
167
default :
166
168
logger . Log ( LogLevel . Error , Strings . UnsupportedMlTask ) ;
@@ -204,11 +206,11 @@ public void GenerateCode()
204
206
ConsolePrinter . PrintIterationSummary ( regressionExperimentResult . RunDetails , new RegressionExperimentSettings ( ) . OptimizingMetric , 5 ) ;
205
207
break ;
206
208
case TaskKind . MulticlassClassification :
207
- var bestMultiIteration = multiExperimentResult . BestRun ;
208
- bestPipeline = bestMultiIteration . Pipeline ;
209
- bestModel = bestMultiIteration . Model ;
210
- ConsolePrinter . ExperimentResultsHeader ( LogLevel . Info , settings . MlTask , settings . Dataset . Name , columnInformation . LabelColumnName , elapsedTime . ToString ( "F2" ) , multiExperimentResult . RunDetails . Count ( ) ) ;
211
- ConsolePrinter . PrintIterationSummary ( multiExperimentResult . RunDetails , new MulticlassExperimentSettings ( ) . OptimizingMetric , 5 ) ;
209
+ var bestMulticlassIteration = multiclassExperimentResult . BestRun ;
210
+ bestPipeline = bestMulticlassIteration . Pipeline ;
211
+ bestModel = bestMulticlassIteration . Model ;
212
+ ConsolePrinter . ExperimentResultsHeader ( LogLevel . Info , settings . MlTask , settings . Dataset . Name , columnInformation . LabelColumnName , elapsedTime . ToString ( "F2" ) , multiclassExperimentResult . RunDetails . Count ( ) ) ;
213
+ ConsolePrinter . PrintIterationSummary ( multiclassExperimentResult . RunDetails , new MulticlassExperimentSettings ( ) . OptimizingMetric , 5 ) ;
212
214
break ;
213
215
}
214
216
}
@@ -278,5 +280,51 @@ private void ConsumeAutoMLSDKLogs(MLContext context)
278
280
}
279
281
} ;
280
282
}
283
+
284
+ private void SafeExecute ( Func < ExperimentResult < BinaryClassificationMetrics > > p , out Exception ex , out ExperimentResult < BinaryClassificationMetrics > binaryExperimentResult , FixedDurationBar pbar )
285
+ {
286
+ try
287
+ {
288
+ binaryExperimentResult = p . Invoke ( ) ;
289
+ ex = null ;
290
+ }
291
+ catch ( Exception e )
292
+ {
293
+ ex = e ;
294
+ binaryExperimentResult = null ;
295
+ return ;
296
+ }
297
+ }
298
+
299
+ private void SafeExecute ( Func < ExperimentResult < RegressionMetrics > > p , out Exception ex , out ExperimentResult < RegressionMetrics > regressionExperimentResult , FixedDurationBar pbar )
300
+ {
301
+ try
302
+ {
303
+ regressionExperimentResult = p . Invoke ( ) ;
304
+ ex = null ;
305
+ }
306
+ catch ( Exception e )
307
+ {
308
+ ex = e ;
309
+ regressionExperimentResult = null ;
310
+ return ;
311
+ }
312
+ }
313
+
314
+ private void SafeExecute ( Func < ExperimentResult < MulticlassClassificationMetrics > > p , out Exception ex , out ExperimentResult < MulticlassClassificationMetrics > multiClassExperimentResult , FixedDurationBar pbar )
315
+ {
316
+ try
317
+ {
318
+ multiClassExperimentResult = p . Invoke ( ) ;
319
+ ex = null ;
320
+ }
321
+ catch ( Exception e )
322
+ {
323
+ ex = e ;
324
+ multiClassExperimentResult = null ;
325
+ pbar . Dispose ( ) ; // or ((ManualResetEvent)pbar.CompletedHandle).Set();
326
+ return ;
327
+ }
328
+ }
281
329
}
282
330
}
0 commit comments