保存和加载经过训练的模型Save and load trained models

了解如何在应用程序中保存和加载经过训练的模型。Learn how to save and load trained models in your application.

在整个模型生成过程中,模型位于内存中,并且可以在整个应用程序生命周期中访问。Throughout the model building process, a model lives in memory and is accessible throughout the application's lifecycle. 但是,一旦应用程序停止运行,而模型未在本地或远程的某个位置保存,则无法再访问该模型。However, once the application stops running, if the model is not saved somewhere locally or remotely, it's no longer accessible. 通常情况下,在其他应用程序中训练模型之后,某些时候会使用模型进行推理或重新训练。Typically models are used at some point after training in other applications either for inference or re-training. 因此,存储模型很重要。Therefore, it's important to store the model. 使用类似下文详述的数据准备和模型训练管道时,请使用本文档后续部分中介绍的步骤保存和加载模型。Save and load models using the steps described in subsequent sections of this document when using data preparation and model training pipelines like the one detailed below. 虽然此示例使用线性回归模型,但相同的过程适用于其他 ML.NET 算法。Although this sample uses a linear regression model, the same process applies to other ML.NET algorithms.

HousingData[] housingData = new HousingData[]
{
    new HousingData
    {
        Size = 600f,
        HistoricalPrices = new float[] { 100000f, 125000f, 122000f },
        CurrentPrice = 170000f
    },
    new HousingData
    {
        Size = 1000f,
        HistoricalPrices = new float[] { 200000f, 250000f, 230000f },
        CurrentPrice = 225000f
    },
    new HousingData
    {
        Size = 1000f,
        HistoricalPrices = new float[] { 126000f, 130000f, 200000f },
        CurrentPrice = 195000f
    }
};

// Create MLContext
MLContext mlContext = new MLContext();

// Load Data
IDataView data = mlContext.Data.LoadFromEnumerable<HousingData>(housingData);

// Define data preparation estimator
EstimatorChain<RegressionPredictionTransformer<LinearRegressionModelParameters>> pipelineEstimator =
    mlContext.Transforms.Concatenate("Features", new string[] { "Size", "HistoricalPrices" })
        .Append(mlContext.Transforms.NormalizeMinMax("Features"))
        .Append(mlContext.Regression.Trainers.Sdca());

// Train model
ITransformer trainedModel = pipelineEstimator.Fit(data);

// Save model
mlContext.Model.Save(trainedModel, data.Schema, "model.zip");

由于大部分模型和数据准备管道都继承自同一组类,这些组件的保存和加载方法签名相同。Because most models and data preparation pipelines inherit from the same set of classes, the save and load method signatures for these components is the same. 根据用例,可以将数据准备管道和模型合并为单个 EstimatorChain(输出单个 ITransformer),也可将它们分隔,从而为其各自创建单独的 ITransformerDepending on your use case, you can either combine the data preparation pipeline and model into a single EstimatorChain which would output a single ITransformer or separate them thus creating a separate ITransformer for each.

在本地保存模型Save a model locally

保存模型时,需要以下两项:When saving a model you need two things:

  1. 模型的 ITransformerThe ITransformer of the model.
  2. ITransformer 预期输入的 DataViewSchemaThe DataViewSchema of the ITransformer's expected input.

训练模型后,通过 Save 方法使用输入数据的 DataViewSchema 将经过训练的模型保存到名为 model.zip 的文件中。After training the model, use the Save method to save the trained model to a file called model.zip using the DataViewSchema of the input data.

// Save Trained Model
mlContext.Model.Save(trainedModel, data.Schema, "model.zip");

加载本地存储的模型Load a model stored locally

本地存储的模型可用于其他进程或应用程序,如 ASP.NET CoreServerless Web ApplicationsModels stored locally can be used in other processes or applications like ASP.NET Core and Serverless Web Applications. 若要了解详细信息,请参阅在 Web API 中使用 ML.NET部署 ML.NET 无服务器 Web 应用操作说明文章。See Use ML.NET in Web API and Deploy ML.NET Serverless Web App how-to articles to learn more.

