00001
00002 #ifndef __LEMGA_AGGREGATING_MGNBOOST_H__
00003 #define __LEMGA_AGGREGATING_MGNBOOST_H__
00004
00011 #include "boosting.h"
00012
00013 namespace lemga {
00014
00021 struct MgnCost : public cost::Cost {
00022 const cost::Cost& orig_cost;
00023 REAL lambda;
00024 REAL wsum;
00025
00026 MgnCost (const cost::Cost& c = cost::_cost)
00027 : orig_cost(c), lambda(0), wsum(0) {}
00028 virtual REAL cost (const REAL& F, const REAL& y) const {
00029 return orig_cost.cost(F - lambda*wsum/y, y);
00030 }
00031 virtual REAL deriv1 (const REAL& F, const REAL& y) const {
00032 return orig_cost.deriv1(F - lambda*wsum/y, y);
00033 }
00034 };
00035
00051 class MgnBoost : public Boosting {
00052 friend struct _mgn_gd;
00053 MgnCost shifted_cost;
00054 void update_cost_l () { shifted_cost.lambda = min_margin()/margin_norm(); }
00055 void update_cost_w () { shifted_cost.wsum = model_weight_sum(); }
00056
00057 public:
00058 explicit MgnBoost (bool cvx = false, const cost::Cost& c = cost::_cost)
00059 : Boosting(cvx, shifted_cost), shifted_cost(c)
00060 { use_gradient_descent(); }
00061 explicit MgnBoost (std::istream& is) { is >> *this; }
00062
00063 virtual const id_t& id () const;
00064 virtual MgnBoost* create () const { return new MgnBoost(); }
00065 virtual MgnBoost* clone () const { return new MgnBoost(*this); }
00066
00067 virtual void train ();
00068
00069 protected:
00070 virtual void train_gd ();
00071 };
00072
00073 struct _mgn_gd : public _boost_gd {
00074 MgnBoost* pm;
00075 explicit _mgn_gd (MgnBoost* p) : _boost_gd(p), pm(p) {}
00076
00077 void set_weight (const Boosting::BoostWgt& bw) {
00078 _boost_gd::set_weight(bw);
00079 pm->update_cost_w();
00080 }
00081
00082 Boosting::BoostWgt gradient () const {
00083 pm->update_cost_w(); pm->update_cost_l();
00084 return _boost_gd::gradient();
00085 }
00086 };
00087
00088 }
00089
00090 #ifdef __MGNBOOST_H__
00091 #warning "This header file may conflict with another `mgnboost.h' file."
00092 #endif
00093 #define __MGNBOOST_H__
00094 #endif