LearningPipelineExtensions.WithOnFitDelegate<TTransformer> メソッド

定義

エスティメーターを指定すると、デリゲートが呼 Fit(IDataView) び出されると呼び出されるラップ オブジェクトを返します。 多くの場合、エスティメーターが適合した内容に関する情報を返すことが重要です。そのため Fit(IDataView) 、メソッドは一般的 ITransformerなオブジェクトではなく、具体的に型指定されたオブジェクトを返します。 ただし、同時に、 IEstimator<TTransformer> 多くのオブジェクトを含むパイプラインに形成されることが多いため、トランスフォーマーを取得する推定器がこのチェーンのどこかに埋もれている場所を介して EstimatorChain<TLastTransformer> 、推定器のチェーンを構築する必要がある場合があります。 このシナリオでは、このメソッドを使用して、fit が呼び出されると呼び出されるデリゲートをアタッチできます。

public static Microsoft.ML.IEstimator<TTransformer> WithOnFitDelegate<TTransformer> (this Microsoft.ML.IEstimator<TTransformer> estimator, Action<TTransformer> onFit) where TTransformer : class, Microsoft.ML.ITransformer;
static member WithOnFitDelegate : Microsoft.ML.IEstimator<'ransformer (requires 'ransformer : null and 'ransformer :> Microsoft.ML.ITransformer)> * Action<'ransformer (requires 'ransformer : null and 'ransformer :> Microsoft.ML.ITransformer)> -> Microsoft.ML.IEstimator<'ransformer (requires 'ransformer : null and 'ransformer :> Microsoft.ML.ITransformer)> (requires 'ransformer : null and 'ransformer :> Microsoft.ML.ITransformer)
<Extension()>
Public Function WithOnFitDelegate(Of TTransformer As {Class, ITransformer}) (estimator As IEstimator(Of TTransformer), onFit As Action(Of TTransformer)) As IEstimator(Of TTransformer)

型パラメーター

TTransformer

によって返される型ITransformerestimator

パラメーター

estimator
IEstimator<TTransformer>

ラップするエスティメーター

onFit
Action<TTransformer>

結果 TTransformer のインスタンスで 1 回 Fit(IDataView) 呼び出されるデリゲートが呼び出されます。 複数回呼び出される可能性があるため Fit(IDataView) 、このデリゲートは複数回呼び出されることもあります。

戻り値

適合が呼び出されるたびに指定されたデリゲートを呼び出すラップ推定器

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms;
using static Microsoft.ML.Transforms.NormalizingTransformer;

namespace Samples.Dynamic
{
    public class WithOnFitDelegate
    {
        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();
            var samples = new List<DataPoint>()
            {
                new DataPoint(){ Features = new float[4] { 8, 1, 3, 0},
                    Label = true },

                new DataPoint(){ Features = new float[4] { 6, 2, 2, 0},
                    Label = true },

                new DataPoint(){ Features = new float[4] { 4, 0, 1, 0},
                    Label = false },

                new DataPoint(){ Features = new float[4] { 2,-1,-1, 1},
                    Label = false }

            };
            // Convert training data to IDataView, the general data type used in
            // ML.NET.
            var data = mlContext.Data.LoadFromEnumerable(samples);

            // Create a pipeline to normalize the features and train a binary
            // classifier. We use WithOnFitDelegate for the intermediate binning
            // normalization step, so that we can inspect the properties of the
            // normalizer after fitting.
            NormalizingTransformer binningTransformer = null;
            var pipeline =
                mlContext.Transforms
                .NormalizeBinning("Features", maximumBinCount: 3)
                .WithOnFitDelegate(
                fittedTransformer => binningTransformer = fittedTransformer)
                .Append(mlContext.BinaryClassification.Trainers
                .LbfgsLogisticRegression());

            Console.WriteLine(binningTransformer == null);
            // Expected Output:
            //   True

            var model = pipeline.Fit(data);

            // During fitting binningTransformer will get assigned a new value
            Console.WriteLine(binningTransformer == null);
            // Expected Output:
            //   False

            // Inspect some of the properties of the binning transformer
            var binningParam = binningTransformer.GetNormalizerModelParameters(0) as
                BinNormalizerModelParameters<ImmutableArray<float>>;

            for (int i = 0; i < binningParam.UpperBounds.Length; i++)
            {
                var upperBounds = string.Join(", ", binningParam.UpperBounds[i]);
                Console.WriteLine(
                    $"Bin {i}: Density = {binningParam.Density[i]}, " +
                    $"Upper-bounds = {upperBounds}");

            }
            // Expected output:
            //   Bin 0: Density = 2, Upper-bounds = 3, 7, Infinity
            //   Bin 1: Density = 2, Upper-bounds = -0.5, 1.5, Infinity
            //   Bin 2: Density = 2, Upper-bounds = 0, 2.5, Infinity
            //   Bin 3: Density = 1, Upper-bounds = 0.5, Infinity
        }

        private class DataPoint
        {
            [VectorType(4)]
            public float[] Features { get; set; }
            public bool Label { get; set; }
        }
    }
}

適用対象