在单独的应用程序或进程中,配合使用 Load 方法和文件路径将经过训练的模型载入应用程序。In a separate application or process, use the Load method along with the file path to get the trained model into your application.

//Define DataViewSchema for data preparation pipeline and trained model
DataViewSchema modelSchema;

// Load trained model
ITransformer trainedModel = mlContext.Model.Load("model.zip", out modelSchema);

加载远程存储的模型Load a model stored remotely

若要将存储在远程位置的数据准备管道和模型加载到应用程序中,请使用 Stream,而不要使用 Load 方法中的文件路径。To load data preparation pipelines and models stored in a remote location into your application, use a Stream instead of a file path in the Load method.

// Create MLContext
MLContext mlContext = new MLContext();

// Define DataViewSchema and ITransformers
DataViewSchema modelSchema;
ITransformer trainedModel;

// Load data prep pipeline and trained model
using (HttpClient client = new HttpClient())
{
    Stream modelFile = await client.GetStreamAsync("<YOUR-REMOTE-FILE-LOCATION>");

    trainedModel = mlContext.Model.Load(modelFile, out modelSchema);
}

使用单独的数据准备和模型管道Working with separate data preparation and model pipelines

备注

使用单独的数据准备和模型训练管道是可选方案。Working with separate data preparation and model training pipelines is optional. 管道的分离使得用户能够更轻松地检查已学习的模型参数。Separation of pipelines makes it easier to inspect the learned model parameters. 对于预测,保存和加载包含数据准备和模型训练操作的单个管道更轻松。For predictions, it's easier to save and load a single pipeline that includes the data preparation and model training operations.

使用单独的数据准备管道和模型时,流程与单个管道相同;但现在需要同时保存和加载两个管道。When working with separate data preparation pipelines and models, the same process as single pipelines applies; except now both pipelines need to be saved and loaded simultaneously.

给定单独的数据准备和模型训练管道:Given separate data preparation and model training pipelines:

// Define data preparation estimator
IEstimator<ITransformer> dataPrepEstimator =
    mlContext.Transforms.Concatenate("Features", new string[] { "Size", "HistoricalPrices" })
        .Append(mlContext.Transforms.NormalizeMinMax("Features"));

// Create data preparation transformer
ITransformer dataPrepTransformer = dataPrepEstimator.Fit(data);

// Define StochasticDualCoordinateAscent regression algorithm estimator
var sdcaEstimator = mlContext.Regression.Trainers.Sdca();

// Pre-process data using data prep operations
IDataView transformedData = dataPrepTransformer.Transform(data);

// Train regression model
RegressionPredictionTransformer<LinearRegressionModelParameters> trainedModel = sdcaEstimator.Fit(transformedData);

保存数据准备管道和经过训练的模型Save data preparation pipeline and trained model

若要保存数据准备管道和经过训练的模型,请使用以下命令:To save both the data preparation pipeline and trained model, use the following commands:

// Save Data Prep transformer
mlContext.Model.Save(dataPrepTransformer, data.Schema, "data_preparation_pipeline.zip");

// Save Trained Model
mlContext.Model.Save(trainedModel, transformedData.Schema, "model.zip");

加载数据准备管道和经过训练的模型Load data preparation pipeline and trained model

在单独的进程或应用程序中,同时加载数据准备管道和经过训练的模型,如下所示:In a separate process or application, load the data preparation pipeline and trained model simultaneously as follows:

// Create MLContext
MLContext mlContext = new MLContext();

// Define data preparation and trained model schemas
DataViewSchema dataPrepPipelineSchema, modelSchema;

// Load data preparation pipeline and trained model
ITransformer dataPrepPipeline = mlContext.Model.Load("data_preparation_pipeline.zip",out dataPrepPipelineSchema);
ITransformer trainedModel = mlContext.Model.Load("model.zip", out modelSchema);