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)

Source:
ModelOperationsCatalog.cs
Source:
ModelOperationsCatalog.cs
Source:
ModelOperationsCatalog.cs

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.

C#
public Microsoft.ML.PredictionEngine<TSrc,TDst> CreatePredictionEngine<TSrc,TDst>(Microsoft.ML.ITransformer transformer, Microsoft.ML.DataViewSchema inputSchema) where TSrc : class where TDst : class, new();

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

ML.NET Preview and other versions
Product Versions
ML.NET 1.0.0, 1.1.0, 1.2.0, 1.3.1, 1.4.0, 1.5.0, 1.6.0, 1.7.0, 2.0.0, 3.0.0, 4.0.0, Preview

CreatePredictionEngine<TSrc,TDst>(ITransformer, PredictionEngineOptions)

Source:
ModelOperationsCatalog.cs
Source:
ModelOperationsCatalog.cs
Source:
ModelOperationsCatalog.cs

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.

C#
public Microsoft.ML.PredictionEngine<TSrc,TDst> CreatePredictionEngine<TSrc,TDst>(Microsoft.ML.ITransformer transformer, Microsoft.ML.PredictionEngineOptions options) where TSrc : class where TDst : class, new();

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

ML.NET Preview and other versions
Product Versions
ML.NET 1.7.0, 2.0.0, 3.0.0, 4.0.0, Preview

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

Source:
ModelOperationsCatalog.cs
Source:
ModelOperationsCatalog.cs
Source:
ModelOperationsCatalog.cs

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

C#
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();

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

C#
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

ML.NET Preview and other versions
Product Versions
ML.NET 1.0.0, 1.1.0, 1.2.0, 1.3.1, 1.4.0, 1.5.0, 1.6.0, 1.7.0, 2.0.0, 3.0.0, 4.0.0, Preview