LibLinear.h

Go to the documentation of this file.
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 "lib/config.h"
00016 
00017 #include "lib/common.h"
00018 #include "base/Parameter.h"
00019 #include "classifier/LinearClassifier.h"
00020 #include "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 CLinearClassifier
00048 {
00049     public:
00051         CLibLinear(void);
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 
00090         virtual bool train(CFeatures* data=NULL);
00091 
00096         virtual inline EClassifierType get_classifier_type() { return CT_LIBLINEAR; }
00097 
00103         inline void set_C(float64_t c_neg, float64_t c_pos) { C1=c_neg; C2=c_pos; }
00104 
00109         inline float64_t get_C1() { return C1; }
00110 
00115         inline float64_t get_C2() { return C2; }
00116 
00121         inline void set_epsilon(float64_t eps) { epsilon=eps; }
00122 
00127         inline float64_t get_epsilon() { return epsilon; }
00128 
00133         inline void set_bias_enabled(bool enable_bias) { use_bias=enable_bias; }
00134 
00139         inline bool get_bias_enabled() { return use_bias; }
00140 
00142         inline virtual const char* get_name() const { return "LibLinear"; }
00143 
00145         inline int32_t get_max_iterations()
00146         {
00147             return max_iterations;
00148         }
00149 
00151         inline void set_max_iterations(int32_t max_iter=1000)
00152         {
00153             max_iterations=max_iter;
00154         }
00155 
00157         inline void set_linear_term(float64_t* linear_term, int32_t len)
00158         {
00159             if (!labels)
00160                 SG_ERROR("Please assign labels first!\n");
00161 
00162             int32_t num_labels=labels->get_num_labels();
00163 
00164             if (num_labels!=len)
00165             {
00166                 SG_ERROR("Number of labels (%d) does not match number"
00167                         "of entries (%d) in linear term \n", num_labels, len);
00168             }
00169 
00170             m_linear_term = CMath::clone_vector(linear_term, len);
00171             m_linear_term_len = len;
00172         }
00173 
00175         void get_linear_term(float64_t** linear_term, int32_t* len);
00176 
00178         void init_linear_term();
00179 
00180     private:
00182         void init();
00183 
00184         void train_one(const problem *prob, const parameter *param, double Cp, double Cn);
00185         void solve_l2r_l1l2_svc(
00186             const problem *prob, double eps, double Cp, double Cn, LIBLINEAR_SOLVER_TYPE st);
00187 
00188         void solve_l1r_l2_svc(problem *prob_col, double eps, double Cp, double Cn);
00189         void solve_l1r_lr(const problem *prob_col, double eps, double Cp, double Cn);
00190 
00191 
00192     protected:
00194         float64_t C1;
00196         float64_t C2;
00198         bool use_bias;
00200         float64_t epsilon;
00202         int32_t max_iterations;
00203 
00205         float64_t* m_linear_term;
00207         int32_t m_linear_term_len;
00208 
00210         LIBLINEAR_SOLVER_TYPE liblinear_solver_type;
00211 };
00212 
00213 #else /* HAVE_LAPACK  */
00214 
00216 #define IGNORE_IN_CLASSLIST
00217 IGNORE_IN_CLASSLIST class CLibLinear : public CLinearClassifier
00218 {
00219 public:
00221     CLibLinear(void) {}
00222 
00224     inline virtual const char* get_name() const { return "LibLinear"; }
00225 };
00226 
00227 #endif //HAVE_LAPACK
00228 
00229 } /* namespace shogun  */
00230 
00231 #endif //_LIBLINEAR_H___
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation