private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner)
      : base(original, cloner) {
      if (original.actualModel != null) this.actualModel = cloner.Clone(original.actualModel);

      this.trainingProblemData = cloner.Clone(original.trainingProblemData);
      this.lossFunction = cloner.Clone(original.lossFunction);
      this.seed = original.seed;
      this.iterations = original.iterations;
      this.maxSize = original.maxSize;
      this.r = original.r;
      this.m = original.m;
      this.nu = original.nu;
    }
        private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner)
            : base(original, cloner)
        {
            if (original.actualModel != null)
            {
                this.actualModel = cloner.Clone(original.actualModel);
            }

            this.trainingProblemData = cloner.Clone(original.trainingProblemData);
            this.lossFunction        = cloner.Clone(original.lossFunction);
            this.seed       = original.seed;
            this.iterations = original.iterations;
            this.maxSize    = original.maxSize;
            this.r          = original.r;
            this.m          = original.m;
            this.nu         = original.nu;
        }
        private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner)
            : base(original, cloner)
        {
            IGradientBoostedTreesModel clonedModel = null;

            if (original.actualModel.IsValueCreated)
            {
                clonedModel = cloner.Clone(original.ActualModel);
            }
            actualModel = new Lazy <IGradientBoostedTreesModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure

            this.trainingProblemData = cloner.Clone(original.trainingProblemData);
            this.lossFunction        = cloner.Clone(original.lossFunction);
            this.seed       = original.seed;
            this.iterations = original.iterations;
            this.maxSize    = original.maxSize;
            this.r          = original.r;
            this.m          = original.m;
            this.nu         = original.nu;
        }
示例#4
0
        protected override void Run(CancellationToken cancellationToken)
        {
            // Set up the algorithm
            if (SetSeedRandomly)
            {
                Seed = Random.RandomSeedGenerator.GetSeed();
            }

            // Set up the results display
            var iterations = new IntValue(0);

            Results.Add(new Result("Iterations", iterations));

            var table = new DataTable("Qualities");

            table.Rows.Add(new DataRow("Loss (train)"));
            table.Rows.Add(new DataRow("Loss (test)"));
            table.Rows["Loss (train)"].VisualProperties.StartIndexZero = true;
            table.Rows["Loss (test)"].VisualProperties.StartIndexZero  = true;

            Results.Add(new Result("Qualities", table));
            var curLoss = new DoubleValue();

            Results.Add(new Result("Loss (train)", curLoss));

            // init
            var problemData  = (IRegressionProblemData)Problem.ProblemData.Clone();
            var lossFunction = LossFunctionParameter.Value;
            var state        = GradientBoostedTreesAlgorithmStatic.CreateGbmState(problemData, lossFunction, (uint)Seed, MaxSize, R, M, Nu);

            var updateInterval = UpdateIntervalParameter.Value.Value;

            // Loop until iteration limit reached or canceled.
            for (int i = 0; i < Iterations; i++)
            {
                cancellationToken.ThrowIfCancellationRequested();

                GradientBoostedTreesAlgorithmStatic.MakeStep(state);

                // iteration results
                if (i % updateInterval == 0)
                {
                    curLoss.Value = state.GetTrainLoss();
                    table.Rows["Loss (train)"].Values.Add(curLoss.Value);
                    table.Rows["Loss (test)"].Values.Add(state.GetTestLoss());
                    iterations.Value = i;
                }
            }

            // final results
            iterations.Value = Iterations;
            curLoss.Value    = state.GetTrainLoss();
            table.Rows["Loss (train)"].Values.Add(curLoss.Value);
            table.Rows["Loss (test)"].Values.Add(state.GetTestLoss());

            // produce variable relevance
            var orderedImpacts = state.GetVariableRelevance().Select(t => new { name = t.Key, impact = t.Value }).ToList();

            var impacts = new DoubleMatrix();
            var matrix  = impacts as IStringConvertibleMatrix;

            matrix.Rows        = orderedImpacts.Count;
            matrix.RowNames    = orderedImpacts.Select(x => x.name);
            matrix.Columns     = 1;
            matrix.ColumnNames = new string[] { "Relative variable relevance" };

            int rowIdx = 0;

            foreach (var p in orderedImpacts)
            {
                matrix.SetValue(string.Format("{0:N2}", p.impact), rowIdx++, 0);
            }

            Results.Add(new Result("Variable relevance", impacts));
            Results.Add(new Result("Loss (test)", new DoubleValue(state.GetTestLoss())));

            // produce solution
            if (ModelCreation == ModelCreation.SurrogateModel || ModelCreation == ModelCreation.Model)
            {
                IRegressionModel model = state.GetModel();

                if (ModelCreation == ModelCreation.SurrogateModel)
                {
                    model = new GradientBoostedTreesModelSurrogate((GradientBoostedTreesModel)model, problemData, (uint)Seed, lossFunction, Iterations, MaxSize, R, M, Nu);
                }

                // for logistic regression we produce a classification solution
                if (lossFunction is LogisticRegressionLoss)
                {
                    var classificationModel = new DiscriminantFunctionClassificationModel(model,
                                                                                          new AccuracyMaximizationThresholdCalculator());
                    var classificationProblemData = new ClassificationProblemData(problemData.Dataset,
                                                                                  problemData.AllowedInputVariables, problemData.TargetVariable, problemData.Transformations);
                    classificationProblemData.TrainingPartition.Start = Problem.ProblemData.TrainingPartition.Start;
                    classificationProblemData.TrainingPartition.End   = Problem.ProblemData.TrainingPartition.End;
                    classificationProblemData.TestPartition.Start     = Problem.ProblemData.TestPartition.Start;
                    classificationProblemData.TestPartition.End       = Problem.ProblemData.TestPartition.End;

                    classificationModel.SetThresholdsAndClassValues(new double[] { double.NegativeInfinity, 0.0 }, new[] { 0.0, 1.0 });


                    var classificationSolution = new DiscriminantFunctionClassificationSolution(classificationModel, classificationProblemData);
                    Results.Add(new Result("Solution", classificationSolution));
                }
                else
                {
                    // otherwise we produce a regression solution
                    Results.Add(new Result("Solution", new GradientBoostedTreesSolution(model, problemData)));
                }
            }
            else if (ModelCreation == ModelCreation.QualityOnly)
            {
                //Do nothing
            }
            else
            {
                throw new NotImplementedException("Selected parameter for CreateSolution isn't implemented yet");
            }
        }