SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVMLight.h
Go to the documentation of this file.
1 /***********************************************************************/
2 /* */
3 /* SVMLight.h */
4 /* */
5 /* Author: Thorsten Joachims */
6 /* Date: 19.07.99 */
7 /* */
8 /* Copyright (c) 1999 Universitaet Dortmund - All rights reserved */
9 /* */
10 /* This software is available for non-commercial use only. It must */
11 /* not be modified and distributed without prior permission of the */
12 /* author. The author is not responsible for implications from the */
13 /* use of this software. */
14 /* */
15 /* THIS INCLUDES THE FOLLOWING ADDITIONS */
16 /* Generic Kernel Interfacing: Soeren Sonnenburg */
17 /* Parallizations: Soeren Sonnenburg */
18 /* Multiple Kernel Learning: Gunnar Raetsch, Soeren Sonnenburg */
19 /* Linadd Speedup: Gunnar Raetsch, Soeren Sonnenburg */
20 /* */
21 /***********************************************************************/
22 #ifndef _SVMLight_H___
23 #define _SVMLight_H___
24 
25 #include <shogun/lib/config.h>
26 
27 #ifdef USE_SVMLIGHT
29 #include <shogun/lib/common.h>
30 
31 namespace shogun
32 {
33 
34 class CKernel;
35 //# define VERSION "V3.50 -- correct??"
36 //# define VERSION_DATE "01.11.00 -- correct??"
37 
38 # define DEF_PRECISION 1E-14
39 # define MAXSHRINK 50000
40 
41 #ifndef DOXYGEN_SHOULD_SKIP_THIS
42 
43 struct MODEL {
45 int32_t sv_num;
47 int32_t at_upper_bound;
49 float64_t b;
51 int32_t* supvec;
53 float64_t *alpha;
55 int32_t *index;
57 int32_t totdoc;
59 CKernel* kernel;
60 
61 /* the following values are not written to file */
63 float64_t loo_error;
65 float64_t loo_recall;
67 float64_t loo_precision;
68 
70 float64_t xa_error;
72 float64_t xa_recall;
74 float64_t xa_precision;
75 };
76 
78 typedef struct quadratic_program {
80  int32_t opt_n;
82  int32_t opt_m;
84  float64_t *opt_ce;
86  float64_t *opt_ce0;
88  float64_t *opt_g;
90  float64_t *opt_g0;
92  float64_t *opt_xinit;
94  float64_t *opt_low;
96  float64_t *opt_up;
97 } QP;
98 
100 typedef int32_t FNUM;
101 
103 typedef float64_t FVAL;
104 
106 struct LEARN_PARM {
108  int32_t type;
110  float64_t svm_c;
112  float64_t* eps;
114  float64_t svm_costratio;
116  float64_t transduction_posratio;
117  /* classified as positives */
119  int32_t biased_hyperplane;
124  int32_t sharedslack;
126  int32_t svm_maxqpsize;
128  int32_t svm_newvarsinqp;
130  int32_t kernel_cache_size;
132  float64_t epsilon_crit;
134  float64_t epsilon_shrink;
136  int32_t svm_iter_to_shrink;
140  int32_t maxiter;
142  int32_t remove_inconsistent;
146  int32_t skip_final_opt_check;
148  int32_t compute_loo;
152  float64_t rho;
156  int32_t xa_depth;
158  char predfile[200];
162  char alphafile[200];
163 
164  /* you probably do not want to touch the following */
166  float64_t epsilon_const;
168  float64_t epsilon_a;
170  float64_t opt_precision;
171 
172  /* the following are only for internal use */
174  int32_t svm_c_steps;
176  float64_t svm_c_factor;
178  float64_t svm_costratio_unlab;
180  float64_t svm_unlabbound;
182  float64_t *svm_cost;
183 };
184 
186 struct TIMING {
188  int32_t time_kernel;
190  int32_t time_opti;
192  int32_t time_shrink;
194  int32_t time_update;
196  int32_t time_model;
198  int32_t time_check;
200  int32_t time_select;
201 };
202 
203 
205 struct SHRINK_STATE
206 {
208  int32_t *active;
210  int32_t *inactive_since;
212  int32_t deactnum;
214  float64_t **a_history;
216  int32_t maxhistory;
218  float64_t *last_a;
220  float64_t *last_lin;
221 };
222 #endif // DOXYGEN_SHOULD_SKIP_THIS
223 
225 class CSVMLight : public CSVM
226 {
227  public:
229  CSVMLight();
230 
237  CSVMLight(float64_t C, CKernel* k, CLabels* lab);
238  virtual ~CSVMLight();
239 
241  void init();
242 
248 
253  int32_t get_runtime();
254 
255 
257  void svm_learn();
258 
275  int32_t optimize_to_convergence(
276  int32_t* docs, int32_t* label, int32_t totdoc, SHRINK_STATE *shrink_state,
277  int32_t *inconsistent, float64_t *a, float64_t *lin, float64_t *c,
278  TIMING *timing_profile, float64_t *maxdiff, int32_t heldout,
279  int32_t retrain);
280 
292  float64_t *a, float64_t *lin, float64_t *c, float64_t* eps, int32_t *label,
293  int32_t totdoc);
294 
299  void clear_index(int32_t *index);
300 
306  void add_to_index(int32_t *index, int32_t elem);
307 
315  int32_t compute_index(int32_t *binfeature, int32_t range, int32_t *index);
316 
335  void optimize_svm(
336  int32_t* docs, int32_t* label, int32_t *exclude_from_eq_const,
337  float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t totdoc,
338  int32_t *working2dnum, int32_t varnum, float64_t *a, float64_t *lin,
339  float64_t *c, float64_t *aicache, QP *qp, float64_t *epsilon_crit_target);
340 
359  int32_t* docs, int32_t* label, int32_t *exclude_from_eq_const,
360  float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t *key,
361  float64_t *a, float64_t *lin, float64_t *c, int32_t varnum, int32_t totdoc,
362  float64_t *aicache, QP *qp);
363 
382  int32_t* docs, int32_t* label, int32_t *exclude_from_eq_const,
383  float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t *key,
384  float64_t *a, float64_t *lin, float64_t *c, int32_t varnum, int32_t totdoc,
385  float64_t *aicache, QP *qp);
386 
399  int32_t calculate_svm_model(
400  int32_t* docs, int32_t *label,float64_t *lin, float64_t *a,
401  float64_t* a_old, float64_t *c, int32_t *working2dnum, int32_t *active2dnum);
402 
419  int32_t check_optimality(
420  int32_t *label, float64_t *a, float64_t* lin, float64_t *c, int32_t totdoc,
421  float64_t *maxdiff, float64_t epsilon_crit_org, int32_t *misclassified,
422  int32_t *inconsistent,int32_t* active2dnum, int32_t *last_suboptimal_at,
423  int32_t iteration);
424 
438  virtual void update_linear_component(
439  int32_t* docs, int32_t *label, int32_t *active2dnum, float64_t *a,
440  float64_t* a_old, int32_t *working2dnum, int32_t totdoc, float64_t *lin,
441  float64_t *aicache, float64_t* c);
442 
447  static void* update_linear_component_mkl_linadd_helper(void* p);
448 
462  int32_t* docs, int32_t *label, int32_t *active2dnum, float64_t *a,
463  float64_t* a_old, int32_t *working2dnum, int32_t totdoc, float64_t *lin,
464  float64_t *aicache);
465 
479  int32_t* docs, int32_t *label, int32_t *active2dnum, float64_t *a,
480  float64_t* a_old, int32_t *working2dnum, int32_t totdoc, float64_t *lin,
481  float64_t *aicache);
482 
483  void call_mkl_callback(float64_t* a, int32_t* label, float64_t* lin);
484 
504  int32_t *label, float64_t *a, float64_t* lin, float64_t* c, int32_t totdoc,
505  int32_t qp_size, int32_t *inconsistent, int32_t* active2dnum,
506  int32_t* working2dnum, float64_t *selcrit, int32_t *select,
507  int32_t cache_only, int32_t *key, int32_t *chosen);
508 
528  int32_t* label, float64_t *a, float64_t *lin, float64_t *c,
529  int32_t totdoc, int32_t qp_size, int32_t *inconsistent,
530  int32_t *active2dnum, int32_t *working2dnum, float64_t *selcrit,
531  int32_t *select, int32_t *key, int32_t *chosen, int32_t iteration);
532 
540  void select_top_n(
541  float64_t *selcrit, int32_t range, int32_t *select, int32_t n);
542 
549  void init_shrink_state(
550  SHRINK_STATE *shrink_state, int32_t totdoc, int32_t maxhistory);
551 
556  void shrink_state_cleanup(SHRINK_STATE *shrink_state);
557 
573  int32_t shrink_problem(
574  SHRINK_STATE *shrink_state, int32_t *active2dnum,
575  int32_t *last_suboptimal_at, int32_t iteration, int32_t totdoc,
576  int32_t minshrink, float64_t *a, int32_t *inconsistent, float64_t* c,
577  float64_t* lin, int* label);
578 
593  virtual void reactivate_inactive_examples(
594  int32_t *label,float64_t *a,SHRINK_STATE *shrink_state, float64_t *lin,
595  float64_t *c, int32_t totdoc,int32_t iteration, int32_t *inconsistent,
596  int32_t *docs,float64_t *aicache, float64_t* maxdiff);
597 
598 protected:
605  virtual float64_t compute_kernel(int32_t i, int32_t j)
606  {
607  return kernel->kernel(i, j);
608  }
609 
614  static void* compute_kernel_helper(void* p);
615 
620  static void* update_linear_component_linadd_helper(void* p);
621 
626  static void* reactivate_inactive_examples_vanilla_helper(void* p);
627 
632  static void* reactivate_inactive_examples_linadd_helper(void* p);
633 
635  virtual const char* get_name() const { return "SVMLight"; }
636 
637  /* interface to QP-solver */
638  float64_t *optimize_qp( QP *qp,float64_t *epsilon_crit, int32_t nx,
639  float64_t *threshold, int32_t& svm_maxqpsize);
640 
649  virtual bool train_machine(CFeatures* data=NULL);
650 
651  protected:
653  MODEL* model;
655  LEARN_PARM* learn_parm;
657  int32_t verbosity;
658 
662  int32_t init_iter;
673 
674  // MKL stuff
675 
681  int32_t count;
688 };
689 }
690 #endif //USE_SVMLIGHT
691 #endif //_SVMLight_H___

SHOGUN Machine Learning Toolbox - Documentation