Skip to content

Commit 1985cce

Browse files
committed
Forward logs of Experiment's sub MLContexts to main MLContext
1 parent e3597cd commit 1985cce

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/Microsoft.ML.AutoML/Experiment/Experiment.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ private void MainContextCanceledEvent(object state)
8585
}
8686
}
8787

88+
private void RelayCurrentContextLogsToLogger(object sender, LoggingEventArgs e)
89+
{
90+
// Relay logs that are generated by the current MLContext to the Experiment class's
91+
// _logger.
92+
_logger.Info(e.Message);
93+
}
94+
8895
public IList<TRunDetail> Execute()
8996
{
9097
var iterationResults = new List<TRunDetail>();
@@ -129,6 +136,7 @@ public IList<TRunDetail> Execute()
129136
// context is canceled to stop further model training. The cancellation of the main MLContext
130137
// a user has instantiated is not desirable, thus additional MLContexts are used.
131138
_currentModelMLContext = _newContextSeedGenerator == null ? new MLContext() : new MLContext(_newContextSeedGenerator.Next());
139+
_currentModelMLContext.Log += RelayCurrentContextLogsToLogger;
132140
var pipeline = PipelineSuggester.GetNextInferredPipeline(_currentModelMLContext, _history, _datasetColumnInfo, _task,
133141
_optimizingMetricInfo.IsMaximizing, _experimentSettings.CacheBeforeTrainer, _logger, _trainerAllowList);
134142
// break if no candidates returned, means no valid pipeline available

test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,40 @@ namespace Microsoft.ML.AutoML.Test
2121
{
2222
public class AutoFitTests : BaseTestClass
2323
{
24+
// Marker necessary for AutoFitContextLogTest to ensure that the wanted logs
25+
// from Experiment's sub MLContexts were relayed to the main calling MLContext.
26+
bool _markerAutoFitContextLogTest;
2427
public AutoFitTests(ITestOutputHelper output) : base(output)
2528
{
2629
}
2730

31+
private void MlContextLog(object sender, LoggingEventArgs e)
32+
{
33+
// Log containing ImageClassificationTrainer will only come from AutoML's sub
34+
// contexts.
35+
if (!_markerAutoFitContextLogTest && e.Message.Contains("[Source=ImageClassificationTrainer;"))
36+
_markerAutoFitContextLogTest = true;
37+
}
38+
39+
[TensorFlowFact]
40+
public void AutoFitContextLogTest()
41+
{
42+
// This test confirms that logs produced from contexts made during AutoML experiment
43+
// runs are correctly relayed to the main Experiment MLContext.
44+
_markerAutoFitContextLogTest = false;
45+
var context = new MLContext(1);
46+
context.Log += MlContextLog;
47+
var datasetPath = DatasetUtil.GetFlowersDataset();
48+
var columnInference = context.Auto().InferColumns(datasetPath, "Label");
49+
var textLoader = context.Data.CreateTextLoader(columnInference.TextLoaderOptions);
50+
var trainData = textLoader.Load(datasetPath);
51+
var result = context.Auto()
52+
.CreateMulticlassClassificationExperiment(15)
53+
.Execute(trainData, columnInference.ColumnInformation);
54+
Assert.True(_markerAutoFitContextLogTest, "Image classification trainer logs from Experiment's sub contexts" +
55+
"were not relayed to the main MLContext.");
56+
}
57+
2858
[Fact]
2959
public void AutoFitBinaryTest()
3060
{
@@ -85,7 +115,7 @@ public void AutoFitMultiTest(bool useNumberOfCVFolds)
85115
Assert.Equal(NumberDataViewType.Single, scoredData.Schema[DefaultColumnNames.PredictedLabel].Type);
86116
}
87117
}
88-
118+
89119
[TensorFlowFact]
90120
//Skipping test temporarily. This test will be re-enabled once the cause of failures has been determined
91121
[Trait("Category", "SkipInCI")]

0 commit comments

Comments
 (0)