Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward logs of Experiment's sub MLContexts to main MLContext #5554

Merged
merged 2 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Microsoft.ML.AutoML/Experiment/Experiment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ private void MainContextCanceledEvent(object state)
}
}

private void RelayCurrentContextLogsToLogger(object sender, LoggingEventArgs e)
{
// Relay logs that are generated by the current MLContext to the Experiment class's
// _logger.
switch (e.Kind)
{
case ChannelMessageKind.Trace:
_logger.Trace(e.Message);
break;
case ChannelMessageKind.Info:
_logger.Info(e.Message);
break;
case ChannelMessageKind.Warning:
_logger.Warning(e.Message);
break;
case ChannelMessageKind.Error:
_logger.Error(e.Message);
break;
default:
throw new NotImplementedException($"{nameof(ChannelMessageKind)}.{e.Kind} is not yet implemented.");
}
}

public IList<TRunDetail> Execute()
{
var iterationResults = new List<TRunDetail>();
Expand Down Expand Up @@ -129,6 +152,7 @@ public IList<TRunDetail> Execute()
// context is canceled to stop further model training. The cancellation of the main MLContext
// a user has instantiated is not desirable, thus additional MLContexts are used.
_currentModelMLContext = _newContextSeedGenerator == null ? new MLContext() : new MLContext(_newContextSeedGenerator.Next());
_currentModelMLContext.Log += RelayCurrentContextLogsToLogger;
var pipeline = PipelineSuggester.GetNextInferredPipeline(_currentModelMLContext, _history, _datasetColumnInfo, _task,
_optimizingMetricInfo.IsMaximizing, _experimentSettings.CacheBeforeTrainer, _logger, _trainerAllowList);
// break if no candidates returned, means no valid pipeline available
Expand Down
30 changes: 30 additions & 0 deletions test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,40 @@ namespace Microsoft.ML.AutoML.Test
{
public class AutoFitTests : BaseTestClass
{
// Marker necessary for AutoFitContextLogTest to ensure that the wanted logs
// from Experiment's sub MLContexts were relayed to the main calling MLContext.
bool _markerAutoFitContextLogTest;
public AutoFitTests(ITestOutputHelper output) : base(output)
{
}

private void MlContextLog(object sender, LoggingEventArgs e)
{
// Log containing ImageClassificationTrainer will only come from AutoML's sub
// contexts.
if (!_markerAutoFitContextLogTest && e.Message.Contains("[Source=ImageClassificationTrainer;"))
_markerAutoFitContextLogTest = true;
}

[TensorFlowFact]
public void AutoFitContextLogTest()
{
// This test confirms that logs produced from contexts made during AutoML experiment
// runs are correctly relayed to the main Experiment MLContext.
_markerAutoFitContextLogTest = false;
var context = new MLContext(1);
context.Log += MlContextLog;
var datasetPath = DatasetUtil.GetFlowersDataset();
var columnInference = context.Auto().InferColumns(datasetPath, "Label");
var textLoader = context.Data.CreateTextLoader(columnInference.TextLoaderOptions);
var trainData = textLoader.Load(datasetPath);
var result = context.Auto()
.CreateMulticlassClassificationExperiment(15)
.Execute(trainData, columnInference.ColumnInformation);
Assert.True(_markerAutoFitContextLogTest, "Image classification trainer logs from Experiment's sub contexts" +
"were not relayed to the main MLContext.");
}

[Fact]
public void AutoFitBinaryTest()
{
Expand Down