ModelOperationsCatalog.Load 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
Load(Stream, DataViewSchema) |
從資料流程載入模型及其輸入架構。 |
Load(String, DataViewSchema) |
從檔案載入模型及其輸入架構。 |
Load(Stream, DataViewSchema)
從資料流程載入模型及其輸入架構。
public Microsoft.ML.ITransformer Load (System.IO.Stream stream, out Microsoft.ML.DataViewSchema inputSchema);
member this.Load : System.IO.Stream * DataViewSchema -> Microsoft.ML.ITransformer
Public Function Load (stream As Stream, ByRef inputSchema As DataViewSchema) As ITransformer
參數
- stream
- Stream
要從中載入的可讀取可搜尋資料流程。
- inputSchema
- DataViewSchema
將包含模型的輸入架構。 如果模型儲存時沒有輸入的任何描述,則不會有任何輸入架構。 在此情況下,這可以是 null
。
傳回
載入的模型。
範例
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.ML;
namespace Samples.Dynamic.ModelOperations
{
public class SaveLoadModel
{
public static void Example()
{
// Create a new ML context, for ML.NET operations. It can be used for
// exception tracking and logging, as well as the source of randomness.
var mlContext = new MLContext();
// Generate sample data.
var data = new List<Data>()
{
new Data() { Value="abc" }
};
// Convert data to IDataView.
var dataView = mlContext.Data.LoadFromEnumerable(data);
var inputColumnName = nameof(Data.Value);
var outputColumnName = nameof(Transformation.Key);
// Transform.
ITransformer model = mlContext.Transforms.Conversion
.MapValueToKey(outputColumnName, inputColumnName).Fit(dataView);
// Save model.
mlContext.Model.Save(model, dataView.Schema, "model.zip");
// Load model.
using (var file = File.OpenRead("model.zip"))
model = mlContext.Model.Load(file, out DataViewSchema schema);
// Create a prediction engine from the model for feeding new data.
var engine = mlContext.Model
.CreatePredictionEngine<Data, Transformation>(model);
var transformation = engine.Predict(new Data() { Value = "abc" });
// Print transformation to console.
Console.WriteLine("Value: {0}\t Key:{1}", transformation.Value,
transformation.Key);
// Value: abc Key:1
}
private class Data
{
public string Value { get; set; }
}
private class Transformation
{
public string Value { get; set; }
public uint Key { get; set; }
}
}
}
適用於
Load(String, DataViewSchema)
從檔案載入模型及其輸入架構。
public Microsoft.ML.ITransformer Load (string filePath, out Microsoft.ML.DataViewSchema inputSchema);
member this.Load : string * DataViewSchema -> Microsoft.ML.ITransformer
Public Function Load (filePath As String, ByRef inputSchema As DataViewSchema) As ITransformer
參數
- filePath
- String
應該從中讀取模型之檔案的路徑。
- inputSchema
- DataViewSchema
將包含模型的輸入架構。 如果模型儲存時沒有輸入的任何描述,則不會有任何輸入架構。 在此情況下,這可以是 null
。
傳回
載入的模型。
範例
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.ML;
namespace Samples.Dynamic.ModelOperations
{
public class SaveLoadModelFile
{
public static void Example()
{
// Create a new ML context, for ML.NET operations. It can be used for
// exception tracking and logging, as well as the source of randomness.
var mlContext = new MLContext();
// Generate sample data.
var data = new List<Data>()
{
new Data() { Value="abc" }
};
// Convert data to IDataView.
var dataView = mlContext.Data.LoadFromEnumerable(data);
var inputColumnName = nameof(Data.Value);
var outputColumnName = nameof(Transformation.Key);
// Transform.
ITransformer model = mlContext.Transforms.Conversion
.MapValueToKey(outputColumnName, inputColumnName).Fit(dataView);
// Save model.
mlContext.Model.Save(model, dataView.Schema, "model.zip");
// Load model.
model = mlContext.Model.Load("model.zip", out DataViewSchema schema);
// Create a prediction engine from the model for feeding new data.
var engine = mlContext.Model
.CreatePredictionEngine<Data, Transformation>(model);
var transformation = engine.Predict(new Data() { Value = "abc" });
// Print transformation to console.
Console.WriteLine("Value: {0}\t Key:{1}", transformation.Value,
transformation.Key);
// Value: abc Key:1
}
private class Data
{
public string Value { get; set; }
}
private class Transformation
{
public string Value { get; set; }
public uint Key { get; set; }
}
}
}