26 #ifndef MLPACK_METHODS_ADABOOST_ADABOOST_IMPL_HPP 27 #define MLPACK_METHODS_ADABOOST_ADABOOST_IMPL_HPP 43 template<
typename WeakLearnerType,
typename MatType>
46 const arma::Row<size_t>& labels,
47 const size_t numClasses,
48 const WeakLearnerType& other,
49 const size_t iterations,
52 Train(data, labels, numClasses, other, iterations, tol);
56 template<
typename WeakLearnerType,
typename MatType>
65 template<
typename WeakLearnerType,
typename MatType>
68 const arma::Row<size_t>& labels,
69 const size_t numClasses,
70 const WeakLearnerType& other,
71 const size_t iterations,
72 const double tolerance)
78 this->tolerance = tolerance;
79 this->numClasses = numClasses;
83 double rt, crt = 0.0, alphat = 0.0, zt;
85 double ztProduct = 1.0;
88 arma::Row<size_t> predictedLabels(labels.n_cols);
91 MatType tempData(data);
94 arma::mat sumFinalH = arma::zeros<arma::mat>(numClasses,
95 predictedLabels.n_cols);
98 const double initWeight = 1.0 / double(data.n_cols * numClasses);
99 arma::mat D(numClasses, data.n_cols);
103 arma::rowvec weights(predictedLabels.n_cols);
106 arma::Row<size_t> finalH(predictedLabels.n_cols);
109 for (
size_t i = 0; i < iterations; ++i)
120 weights = arma::sum(D);
123 WeakLearnerType w(other, tempData, labels, numClasses, weights);
124 w.Classify(tempData, predictedLabels);
130 for (
size_t j = 0; j < D.n_cols; ++j)
132 if (predictedLabels(j) == labels(j))
133 rt += arma::accu(D.col(j));
135 rt -= arma::accu(D.col(j));
138 if ((i > 0) && (std::abs(rt - crt) < tolerance))
145 alpha.push_back(1.0);
154 alphat = 0.5 * log((1 + rt) / (1 - rt));
156 alpha.push_back(alphat);
160 for (
size_t j = 0; j < D.n_cols; ++j)
162 const double expo = exp(alphat);
163 if (predictedLabels(j) == labels(j))
165 for (
size_t k = 0; k < D.n_rows; ++k)
174 sumFinalH(k, j) += (alphat);
176 sumFinalH(k, j) -= (alphat);
181 for (
size_t k = 0; k < D.n_rows; ++k)
189 sumFinalH(k, j) += alphat;
191 sumFinalH(k, j) -= alphat;
208 template<
typename WeakLearnerType,
typename MatType>
211 arma::Row<size_t>& predictedLabels)
213 arma::Row<size_t> tempPredictedLabels(test.n_cols);
214 arma::mat probabilities;
216 Classify(test, predictedLabels, probabilities);
222 template<
typename WeakLearnerType,
typename MatType>
225 arma::Row<size_t>& predictedLabels,
226 arma::mat& probabilities)
228 arma::Row<size_t> tempPredictedLabels(test.n_cols);
230 probabilities.zeros(numClasses, test.n_cols);
231 predictedLabels.set_size(test.n_cols);
233 for (
size_t i = 0; i < wl.size(); ++i)
235 wl[i].Classify(test, tempPredictedLabels);
237 for (
size_t j = 0; j < tempPredictedLabels.n_cols; ++j)
238 probabilities(tempPredictedLabels(j), j) += alpha[i];
242 arma::uword maxIndex = 0;
244 for (
size_t i = 0; i < predictedLabels.n_cols; ++i)
246 probabilities.col(i) /= arma::accu(probabilities.col(i));
247 pRow = probabilities.unsafe_col(i);
249 predictedLabels(i) = maxIndex;
256 template<
typename WeakLearnerType,
typename MatType>
257 template<
typename Archive>
261 ar(CEREAL_NVP(numClasses));
262 ar(CEREAL_NVP(tolerance));
263 ar(CEREAL_NVP(alpha));
266 if (cereal::is_loading<Archive>())
269 wl.resize(alpha.size());
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The AdaBoost class.
Definition: adaboost.hpp:81
AdaBoost(const MatType &data, const arma::Row< size_t > &labels, const size_t numClasses, const WeakLearnerType &other, const size_t iterations=100, const double tolerance=1e-6)
Constructor.
Definition: adaboost_impl.hpp:44
Definition: hmm_train_main.cpp:300