LearningModelSession 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
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;
}
}