LearningModel Class

Definition

Represents a trained machine learning model.

public ref class LearningModel sealed : IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.AI.MachineLearning.MachineLearningContract, 65536)]
/// [Windows.Foundation.Metadata.DualApiPartition(version=167772166)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Static(Windows.AI.MachineLearning.ILearningModelStatics, 65536, "Windows.AI.MachineLearning.MachineLearningContract")]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class LearningModel sealed : IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.AI.MachineLearning.MachineLearningContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Static(Windows.AI.MachineLearning.ILearningModelStatics, 65536, "Windows.AI.MachineLearning.MachineLearningContract")]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.DualApiPartition(version=1)]
class LearningModel sealed : IClosable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.AI.MachineLearning.MachineLearningContract), 65536)]
[Windows.Foundation.Metadata.DualApiPartition(version=167772166)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Static(typeof(Windows.AI.MachineLearning.ILearningModelStatics), 65536, "Windows.AI.MachineLearning.MachineLearningContract")]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class LearningModel : System.IDisposable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.AI.MachineLearning.MachineLearningContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Static(typeof(Windows.AI.MachineLearning.ILearningModelStatics), 65536, "Windows.AI.MachineLearning.MachineLearningContract")]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.DualApiPartition(version=1)]
public sealed class LearningModel : System.IDisposable
Public NotInheritable Class LearningModel
Implements IDisposable
Inheritance
LearningModel
Attributes
Implements

Windows 10 requirements

Device family
Windows 10, version 1809 (introduced in 10.0.17763.0)
API contract
Windows.AI.MachineLearning.MachineLearningContract (introduced in v1.0)

Examples

The following example loads a model and creates an evaluation session with it.

private async Task LoadModelAsync(string _modelFileName)
{
    LearningModel _model;
    LearningModelSession _session;

    try
    {
        // Load and create the model
        var modelFile = 
            await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_modelFileName}"));
        _model = await LearningModel.LoadFromStorageFileAsync(modelFile);

        // Create the evaluation session with the model
        _session = new LearningModelSession(_model);

    }
    catch (Exception ex)
    {
        StatusBlock.Text = $"error: {ex.Message}";
        _model = null;
    }
}

Remarks

This is the main object you use to interact with Windows ML. You use it to load, bind, and evaluate trained ONNX models:

  1. Load the model using one of the Load* constructors.
  2. Enumerate the InputFeatures and OutputFeatures and bind to your model.
  3. Create a LearningModelSession and evalaute.

Windows Server

To use this API on Windows Server, you must use Windows Server 2019 with Desktop Experience.

Thread safety

This API is thread-safe.

Properties

Author

The name of the model author.

Description

A description of the model.

Domain

The domain of the model.

InputFeatures

A list of the model's input features.

Metadata

The raw <string,string> metadata from the ONNX model.

Name

The name of the model.

OutputFeatures

A list of the model's output features.

Version

The version of the model.

Methods

Close()

Releases the in-memory ONNX model. After calling this, you cannot create any more LearningModelSession objects.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

LoadFromFilePath(String)

Loads an ONNX model from a file on disk.

LoadFromFilePath(String, ILearningModelOperatorProvider)

Loads an ONNX model from a file on disk.

LoadFromStorageFileAsync(IStorageFile)

Loads an ONNX model from an IStorageFile asynchronously.

LoadFromStorageFileAsync(IStorageFile, ILearningModelOperatorProvider)

Loads an ONNX model from an IStorageFile asynchronously.

LoadFromStream(IRandomAccessStreamReference)

Loads an ONNX model from a stream.

LoadFromStream(IRandomAccessStreamReference, ILearningModelOperatorProvider)

Loads an ONNX model from a stream.

LoadFromStreamAsync(IRandomAccessStreamReference)

Loads an ONNX model from a stream asynchronously.

LoadFromStreamAsync(IRandomAccessStreamReference, ILearningModelOperatorProvider)

Loads an ONNX model from a stream asynchronously.

Applies to

See also