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 REAL train ();
00068 };
00069
00070 struct _mgn_gd : public _boost_gd {
00071 MgnBoost* pm;
00072 explicit _mgn_gd (MgnBoost* p) : _boost_gd(p), pm(p) {}
00073
00074 void set_weight (const Boosting::BoostWgt& bw) {
00075 _boost_gd::set_weight(bw);
00076 pm->update_cost_w();
00077 }
00078
00079 Boosting::BoostWgt gradient () const {
00080 pm->update_cost_w(); pm->update_cost_l();
00081 return _boost_gd::gradient();
00082 }
00083 };
00084
00085 }
00086
00087 #ifdef __MGNBOOST_H__
00088 #warning "This header file may conflict with another `mgnboost.h' file."
00089 #endif
00090 #define __MGNBOOST_H__
00091 #endif