Esempio n. 1
0
        public static CommonOutputs.AnomalyDetectionOutput CreateAnomalyPipelineEnsemble(IHostEnvironment env, PipelineAnomalyInput input)
        {
            Contracts.CheckValue(env, nameof(env));
            var host = env.Register("CombineModels");

            host.CheckValue(input, nameof(input));
            host.CheckNonEmpty(input.Models, nameof(input.Models));

            IRegressionOutputCombiner combiner;

            switch (input.ModelCombiner)
            {
            case ScoreCombiner.Median:
                combiner = new Median(host);
                break;

            case ScoreCombiner.Average:
                combiner = new Average(host);
                break;

            default:
                throw host.Except("Unknown combiner kind");
            }
            var ensemble = SchemaBindablePipelineEnsembleBase.Create(host, input.Models, combiner, AnnotationUtils.Const.ScoreColumnKind.AnomalyDetection);

            return(CreatePipelineEnsemble <CommonOutputs.AnomalyDetectionOutput>(host, input.Models, ensemble));
        }
Esempio n. 2
0
        public static CommonOutputs.BinaryClassificationOutput CreateBinaryPipelineEnsemble(IHostEnvironment env, PipelineClassifierInput input)
        {
            Contracts.CheckValue(env, nameof(env));
            var host = env.Register("CombineModels");

            host.CheckValue(input, nameof(input));
            host.CheckNonEmpty(input.Models, nameof(input.Models));

            IBinaryOutputCombiner combiner;

            switch (input.ModelCombiner)
            {
            case ClassifierCombiner.Median:
                combiner = new Median(host);
                break;

            case ClassifierCombiner.Average:
                combiner = new Average(host);
                break;

            case ClassifierCombiner.Vote:
                combiner = new Voting(host);
                break;

            default:
                throw host.Except("Unknown combiner kind");
            }
            var ensemble = SchemaBindablePipelineEnsembleBase.Create(host, input.Models, combiner, MetadataUtils.Const.ScoreColumnKind.BinaryClassification);

            return(CreatePipelineEnsemble <CommonOutputs.BinaryClassificationOutput>(host, input.Models, ensemble));
        }
Esempio n. 3
0
            public BoundBase(SchemaBindablePipelineEnsembleBase parent, RoleMappedSchema schema)
            {
                Parent = parent;
                InputRoleMappedSchema = schema;
                OutputSchema          = ScoreSchemaFactory.Create(Parent.ScoreType, Parent._scoreColumnKind);
                _inputColIndices      = new HashSet <int>();
                for (int i = 0; i < Parent._inputCols.Length; i++)
                {
                    var name = Parent._inputCols[i];
                    var col  = InputRoleMappedSchema.Schema.GetColumnOrNull(name);
                    if (!col.HasValue)
                    {
                        throw Parent.Host.ExceptSchemaMismatch(nameof(InputRoleMappedSchema), "input", name);
                    }
                    _inputColIndices.Add(col.Value.Index);
                }

                Mappers        = new ISchemaBoundRowMapper[Parent.PredictorModels.Length];
                BoundPipelines = new IRowToRowMapper[Parent.PredictorModels.Length];
                ScoreCols      = new int[Parent.PredictorModels.Length];
                for (int i = 0; i < Mappers.Length; i++)
                {
                    // Get the RoleMappedSchema to pass to the predictor.
                    var emptyDv = new EmptyDataView(Parent.Host, schema.Schema);
                    Parent.PredictorModels[i].PrepareData(Parent.Host, emptyDv, out RoleMappedData rmd, out IPredictor predictor);

                    // Get the predictor as a bindable mapper, and bind it to the RoleMappedSchema found above.
                    var bindable = ScoreUtils.GetSchemaBindableMapper(Parent.Host, Parent.PredictorModels[i].Predictor);
                    Mappers[i] = bindable.Bind(Parent.Host, rmd.Schema) as ISchemaBoundRowMapper;
                    if (Mappers[i] == null)
                    {
                        throw Parent.Host.Except("Predictor {0} is not a row to row mapper", i);
                    }

                    // Make sure there is a score column, and remember its index.
                    var scoreCol = Mappers[i].OutputSchema.GetColumnOrNull(MetadataUtils.Const.ScoreValueKind.Score);
                    if (!scoreCol.HasValue)
                    {
                        throw Parent.Host.Except("Predictor {0} does not contain a score column", i);
                    }
                    ScoreCols[i] = scoreCol.Value.Index;

                    // Get the pipeline.
                    var dv       = new EmptyDataView(Parent.Host, schema.Schema);
                    var tm       = new TransformModelImpl(Parent.Host, dv, dv);
                    var pipeline = Parent.PredictorModels[i].TransformModel.Apply(Parent.Host, tm);
                    BoundPipelines[i] = pipeline.AsRowToRowMapper(Parent.Host);
                    if (BoundPipelines[i] == null)
                    {
                        throw Parent.Host.Except("Transform pipeline {0} contains transforms that do not implement IRowToRowMapper", i);
                    }
                }
            }
Esempio n. 4
0
        public static CommonOutputs.MulticlassClassificationOutput CreateMulticlassPipelineEnsemble(IHostEnvironment env, PipelineClassifierInput input)
        {
            Contracts.CheckValue(env, nameof(env));
            var host = env.Register("CombineModels");

            host.CheckValue(input, nameof(input));
            host.CheckNonEmpty(input.Models, nameof(input.Models));

            IOutputCombiner <VBuffer <Single> > combiner;

            switch (input.ModelCombiner)
            {
            case ClassifierCombiner.Median:
                combiner = new MultiMedian(host, new MultiMedian.Options()
                {
                    Normalize = true
                });
                break;

            case ClassifierCombiner.Average:
                combiner = new MultiAverage(host, new MultiAverage.Options()
                {
                    Normalize = true
                });
                break;

            case ClassifierCombiner.Vote:
                combiner = new MultiVoting(host);
                break;

            default:
                throw host.Except("Unknown combiner kind");
            }
            var ensemble = SchemaBindablePipelineEnsembleBase.Create(host, input.Models, combiner, AnnotationUtils.Const.ScoreColumnKind.MulticlassClassification);

            return(CreatePipelineEnsemble <CommonOutputs.MulticlassClassificationOutput>(host, input.Models, ensemble));
        }
Esempio n. 5
0
        private static TOut CreatePipelineEnsemble <TOut>(IHostEnvironment env, PredictorModel[] predictors, SchemaBindablePipelineEnsembleBase ensemble)
            where TOut : CommonOutputs.TrainerOutput, new()
        {
            var inputSchema = predictors[0].TransformModel.InputSchema;
            var dv          = new EmptyDataView(env, inputSchema);

            // The role mappings are specific to the individual predictors.
            var rmd            = new RoleMappedData(dv);
            var predictorModel = new PredictorModelImpl(env, rmd, dv, ensemble);

            var output = new TOut {
                PredictorModel = predictorModel
            };

            return(output);
        }