Delen via


ModelOperationsCatalog.Save Method

Definition

Overloads

Save(ITransformer, DataViewSchema, Stream)

Save a transformer model and the schema of the data that was used to train it to the stream.

Save(ITransformer, DataViewSchema, String)

Save a transformer model and the schema of the data that was used to train it to the file.

Save<TSource>(ITransformer, IDataLoader<TSource>, Stream)

Save a transformer model and the loader used to create its input data to the stream.

Save<TSource>(ITransformer, IDataLoader<TSource>, String)

Save a transformer model and the loader used to create its input data to the file.

Save(ITransformer, DataViewSchema, Stream)

Save a transformer model and the schema of the data that was used to train it to the stream.

public void Save (Microsoft.ML.ITransformer model, Microsoft.ML.DataViewSchema inputSchema, System.IO.Stream stream);
member this.Save : Microsoft.ML.ITransformer * Microsoft.ML.DataViewSchema * System.IO.Stream -> unit
Public Sub Save (model As ITransformer, inputSchema As DataViewSchema, stream As Stream)

Parameters

model
ITransformer

The trained model to be saved. Note that this can be null, as a shorthand for an empty transformer chain. Upon loading with Load(Stream, DataViewSchema) the returned value will be an empty TransformerChain<TLastTransformer>.

inputSchema
DataViewSchema

The schema of the input to the transformer. This can be null.

stream
Stream

A writeable, seekable stream to save to.

Applies to

Save(ITransformer, DataViewSchema, String)

Save a transformer model and the schema of the data that was used to train it to the file.

public void Save (Microsoft.ML.ITransformer model, Microsoft.ML.DataViewSchema inputSchema, string filePath);
member this.Save : Microsoft.ML.ITransformer * Microsoft.ML.DataViewSchema * string -> unit
Public Sub Save (model As ITransformer, inputSchema As DataViewSchema, filePath As String)

Parameters

model
ITransformer

The trained model to be saved. Note that this can be null, as a shorthand for an empty transformer chain. Upon loading with Load(Stream, DataViewSchema) the returned value will be an empty TransformerChain<TLastTransformer>.

inputSchema
DataViewSchema

The schema of the input to the transformer. This can be null.

filePath
String

Path where model should be saved.

Examples

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

Applies to

Save<TSource>(ITransformer, IDataLoader<TSource>, Stream)

Save a transformer model and the loader used to create its input data to the stream.

public void Save<TSource> (Microsoft.ML.ITransformer model, Microsoft.ML.IDataLoader<TSource> loader, System.IO.Stream stream);
member this.Save : Microsoft.ML.ITransformer * Microsoft.ML.IDataLoader<'Source> * System.IO.Stream -> unit
Public Sub Save(Of TSource) (model As ITransformer, loader As IDataLoader(Of TSource), stream As Stream)

Type Parameters

TSource

Parameters

model
ITransformer

The trained model to be saved. Note that this can be null, as a shorthand for an empty transformer chain. Upon loading with LoadWithDataLoader(Stream, IDataLoader<IMultiStreamSource>) the returned value will be an empty TransformerChain<TLastTransformer>.

loader
IDataLoader<TSource>

The loader that was used to create data to train the model.

stream
Stream

A writeable, seekable stream to save to.

Applies to

Save<TSource>(ITransformer, IDataLoader<TSource>, String)

Save a transformer model and the loader used to create its input data to the file.

public void Save<TSource> (Microsoft.ML.ITransformer model, Microsoft.ML.IDataLoader<TSource> loader, string filePath);
member this.Save : Microsoft.ML.ITransformer * Microsoft.ML.IDataLoader<'Source> * string -> unit
Public Sub Save(Of TSource) (model As ITransformer, loader As IDataLoader(Of TSource), filePath As String)

Type Parameters

TSource

Parameters

model
ITransformer

The trained model to be saved. Note that this can be null, as a shorthand for an empty transformer chain. Upon loading with LoadWithDataLoader(Stream, IDataLoader<IMultiStreamSource>) the returned value will be an empty TransformerChain<TLastTransformer>.

loader
IDataLoader<TSource>

The loader that was used to create data to train the model.

filePath
String

Path where model should be saved.

Applies to