SHOGUN
v2.0.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2007-2010 Soeren Sonnenburg 00008 * Copyright (c) 2007-2009 The LIBLINEAR Project. 00009 * Copyright (C) 2007-2010 Fraunhofer Institute FIRST and Max-Planck-Society 00010 */ 00011 00012 #ifndef _LIBLINEAR_H___ 00013 #define _LIBLINEAR_H___ 00014 00015 #include <shogun/lib/config.h> 00016 00017 #include <shogun/lib/common.h> 00018 #include <shogun/base/Parameter.h> 00019 #include <shogun/machine/LinearMachine.h> 00020 #include <shogun/optimization/liblinear/shogun_liblinear.h> 00021 00022 namespace shogun 00023 { 00025 enum LIBLINEAR_SOLVER_TYPE 00026 { 00028 L2R_LR, 00030 L2R_L2LOSS_SVC_DUAL, 00032 L2R_L2LOSS_SVC, 00034 // (default since this is the standard SVM) 00035 L2R_L1LOSS_SVC_DUAL, 00037 L1R_L2LOSS_SVC, 00039 L1R_LR, 00041 L2R_LR_DUAL 00042 }; 00043 00044 #ifdef HAVE_LAPACK 00045 00047 class CLibLinear : public CLinearMachine 00048 { 00049 public: 00050 MACHINE_PROBLEM_TYPE(PT_BINARY); 00051 00053 CLibLinear(); 00054 00059 CLibLinear(LIBLINEAR_SOLVER_TYPE liblinear_solver_type); 00060 00067 CLibLinear( 00068 float64_t C, CDotFeatures* traindat, 00069 CLabels* trainlab); 00070 00072 virtual ~CLibLinear(); 00073 00074 inline LIBLINEAR_SOLVER_TYPE get_liblinear_solver_type() 00075 { 00076 return liblinear_solver_type; 00077 } 00078 00079 inline void set_liblinear_solver_type(LIBLINEAR_SOLVER_TYPE st) 00080 { 00081 liblinear_solver_type=st; 00082 } 00083 00088 virtual inline EMachineType get_classifier_type() { return CT_LIBLINEAR; } 00089 00095 inline void set_C(float64_t c_neg, float64_t c_pos) { C1=c_neg; C2=c_pos; } 00096 00101 inline float64_t get_C1() { return C1; } 00102 00107 inline float64_t get_C2() { return C2; } 00108 00113 inline void set_epsilon(float64_t eps) { epsilon=eps; } 00114 00119 inline float64_t get_epsilon() { return epsilon; } 00120 00125 inline void set_bias_enabled(bool enable_bias) { use_bias=enable_bias; } 00126 00131 inline bool get_bias_enabled() { return use_bias; } 00132 00134 inline virtual const char* get_name() const { return "LibLinear"; } 00135 00137 inline int32_t get_max_iterations() 00138 { 00139 return max_iterations; 00140 } 00141 00143 inline void set_max_iterations(int32_t max_iter=1000) 00144 { 00145 max_iterations=max_iter; 00146 } 00147 00149 void set_linear_term(const SGVector<float64_t> linear_term); 00150 00152 SGVector<float64_t> get_linear_term(); 00153 00155 void init_linear_term(); 00156 00157 protected: 00166 virtual bool train_machine(CFeatures* data=NULL); 00167 00168 private: 00170 void init(); 00171 00172 void train_one(const problem *prob, const parameter *param, double Cp, double Cn); 00173 void solve_l2r_l1l2_svc( 00174 const problem *prob, double eps, double Cp, double Cn, LIBLINEAR_SOLVER_TYPE st); 00175 00176 void solve_l1r_l2_svc(problem *prob_col, double eps, double Cp, double Cn); 00177 void solve_l1r_lr(const problem *prob_col, double eps, double Cp, double Cn); 00178 void solve_l2r_lr_dual(const problem *prob, double eps, double Cp, double Cn); 00179 00180 00181 protected: 00183 float64_t C1; 00185 float64_t C2; 00187 bool use_bias; 00189 float64_t epsilon; 00191 int32_t max_iterations; 00192 00194 SGVector<float64_t> m_linear_term; 00195 00197 LIBLINEAR_SOLVER_TYPE liblinear_solver_type; 00198 }; 00199 00200 #endif //HAVE_LAPACK 00201 00202 } /* namespace shogun */ 00203 00204 #endif //_LIBLINEAR_H___