SHOGUN
v2.0.0
|
00001 /* 00002 * Copyright (c) 2007-2009 The LIBLINEAR Project. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 00012 * 2. Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 00016 * 3. Neither name of copyright holders nor the names of its contributors 00017 * may be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00024 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 00025 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00027 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 */ 00033 #ifndef DOXYGEN_SHOULD_SKIP_THIS 00034 00035 #ifndef _LIBLINEAR_H 00036 #define _LIBLINEAR_H 00037 00038 #include <shogun/lib/config.h> 00039 00040 #ifdef HAVE_LAPACK 00041 #include <shogun/optimization/liblinear/tron.h> 00042 #include <shogun/features/DotFeatures.h> 00043 #include <vector> 00044 00045 namespace shogun 00046 { 00047 00048 #ifdef __cplusplus 00049 extern "C" { 00050 #endif 00051 00053 struct problem 00054 { 00056 int32_t l; 00058 int32_t n; 00060 float64_t* y; 00062 CDotFeatures* x; 00064 bool use_bias; 00065 }; 00066 00068 struct parameter 00069 { 00071 int32_t solver_type; 00072 00073 /* these are for training only */ 00075 float64_t eps; 00077 float64_t C; 00079 int32_t nr_weight; 00081 int32_t *weight_label; 00083 float64_t* weight; 00084 }; 00085 00087 struct model 00088 { 00090 struct parameter param; 00092 int32_t nr_class; 00094 int32_t nr_feature; 00096 float64_t *w; 00098 int32_t *label; 00100 float64_t bias; 00101 }; 00102 00103 void destroy_model(struct model *model_); 00104 void destroy_param(struct parameter *param); 00105 #ifdef __cplusplus 00106 } 00107 #endif 00108 00110 class l2loss_svm_fun : public function 00111 { 00112 public: 00119 l2loss_svm_fun(const problem *prob, float64_t Cp, float64_t Cn); 00120 ~l2loss_svm_fun(); 00121 00127 float64_t fun(float64_t *w); 00128 00134 void grad(float64_t *w, float64_t *g); 00135 00141 void Hv(float64_t *s, float64_t *Hs); 00142 00147 int32_t get_nr_variable(); 00148 00149 private: 00150 void Xv(float64_t *v, float64_t *Xv); 00151 void subXv(float64_t *v, float64_t *Xv); 00152 void subXTv(float64_t *v, float64_t *XTv); 00153 00154 float64_t *C; 00155 float64_t *z; 00156 float64_t *D; 00157 int32_t *I; 00158 int32_t sizeI; 00159 const problem *prob; 00160 }; 00161 00163 class l2r_lr_fun : public function 00164 { 00165 public: 00172 l2r_lr_fun(const problem *prob, float64_t* C); 00173 ~l2r_lr_fun(); 00174 00180 float64_t fun(float64_t *w); 00181 00187 void grad(float64_t *w, float64_t *g); 00188 00194 void Hv(float64_t *s, float64_t *Hs); 00195 00196 int32_t get_nr_variable(); 00197 00198 private: 00199 void Xv(float64_t *v, float64_t *Xv); 00200 void XTv(float64_t *v, float64_t *XTv); 00201 00202 float64_t *C; 00203 float64_t *z; 00204 float64_t *D; 00205 const problem *m_prob; 00206 }; 00207 00208 class l2r_l2_svc_fun : public function 00209 { 00210 public: 00211 l2r_l2_svc_fun(const problem *prob, float64_t* Cs); 00212 ~l2r_l2_svc_fun(); 00213 00214 double fun(double *w); 00215 void grad(double *w, double *g); 00216 void Hv(double *s, double *Hs); 00217 00218 int get_nr_variable(); 00219 00220 protected: 00221 void Xv(double *v, double *Xv); 00222 void subXv(double *v, double *Xv); 00223 void subXTv(double *v, double *XTv); 00224 00225 double *C; 00226 double *z; 00227 double *D; 00228 int *I; 00229 int sizeI; 00230 const problem *m_prob; 00231 }; 00232 00233 class l2r_l2_svr_fun: public l2r_l2_svc_fun 00234 { 00235 public: 00236 l2r_l2_svr_fun(const problem *prob, double *Cs, double p); 00237 00238 double fun(double *w); 00239 void grad(double *w, double *g); 00240 00241 private: 00242 double m_p; 00243 }; 00244 00245 00246 struct mcsvm_state 00247 { 00248 double* w; 00249 double* B; 00250 double* G; 00251 double* alpha; 00252 double* alpha_new; 00253 int* index; 00254 double* QD; 00255 int* d_ind; 00256 double* d_val; 00257 int* alpha_index; 00258 int* y_index; 00259 int* active_size_i; 00260 bool allocated,inited; 00261 00262 mcsvm_state() 00263 { 00264 w = NULL; 00265 B = NULL; 00266 G = NULL; 00267 alpha = NULL; 00268 alpha_new = NULL; 00269 index = NULL; 00270 QD = NULL; 00271 d_ind = NULL; 00272 d_val = NULL; 00273 alpha_index = NULL; 00274 y_index = NULL; 00275 active_size_i = NULL; 00276 allocated = false; 00277 inited = false; 00278 } 00279 00280 ~mcsvm_state() 00281 { 00282 SG_FREE(w); 00283 SG_FREE(B); 00284 SG_FREE(G); 00285 SG_FREE(alpha); 00286 SG_FREE(alpha_new); 00287 SG_FREE(index); 00288 SG_FREE(QD); 00289 SG_FREE(d_ind); 00290 SG_FREE(d_val); 00291 SG_FREE(alpha_index); 00292 SG_FREE(y_index); 00293 SG_FREE(active_size_i); 00294 } 00295 }; 00296 00297 class Solver_MCSVM_CS 00298 { 00299 public: 00300 Solver_MCSVM_CS(const problem *prob, int nr_class, double *C, 00301 double *w0, double eps, int max_iter, 00302 double train_time, mcsvm_state* given_state); 00303 ~Solver_MCSVM_CS(); 00304 void solve(); 00305 private: 00306 void solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new); 00307 bool be_shrunk(int i, int m, int yi, double alpha_i, double minG); 00308 double *C; 00309 int w_size, l; 00310 int nr_class; 00311 int max_iter; 00312 double eps; 00313 double max_train_time; 00314 double* w0; 00315 const problem *prob; 00316 mcsvm_state* state; 00317 }; 00318 00319 00320 } 00321 #endif //HAVE_LAPACK 00322 #endif //_LIBLINEAR_H 00323 00324 #endif // DOXYGEN_SHOULD_SKIP_THIS