示例#1
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="AdaBoost&lt;TModel&gt;"/> class.
 /// </summary>
 ///
 /// <param name="model">The model to be learned.</param>
 /// <param name="creationFunction">The model fitting function.</param>
 ///
 public AdaBoost(Boost <TModel> model, ModelConstructor <TModel> creationFunction)
 {
     this.classifier  = model;
     this.Creation    = creationFunction;
     this.convergence = new RelativeConvergence();
 }
示例#2
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="AdaBoost&lt;TModel&gt;"/> class.
 /// </summary>
 ///
 /// <param name="model">The model to be learned.</param>
 ///
 public AdaBoost(Boost <TModel> model)
 {
     this.classifier  = model;
     this.convergence = new RelativeConvergence();
 }
示例#3
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="AdaBoost&lt;TModel&gt;"/> class.
 /// </summary>
 ///
 /// <param name="model">The model to be learned.</param>
 ///
 public AdaBoost(Boost <TModel> model)
 {
     this.Model = model;
 }
示例#4
0
        /// <summary>
        ///   Learns a model that can map the given inputs to the given outputs.
        /// </summary>
        ///
        /// <param name="x">The model inputs.</param>
        /// <param name="y">The desired outputs associated with each <paramref name="x">inputs</paramref>.</param>
        /// <param name="weights">The weight of importance for each input-output pair (if supported by the learning algorithm).</param>
        ///
        /// <returns>A model that has learned how to produce <paramref name="y"/> given <paramref name="x"/>.</returns>
        ///
        public Boost <TModel> Learn(double[][] x, int[] y, double[] weights = null)
        {
            if (weights == null)
            {
                weights = Vector.Create(x.Length, 1.0 / x.Length);
            }

            if (Model == null)
            {
                Model = new Boost <TModel>();
            }

            double error     = 0;
            double weightSum = 0;

            var    predicted  = new int[y.Length];
            var    parameters = new AdaBoostParameters();
            TModel model;

            do
            {
                // Create and train a classifier
                var learner = Learner(parameters);
                model = learner.Learn(x, y, weights);

                if (model == null)
                {
                    break;
                }

                // Determine its current accuracy
                model.Decide(x, result: predicted);

                error = 0;
                for (int i = 0; i < predicted.Length; i++)
                {
                    if (predicted[i] != y[i])
                    {
                        error += weights[i];
                    }
                }

                if (error >= threshold)
                {
                    break;
                }


                // AdaBoost
                double w = 0.5 * System.Math.Log((1.0 - error) / error);

                double sum = 0;
                for (int i = 0; i < weights.Length; i++)
                {
                    double d;
                    if (y[i] == predicted[i])
                    {
                        d = Math.Exp(-w);
                    }
                    else
                    {
                        d = Math.Exp(+w);
                    }

                    weights[i] *= d;
                    sum        += weights[i];
                }

                // Update sample weights
                for (int i = 0; i < weights.Length; i++)
                {
                    weights[i] /= sum;
                }

                Model.Add(w, model);

                weightSum += w;

                convergence.NewValue = error;
            } while (!convergence.HasConverged);


            // Normalize weights for confidence calculation
            for (int i = 0; i < Model.Models.Count; i++)
            {
                Model.Models[i].Weight /= weightSum;
            }

            return(Model);
        }
示例#5
0
 public AdaBoost(Boost <TModel> model, ModelConstructor <TModel> creationFunction)
 {
     this.Model    = model;
     this.Creation = creationFunction;
 }