mlpack  master
adaboost.hpp
Go to the documentation of this file.
1 
28 #ifndef MLPACK_METHODS_ADABOOST_ADABOOST_HPP
29 #define MLPACK_METHODS_ADABOOST_ADABOOST_HPP
30 
31 #include <mlpack/prereqs.hpp>
34 
35 namespace mlpack {
36 namespace adaboost {
37 
79 template<typename WeakLearnerType = mlpack::perceptron::Perceptron<>,
80  typename MatType = arma::mat>
81 class AdaBoost
82 {
83  public:
96  AdaBoost(const MatType& data,
97  const arma::Row<size_t>& labels,
98  const WeakLearnerType& other,
99  const size_t iterations = 100,
100  const double tolerance = 1e-6);
101 
106  AdaBoost(const double tolerance = 1e-6);
107 
108  // Return the value of ztProduct.
109  double ZtProduct() { return ztProduct; }
110 
112  double Tolerance() const { return tolerance; }
114  double& Tolerance() { return tolerance; }
115 
117  size_t Classes() const { return classes; }
118 
120  size_t WeakLearners() const { return alpha.size(); }
121 
123  double Alpha(const size_t i) const { return alpha[i]; }
125  double& Alpha(const size_t i) { return alpha[i]; }
126 
128  const WeakLearnerType& WeakLearner(const size_t i) const { return wl[i]; }
130  WeakLearnerType& WeakLearner(const size_t i) { return wl[i]; }
131 
143  void Train(const MatType& data,
144  const arma::Row<size_t>& labels,
145  const WeakLearnerType& learner,
146  const size_t iterations = 100,
147  const double tolerance = 1e-6);
148 
156  void Classify(const MatType& test, arma::Row<size_t>& predictedLabels);
157 
161  template<typename Archive>
162  void Serialize(Archive& ar, const unsigned int /* version */);
163 
164 private:
166  size_t classes;
167  // The tolerance for change in rt and when to stop.
168  double tolerance;
169 
171  std::vector<WeakLearnerType> wl;
173  std::vector<double> alpha;
174 
176  double ztProduct;
177 
178 }; // class AdaBoost
179 
180 } // namespace adaboost
181 } // namespace mlpack
182 
183 #include "adaboost_impl.hpp"
184 
185 #endif
size_t WeakLearners() const
Get the number of weak learners in the model.
Definition: adaboost.hpp:120
std::vector< WeakLearnerType > wl
The vector of weak learners.
Definition: adaboost.hpp:171
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
WeakLearnerType & WeakLearner(const size_t i)
Modify the given weak learner (be careful!).
Definition: adaboost.hpp:130
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Alpha(const size_t i) const
Get the weights for the given weak learner.
Definition: adaboost.hpp:123
The AdaBoost class.
Definition: adaboost.hpp:81
double & Alpha(const size_t i)
Modify the weight for the given weak learner (be careful!).
Definition: adaboost.hpp:125
void Train(const MatType &data, const arma::Row< size_t > &labels, const WeakLearnerType &learner, const size_t iterations=100, const double tolerance=1e-6)
Train AdaBoost on the given dataset.
void Classify(const MatType &test, arma::Row< size_t > &predictedLabels)
Classify the given test points.
const WeakLearnerType & WeakLearner(const size_t i) const
Get the given weak learner.
Definition: adaboost.hpp:128
size_t classes
The number of classes in the model.
Definition: adaboost.hpp:166
size_t Classes() const
Get the number of classes this model is trained on.
Definition: adaboost.hpp:117
double Tolerance() const
Get the tolerance for stopping the optimization during training.
Definition: adaboost.hpp:112
double & Tolerance()
Modify the tolerance for stopping the optimization during training.
Definition: adaboost.hpp:114
std::vector< double > alpha
The weights corresponding to each weak learner.
Definition: adaboost.hpp:173
double ztProduct
To check for the bound for the Hamming loss.
Definition: adaboost.hpp:176
AdaBoost(const MatType &data, const arma::Row< size_t > &labels, const WeakLearnerType &other, const size_t iterations=100, const double tolerance=1e-6)
Constructor.
void Serialize(Archive &ar, const unsigned int)
Serialize the AdaBoost model.