LearningModel 类

定义

表示经过训练的机器学习模型。

public ref 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.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class LearningModel final : IClosable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.AI.MachineLearning.MachineLearningContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class LearningModel : System.IDisposable
Public NotInheritable Class LearningModel
Implements IDisposable
继承
Object Platform::Object IInspectable LearningModel
属性
实现

Windows 要求

设备系列
Windows 10, version 1809 (在 10.0.17763.0 中引入)
API contract
Windows.AI.MachineLearning.MachineLearningContract (在 v1.0 中引入)

示例

以下示例加载模型并使用它创建评估会话。

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;
    }
}

注解

这是用于与 Windows ML 交互的main对象。 使用它来加载、绑定和评估训练的 ONNX 模型:

  1. 使用 Load* 构造函数之一 加载模型。
  2. 枚举 InputFeaturesOutputFeatures 并 绑定到模型。
  3. 创建 LearningModelSession 并规避。

Windows Server

若要在 Windows Server 上使用此 API,必须使用具有桌面体验的 Windows Server 2019。

线程安全

此 API 是线程安全的。

属性

Author

模型作者的名称。

Description

模型的说明。

Domain

模型的域。

InputFeatures

模型的输入特征列表。

Metadata

来自 ONNX 模型的原始 <字符串、字符串> 元数据。

Name

模型的名称。

OutputFeatures

模型的输出特征列表。

Version

模型的版本。

方法

Close()

释放内存中的 ONNX 模型。 调用此对象后,无法再创建 LearningModelSession 对象。

Dispose()

执行与释放或重置非托管资源关联的应用程序定义的任务。

LoadFromFilePath(String)

从磁盘上的文件加载 ONNX 模型。

LoadFromFilePath(String, ILearningModelOperatorProvider)

从磁盘上的文件加载 ONNX 模型。

LoadFromStorageFileAsync(IStorageFile)

异步从 IStorageFile 加载 ONNX 模型。

LoadFromStorageFileAsync(IStorageFile, ILearningModelOperatorProvider)

异步从 IStorageFile 加载 ONNX 模型。

LoadFromStream(IRandomAccessStreamReference)

从流加载 ONNX 模型。

LoadFromStream(IRandomAccessStreamReference, ILearningModelOperatorProvider)

从流加载 ONNX 模型。

LoadFromStreamAsync(IRandomAccessStreamReference)

异步从流加载 ONNX 模型。

LoadFromStreamAsync(IRandomAccessStreamReference, ILearningModelOperatorProvider)

异步从流加载 ONNX 模型。

适用于

另请参阅