Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Dotnet Machinelearning AutoMLExperiment Run

From Leeroopedia


Knowledge Sources
Domains Machine_Learning, AutoML
Last Updated 2026-02-09 00:00 GMT

Overview

Concrete tool for executing an AutoML experiment search loop provided by ML.NET AutoML.

Description

The Run() and RunAsync() methods on AutoMLExperiment initiate the automated model search. Run() is a synchronous wrapper that blocks the calling thread until the experiment completes. RunAsync() is the core async implementation that accepts a CancellationToken for cooperative cancellation. Internally, the method loops through trials: the configured tuner proposes hyperparameter configurations, each trial trains a concrete pipeline, evaluates it against the configured metric, and reports back. The loop continues until the training time budget expires or the cancellation token is triggered. Upon completion, the method returns the TrialResult with the best metric observed across all trials.

Usage

Import Microsoft.ML.AutoML and call Run() or RunAsync() after the experiment is fully configured with a time budget, metric, dataset, and pipeline. Use Run() in console applications and scripts. Use RunAsync() with a CancellationToken in async contexts (web apps, UI apps) or when you need to support graceful early termination.

Code Reference

Source Location

  • Repository: ML.NET
  • File: src/Microsoft.ML.AutoML/AutoMLExperiment/AutoMLExperiment.cs (Lines 202-205 for Run, Lines 214-351 for RunAsync)

Signature

// Synchronous execution - blocks until complete
public TrialResult Run()

// Asynchronous execution with cancellation support
public Task<TrialResult> RunAsync(CancellationToken ct = default)

Import

using Microsoft.ML.AutoML;

I/O Contract

Inputs

Name Type Required Description
(implicit) AutoMLExperiment Yes A fully configured experiment instance (time budget, metric, dataset, and pipeline must all be set)
ct CancellationToken No (default: default) Token for cooperative cancellation of the search loop (RunAsync only)

Outputs

Name Type Description
(return from Run) TrialResult The best trial result found during the experiment, containing the trained model, metric, loss, and trial settings
(return from RunAsync) Task<TrialResult> A task that resolves to the best TrialResult upon experiment completion

Usage Examples

Basic Example

var mlContext = new MLContext();

// (Assume experiment is fully configured)
var experiment = mlContext.Auto()
    .CreateExperiment()
    .SetTrainingTimeInSeconds(60)
    .SetBinaryClassificationMetric(BinaryClassificationMetric.Accuracy)
    .SetDataset(trainData, validationData)
    .SetPipeline(pipeline);

// Synchronous execution - blocks until time budget expires
TrialResult bestResult = experiment.Run();

Console.WriteLine($"Best metric: {bestResult.Metric}");
Console.WriteLine($"Training duration: {bestResult.DurationInMilliseconds}ms");

Async with Cancellation Example

// Create a cancellation token source with a timeout
using var cts = new CancellationTokenSource();

// Async execution with cancellation support
TrialResult bestResult = await experiment.RunAsync(cts.Token);

Console.WriteLine($"Best metric: {bestResult.Metric}");

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment