Dela via


ModelOperationsCatalog.CreatePredictionEngine Method

Definition

Overloads

CreatePredictionEngine<TSrc,TDst>(ITransformer, DataViewSchema)

Create a prediction engine for one-time prediction. It's mainly used in conjunction with Load(Stream, DataViewSchema), where input schema is extracted during loading the model.

CreatePredictionEngine<TSrc,TDst>(ITransformer, PredictionEngineOptions)

Create a prediction engine for one-time prediction. It's mainly used in conjunction with Load(Stream, DataViewSchema), where input schema is extracted during loading the model.

CreatePredictionEngine<TSrc,TDst>(ITransformer, Boolean, SchemaDefinition, SchemaDefinition)

Create a prediction engine for one-time prediction (default usage).

CreatePredictionEngine<TSrc,TDst>(ITransformer, DataViewSchema)

Create a prediction engine for one-time prediction. It's mainly used in conjunction with Load(Stream, DataViewSchema), where input schema is extracted during loading the model.

public Microsoft.ML.PredictionEngine<TSrc,TDst> CreatePredictionEngine<TSrc,TDst> (Microsoft.ML.ITransformer transformer, Microsoft.ML.DataViewSchema inputSchema) where TSrc : class where TDst : class, new();
member this.CreatePredictionEngine : Microsoft.ML.ITransformer * Microsoft.ML.DataViewSchema -> Microsoft.ML.PredictionEngine<'Src, 'Dst (requires 'Src : null and 'Dst : null and 'Dst : (new : unit -> 'Dst))> (requires 'Src : null and 'Dst : null and 'Dst : (new : unit -> 'Dst))
Public Function CreatePredictionEngine(Of TSrc As Class, TDst As Class) (transformer As ITransformer, inputSchema As DataViewSchema) As PredictionEngine(Of TSrc, TDst)

Type Parameters

TSrc

The class that defines the input data.

TDst

The class that defines the output data.

Parameters

transformer
ITransformer

The transformer to use for prediction.

inputSchema
DataViewSchema

Input schema.

Returns

Applies to

CreatePredictionEngine<TSrc,TDst>(ITransformer, PredictionEngineOptions)

Create a prediction engine for one-time prediction. It's mainly used in conjunction with Load(Stream, DataViewSchema), where input schema is extracted during loading the model.

public Microsoft.ML.PredictionEngine<TSrc,TDst> CreatePredictionEngine<TSrc,TDst> (Microsoft.ML.ITransformer transformer, Microsoft.ML.PredictionEngineOptions options) where TSrc : class where TDst : class, new();
member this.CreatePredictionEngine : Microsoft.ML.ITransformer * Microsoft.ML.PredictionEngineOptions -> Microsoft.ML.PredictionEngine<'Src, 'Dst (requires 'Src : null and 'Dst : null and 'Dst : (new : unit -> 'Dst))> (requires 'Src : null and 'Dst : null and 'Dst : (new : unit -> 'Dst))
Public Function CreatePredictionEngine(Of TSrc As Class, TDst As Class) (transformer As ITransformer, options As PredictionEngineOptions) As PredictionEngine(Of TSrc, TDst)

Type Parameters

TSrc

The class that defines the input data.

TDst

The class that defines the output data.

Parameters

transformer
ITransformer

The transformer to use for prediction.

options
PredictionEngineOptions

Advanced configuration options.

Returns

Applies to

CreatePredictionEngine<TSrc,TDst>(ITransformer, Boolean, SchemaDefinition, SchemaDefinition)

Create a prediction engine for one-time prediction (default usage).

public Microsoft.ML.PredictionEngine<TSrc,TDst> CreatePredictionEngine<TSrc,TDst> (Microsoft.ML.ITransformer transformer, bool ignoreMissingColumns = true, Microsoft.ML.Data.SchemaDefinition inputSchemaDefinition = default, Microsoft.ML.Data.SchemaDefinition outputSchemaDefinition = default) where TSrc : class where TDst : class, new();
member this.CreatePredictionEngine : Microsoft.ML.ITransformer * bool * Microsoft.ML.Data.SchemaDefinition * Microsoft.ML.Data.SchemaDefinition -> Microsoft.ML.PredictionEngine<'Src, 'Dst (requires 'Src : null and 'Dst : null and 'Dst : (new : unit -> 'Dst))> (requires 'Src : null and 'Dst : null and 'Dst : (new : unit -> 'Dst))
Public Function CreatePredictionEngine(Of TSrc As Class, TDst As Class) (transformer As ITransformer, Optional ignoreMissingColumns As Boolean = true, Optional inputSchemaDefinition As SchemaDefinition = Nothing, Optional outputSchemaDefinition As SchemaDefinition = Nothing) As PredictionEngine(Of TSrc, TDst)

Type Parameters

TSrc

The class that defines the input data.

TDst

The class that defines the output data.

Parameters

transformer
ITransformer

The transformer to use for prediction.

ignoreMissingColumns
Boolean

Whether to throw an exception if a column exists in outputSchemaDefinition but the corresponding member doesn't exist in TDst.

inputSchemaDefinition
SchemaDefinition

Additional settings of the input schema.

outputSchemaDefinition
SchemaDefinition

Additional settings of the output schema.

Returns

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