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 TrialResult Properties

From Leeroopedia


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

Overview

Concrete tool for extracting the best model, metric, and metadata from an AutoML trial result provided by ML.NET AutoML.

Description

The TrialResult class encapsulates the outcome of the best trial found during an AutoML experiment. It exposes the trained model as an ITransformer via the Model property, the evaluation score via Metric, the tuner-internal loss via Loss, and the wall-clock training time via DurationInMilliseconds. The TrialSettings property contains the full hyperparameter configuration that produced this result. Optional resource monitoring properties PeakCpu and PeakMemoryInMegaByte report peak resource utilization during the trial. A generic variant TrialResult<TMetric> provides a strongly-typed metric object for richer evaluation details.

Usage

Import Microsoft.ML.AutoML to access TrialResult properties after calling Run() or RunAsync() on an AutoML experiment. Use Model to obtain the trained transformer for predictions or saving. Use Metric and Loss for evaluation reporting. Use TrialSettings for experiment logging and reproducibility.

Code Reference

Source Location

  • Repository: ML.NET
  • File: src/Microsoft.ML.AutoML/AutoMLExperiment/TrialResult.cs (Lines 15-49 for TrialResult, Lines 54-68 for TrialResult<TMetric>)

Signature

public class TrialResult
{
    // The trained model (best pipeline with optimal hyperparameters)
    public ITransformer Model { get; set; }

    // The evaluation metric value (e.g., accuracy, AUC)
    public double Metric { get; set; }

    // The loss value used by the tuner (lower is better)
    public double Loss { get; set; }

    // Wall-clock training duration for this trial
    public double DurationInMilliseconds { get; set; }

    // The hyperparameter configuration that produced this result
    public TrialSettings TrialSettings { get; set; }

    // Optional peak CPU utilization during the trial
    public double? PeakCpu { get; set; }

    // Optional peak memory usage in megabytes during the trial
    public double? PeakMemoryInMegaByte { get; set; }
}

public class TrialResult<TMetric> : TrialResult
{
    // Strongly-typed evaluation metrics object
    public TMetric Metrics { get; set; }
}

Import

using Microsoft.ML.AutoML;

I/O Contract

Inputs

Name Type Required Description
(implicit) TrialResult Yes The trial result object returned by Run() or RunAsync()

Outputs

Name Type Description
Model ITransformer The trained model with optimal algorithm and hyperparameters, ready for predictions or saving
Metric double The evaluation metric value (higher is better for metrics like Accuracy, AUC)
Loss double The internal loss value used by the tuner (lower is always better)
DurationInMilliseconds double Wall-clock time for this trial's training and evaluation
TrialSettings TrialSettings The complete hyperparameter configuration including algorithm choice
PeakCpu double? Optional peak CPU utilization percentage during the trial
PeakMemoryInMegaByte double? Optional peak memory consumption in megabytes during the trial

Usage Examples

Basic Example

// Run the experiment and get the best trial result
TrialResult bestResult = experiment.Run();

// Extract the trained model for predictions
ITransformer bestModel = bestResult.Model;

// Report the evaluation metric
Console.WriteLine($"Best metric (e.g., AUC): {bestResult.Metric:F4}");
Console.WriteLine($"Loss: {bestResult.Loss:F4}");
Console.WriteLine($"Duration: {bestResult.DurationInMilliseconds:F0}ms");

// Log resource usage if available
if (bestResult.PeakCpu.HasValue)
    Console.WriteLine($"Peak CPU: {bestResult.PeakCpu.Value:F1}%");
if (bestResult.PeakMemoryInMegaByte.HasValue)
    Console.WriteLine($"Peak Memory: {bestResult.PeakMemoryInMegaByte.Value:F1} MB");

// Save the best model to disk
mlContext.Model.Save(bestModel, data.Schema, "BestAutoMLModel.zip");

Trial Settings Inspection Example

// Inspect the hyperparameters that produced the best result
TrialResult bestResult = experiment.Run();
TrialSettings settings = bestResult.TrialSettings;

Console.WriteLine($"Trial ID: {settings.TrialId}");
Console.WriteLine($"Hyperparameters: {settings.Parameter}");

Related Pages

Implements Principle

Requires Environment

Page Connections

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