SHOGUN
v1.1.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/classifier/svm/SVM_linear.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 MCSVM_CS, 00039 L1R_L2LOSS_SVC, 00041 L1R_LR 00042 }; 00043 00044 #ifdef HAVE_LAPACK 00045 00047 class CLibLinear : public CLinearMachine 00048 { 00049 public: 00051 CLibLinear(); 00052 00057 CLibLinear(LIBLINEAR_SOLVER_TYPE liblinear_solver_type); 00058 00065 CLibLinear( 00066 float64_t C, CDotFeatures* traindat, 00067 CLabels* trainlab); 00068 00070 virtual ~CLibLinear(); 00071 00072 inline LIBLINEAR_SOLVER_TYPE get_liblinear_solver_type() 00073 { 00074 return liblinear_solver_type; 00075 } 00076 00077 inline void set_liblinear_solver_type(LIBLINEAR_SOLVER_TYPE st) 00078 { 00079 liblinear_solver_type=st; 00080 } 00081 00086 virtual inline EClassifierType get_classifier_type() { return CT_LIBLINEAR; } 00087 00093 inline void set_C(float64_t c_neg, float64_t c_pos) { C1=c_neg; C2=c_pos; } 00094 00099 inline float64_t get_C1() { return C1; } 00100 00105 inline float64_t get_C2() { return C2; } 00106 00111 inline void set_epsilon(float64_t eps) { epsilon=eps; } 00112 00117 inline float64_t get_epsilon() { return epsilon; } 00118 00123 inline void set_bias_enabled(bool enable_bias) { use_bias=enable_bias; } 00124 00129 inline bool get_bias_enabled() { return use_bias; } 00130 00132 inline virtual const char* get_name() const { return "LibLinear"; } 00133 00135 inline int32_t get_max_iterations() 00136 { 00137 return max_iterations; 00138 } 00139 00141 inline void set_max_iterations(int32_t max_iter=1000) 00142 { 00143 max_iterations=max_iter; 00144 } 00145 00147 inline void set_linear_term(SGVector<float64_t> linear_term) 00148 { 00149 if (!labels) 00150 SG_ERROR("Please assign labels first!\n"); 00151 00152 int32_t num_labels=labels->get_num_labels(); 00153 00154 if (num_labels!=linear_term.vlen) 00155 { 00156 SG_ERROR("Number of labels (%d) does not match number" 00157 " of entries (%d) in linear term \n", num_labels, 00158 linear_term.vlen); 00159 } 00160 00161 m_linear_term.destroy_vector(); 00162 m_linear_term.vector=CMath::clone_vector(linear_term.vector, 00163 linear_term.vlen); 00164 m_linear_term.vlen=linear_term.vlen; 00165 } 00166 00168 SGVector<float64_t> get_linear_term(); 00169 00171 void init_linear_term(); 00172 00173 protected: 00182 virtual bool train_machine(CFeatures* data=NULL); 00183 00184 private: 00186 void init(); 00187 00188 void train_one(const problem *prob, const parameter *param, double Cp, double Cn); 00189 void solve_l2r_l1l2_svc( 00190 const problem *prob, double eps, double Cp, double Cn, LIBLINEAR_SOLVER_TYPE st); 00191 00192 void solve_l1r_l2_svc(problem *prob_col, double eps, double Cp, double Cn); 00193 void solve_l1r_lr(const problem *prob_col, double eps, double Cp, double Cn); 00194 00195 00196 protected: 00198 float64_t C1; 00200 float64_t C2; 00202 bool use_bias; 00204 float64_t epsilon; 00206 int32_t max_iterations; 00207 00209 SGVector<float64_t> m_linear_term; 00210 00212 LIBLINEAR_SOLVER_TYPE liblinear_solver_type; 00213 }; 00214 00215 #endif //HAVE_LAPACK 00216 00217 } /* namespace shogun */ 00218 00219 #endif //_LIBLINEAR_H___