LearningModelSession 构造函数

定义

重载

LearningModelSession(LearningModel)

使用默认设备创建会话。

LearningModelSession(LearningModel, LearningModelDevice)

使用指定的设备创建会话。

LearningModelSession(LearningModel, LearningModelDevice, LearningModelSessionOptions)

使用指定的设备和其他推理选项创建会话。

LearningModelSession(LearningModel)

使用默认设备创建会话。

public:
 LearningModelSession(LearningModel ^ model);
 LearningModelSession(LearningModel const& model);
public LearningModelSession(LearningModel model);
function LearningModelSession(model)
Public Sub New (model As LearningModel)

参数

model
LearningModel

此会话的已训练机器学习模型。

示例

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

private async Task LoadModelAsync(LearningModel _model, string _modelFileName, LearningModelSession _session)
{
    // Only load the model one time.
    if (_model != null) return;

    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 and device.
        _session = new LearningModelSession(_model);
    }
    catch (Exception ex)
    {
        StatusBlock.Text = $"error: {ex.Message}";
        _model = null;
    }
}

注解

Windows Server

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

线程安全

此 API 是线程安全的。

适用于

LearningModelSession(LearningModel, LearningModelDevice)

使用指定的设备创建会话。

public:
 LearningModelSession(LearningModel ^ model, LearningModelDevice ^ deviceToRunOn);
 LearningModelSession(LearningModel const& model, LearningModelDevice const& deviceToRunOn);
public LearningModelSession(LearningModel model, LearningModelDevice deviceToRunOn);
function LearningModelSession(model, deviceToRunOn)
Public Sub New (model As LearningModel, deviceToRunOn As LearningModelDevice)

参数

model
LearningModel

此会话的已训练机器学习模型。

deviceToRunOn
LearningModelDevice

会话的评估设备。

示例

以下示例加载模型,选择要评估模型的设备,并创建评估会话。

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

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

        // Select the device to evaluate on
        LearningModelDevice device = null;
        if (_useGPU)
        {
            // Use a GPU or other DirectX device to evaluate the model.
            device = new LearningModelDevice(LearningModelDeviceKind.DirectX);
        }
        else
        {
            // Use the CPU to evaluate the model.
            device = new LearningModelDevice(LearningModelDeviceKind.Cpu);
        }

        // Create the evaluation session with the model and device.
        _session = new LearningModelSession(_model, device);

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

注解

Windows Server

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

线程安全

此 API 是线程安全的。

适用于

LearningModelSession(LearningModel, LearningModelDevice, LearningModelSessionOptions)

使用指定的设备和其他推理选项创建会话。

public:
 LearningModelSession(LearningModel ^ model, LearningModelDevice ^ deviceToRunOn, LearningModelSessionOptions ^ learningModelSessionOptions);
 LearningModelSession(LearningModel const& model, LearningModelDevice const& deviceToRunOn, LearningModelSessionOptions const& learningModelSessionOptions);
public LearningModelSession(LearningModel model, LearningModelDevice deviceToRunOn, LearningModelSessionOptions learningModelSessionOptions);
function LearningModelSession(model, deviceToRunOn, learningModelSessionOptions)
Public Sub New (model As LearningModel, deviceToRunOn As LearningModelDevice, learningModelSessionOptions As LearningModelSessionOptions)

参数

model
LearningModel

此会话的已训练机器学习模型。

deviceToRunOn
LearningModelDevice

会话的评估设备。

learningModelSessionOptions
LearningModelSessionOptions

用于配置会话创建和评估的选项。

Windows 要求

设备系列
Windows 10, version 1903 (在 10.0.18362.0 中引入)
API contract
Windows.AI.MachineLearning.MachineLearningContract (在 v2.0 中引入)

示例

以下示例使用 LearningModelSessionOptions 加载模型并配置评估会话。

private LearningModelSessionOptions CreateSessionOptions()
{
    var options = new LearningModelSessionOptions();

    // Disable constant batch size optimizations
    options.BatchSizeOverride = 0;

    return options;
}

private async Task LoadModelAsync(string modelFileName)
{
    LearningModel model;
    LearningModelDevice device;
    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 default LearningModelDevice.
        device = new LearningModelDevice(LearningModelDeviceKind.Default);

        // Create LearningModelSessionOptions with necessary options set.
        LearningModelSessionOptions options = CreateSessionOptions();

        // Create the evaluation session with the model and LearningModelSessionOptions.
        session = new LearningModelSession(model, device, options);

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

适用于