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

반환되는 형식입니다 ITransformer . estimator

매개 변수

estimator
IEstimator<TTransformer>

래핑할 추정기

onFit
Action<TTransformer>

결과 TTransformer 인스턴스와 함께 호출된 대리자가 한 번 Fit(IDataView) 호출됩니다. 여러 번 호출될 수 있으므로 Fit(IDataView) 이 대리자를 여러 번 호출할 수도 있습니다.

반환

fit이 호출될 때마다 표시된 대리자를 호출하는 래핑 추정기

예제

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

적용 대상