00001 00005 #include <assert.h> 00006 #include <cmath> 00007 #include "bagging.h" 00008 00009 REGISTER_CREATOR(lemga::Bagging); 00010 00011 namespace lemga { 00012 00013 Output Bagging::operator() (const Input& x) const { 00014 assert(n_in_agg <= size() && _n_out > 0); 00015 00016 Output y(_n_out, 0); 00017 for (UINT i = 0; i < n_in_agg; ++i) { 00018 assert(lm[i] != 0 && exact_dimensions(*lm[i])); 00019 Output yi = (*lm[i])(x); 00020 for (UINT j = 0; j < _n_out; ++j) 00021 y[j] += (yi[j] > 0)? 1 : -1; 00022 } 00023 00024 return y; 00025 } 00026 00027 void Bagging::train () { 00028 assert(ptd != 0 && ptw != 0); 00029 set_dimensions(*ptd); 00030 00031 for (n_in_agg = size(); n_in_agg < max_n_model; ++n_in_agg) { 00032 #if VERBOSE_OUTPUT 00033 std::cout << "=== " << id() << " #" << n_in_agg+1 << " / " 00034 << max_n_model << " ===\n"; 00035 #endif 00036 assert(lm_base != 0); 00037 LearnModel *p = lm_base->clone(); 00038 p->set_train_data(ptd->random_sample(*ptw, n_samples)); 00039 p->train(); 00040 p->set_train_data(ptd, ptw); // get rid of the random sample 00041 set_dimensions(*p); 00042 lm.push_back(p); 00043 } 00044 } 00045 00046 REAL Bagging::margin_norm () const { 00047 return n_in_agg; 00048 } 00049 00050 REAL Bagging::margin_of (const Input& x, const Output& y) const { 00051 assert(std::fabs(y[0]*y[0]-1) < INFINITESIMAL); 00052 return (*this)(x)[0] * y[0]; 00053 } 00054 00055 } // namespace lemga