ModelOperationsCatalog.Load 메서드

정의

오버로드

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

적용 대상