Search⌘ K
AI Features

Solution Review: Configuring a Custom AutoML Monitor

Explore how to configure and integrate a custom AutoML monitor in your ML.NET project. Learn to modify the ReportBestTrial method, instantiate the monitor, and attach it to experiments to enhance monitoring during automated machine learning tasks.

The complete solution is available in the following playground:

using Microsoft.ML.AutoML;

namespace AutoMlCustomMonitor;

internal class CustomMonitor : IMonitor
{
    private readonly SweepablePipeline _pipeline;
    private readonly IEnumerable<TrialResult> _completedTrials;

    public CustomMonitor(SweepablePipeline pipeline)
    {
        _pipeline = pipeline;
        _completedTrials = new List<TrialResult>();
    }

    public IEnumerable<TrialResult> GetCompletedTrials() => _completedTrials;

    public void ReportBestTrial(TrialResult result)
    {
        var trialId = result.TrialSettings.TrialId;
        var timeToTrain = result.DurationInMilliseconds;
        var pipeline = _pipeline.ToString(result.TrialSettings.Parameter);
        Console.WriteLine($"The best trial is {trialId}. Finished training in {timeToTrain}ms with pipeline {pipeline}");
    }

    public void ReportCompletedTrial(TrialResult result)
    {
        var trialId = result.TrialSettings.TrialId;
        var timeToTrain = result.DurationInMilliseconds;
        var pipeline = _pipeline.ToString(result.TrialSettings.Parameter);
        Console.WriteLine($"Trial {trialId} finished training in {timeToTrain}ms with pipeline {pipeline}");
    }

    public void ReportFailTrial(TrialSettings settings, Exception exception = null)
    {
        if (exception.Message.Contains("Operation was canceled."))
        {
            Console.WriteLine($"{settings.TrialId} cancelled. Time budget exceeded.");
        }
        Console.WriteLine($"{settings.TrialId} failed with exception {exception.Message}");
    }

    public void ReportRunningTrial(TrialSettings setting)
    {
        return;
    }
}
The complete solution with an improved AutoML monitor

Solving the challenge

Here are the steps we take to complete specific ...