LearningModelSession Constructors

Definition

Overloads

LearningModelSession(LearningModel)

Creates a session using the default device.

LearningModelSession(LearningModel, LearningModelDevice)

Creates a session using the specified device.

LearningModelSession(LearningModel, LearningModelDevice, LearningModelSessionOptions)

Creates a session using the specified device and additional inference options.

LearningModelSession(LearningModel)

Creates a session using the default device.

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

Parameters

model
LearningModel

The trained machine learning model for this session.

Examples

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

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

Remarks

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.

Applies to

LearningModelSession(LearningModel, LearningModelDevice)

Creates a session using the specified device.

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)

Parameters

model
LearningModel

The trained machine learning model for this session.

deviceToRunOn
LearningModelDevice

The session's evaluation device.

Examples

The following example loads a model, selects the device on which to evaluate the model, and creates an evaluation session.

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

Remarks

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.

Applies to

LearningModelSession(LearningModel, LearningModelDevice, LearningModelSessionOptions)

Creates a session using the specified device and additional inference options.

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)

Parameters

model
LearningModel

The trained machine learning model for this session.

deviceToRunOn
LearningModelDevice

The session's evaluation device.

learningModelSessionOptions
LearningModelSessionOptions

The options used to configure session creation and evaluation.

Windows requirements

Device family
Windows 10, version 1903 (introduced in 10.0.18362.0)
API contract
Windows.AI.MachineLearning.MachineLearningContract (introduced in v2.0)

Examples

The following example loads a model and configures an evaluation session using 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;
    }
}

Applies to