00001 00005 #include <assert.h> 00006 #include <cmath> 00007 #include <iostream> 00008 #include "adaboost.h" 00009 00010 REGISTER_CREATOR(lemga::AdaBoost); 00011 00012 namespace lemga { 00013 00014 REAL AdaBoost::train () { 00015 cur_err.resize(n_samples); 00016 const REAL err = Boosting::train(); 00017 cur_err.clear(); 00018 return err; 00019 } 00020 00021 REAL AdaBoost::linear_weight (const DataWgt& sw, const LearnModel& l) { 00022 assert(n_output() == l.n_output()); 00023 00024 REAL err = 0; 00025 for (UINT i = 0; i < n_samples; ++i) { 00026 if ((cur_err[i] = l.c_error(l.get_output(i), ptd->y(i))) > 0.1) 00027 err += sw[i]; 00028 } 00029 #if VERBOSE_OUTPUT 00030 std::cout << "Weighted classification error: " << err*100 << "%\n"; 00031 #endif 00032 00033 if (err >= 0.5) return -1; 00034 00035 REAL beta; 00036 if (err <= 0) 00037 beta = 1000; 00038 else 00039 beta = 1 / err - 1; 00040 return std::log(beta) / 2; 00041 } 00042 00043 REAL AdaBoost::convex_weight (const DataWgt&, const LearnModel&) { 00044 std::cerr << "Please use the gradient descent methods for" 00045 " convex combinations\n"; 00046 OBJ_FUNC_UNDEFINED("convex_weight"); 00047 } 00048 00049 /* We assume classification problem here. The density update rule is 00050 * d <- d * e^(-w y f) 00051 * if y and f are binary, it is equivalent to say 00052 * d <- d * beta for y != f, where beta = e^2w */ 00053 void AdaBoost::linear_smpwgt (DataWgt& sw) { 00054 const REAL beta = std::exp(2 * lm_wgt[n_in_agg-1]); 00055 REAL bw_sum = 0; 00056 for (UINT i = 0; i < n_samples; ++i) { 00057 if (cur_err[i] > 0.1) 00058 sw[i] *= beta; 00059 bw_sum += sw[i]; 00060 } 00061 00062 assert(bw_sum != 0); 00063 for (UINT i = 0; i < n_samples; ++i) 00064 sw[i] /= bw_sum; 00065 } 00066 00067 } // namespace lemga