SHOGUN  v3.0.0
 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/kernel/Kernel.h>
31 #include <shogun/lib/common.h>
32 
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <time.h>
38 
39 namespace shogun
40 {
41 //# define VERSION "V3.50 -- correct??"
42 //# define VERSION_DATE "01.11.00 -- correct??"
43 
44 # define DEF_PRECISION 1E-14
45 # define MAXSHRINK 50000
46 
47 #ifndef DOXYGEN_SHOULD_SKIP_THIS
48 
49 struct MODEL {
51 int32_t sv_num;
53 int32_t at_upper_bound;
55 float64_t b;
57 int32_t* supvec;
59 float64_t *alpha;
61 int32_t *index;
63 int32_t totdoc;
65 CKernel* kernel;
66 
67 /* the following values are not written to file */
69 float64_t loo_error;
71 float64_t loo_recall;
73 float64_t loo_precision;
74 
76 float64_t xa_error;
78 float64_t xa_recall;
80 float64_t xa_precision;
81 };
82 
84 typedef struct quadratic_program {
86  int32_t opt_n;
88  int32_t opt_m;
90  float64_t *opt_ce;
92  float64_t *opt_ce0;
94  float64_t *opt_g;
96  float64_t *opt_g0;
98  float64_t *opt_xinit;
100  float64_t *opt_low;
102  float64_t *opt_up;
103 } QP;
104 
106 typedef int32_t FNUM;
107 
109 typedef float64_t FVAL;
110 
112 struct LEARN_PARM {
114  int32_t type;
116  float64_t svm_c;
118  float64_t* eps;
120  float64_t svm_costratio;
122  float64_t transduction_posratio;
123  /* classified as positives */
125  int32_t biased_hyperplane;
130  int32_t sharedslack;
132  int32_t svm_maxqpsize;
134  int32_t svm_newvarsinqp;
136  int32_t kernel_cache_size;
138  float64_t epsilon_crit;
140  float64_t epsilon_shrink;
142  int32_t svm_iter_to_shrink;
146  int32_t maxiter;
148  int32_t remove_inconsistent;
152  int32_t skip_final_opt_check;
154  int32_t compute_loo;
158  float64_t rho;
162  int32_t xa_depth;
164  char predfile[200];
168  char alphafile[200];
169 
170  /* you probably do not want to touch the following */
172  float64_t epsilon_const;
174  float64_t epsilon_a;
176  float64_t opt_precision;
177 
178  /* the following are only for internal use */
180  int32_t svm_c_steps;
182  float64_t svm_c_factor;
184  float64_t svm_costratio_unlab;
186  float64_t svm_unlabbound;
188  float64_t *svm_cost;
189 };
190 
192 struct TIMING {
194  int32_t time_kernel;
196  int32_t time_opti;
198  int32_t time_shrink;
200  int32_t time_update;
202  int32_t time_model;
204  int32_t time_check;
206  int32_t time_select;
207 };
208 
209 
211 struct SHRINK_STATE
212 {
214  int32_t *active;
216  int32_t *inactive_since;
218  int32_t deactnum;
220  float64_t **a_history;
222  int32_t maxhistory;
224  float64_t *last_a;
226  float64_t *last_lin;
227 };
228 #endif // DOXYGEN_SHOULD_SKIP_THIS
229 
231 class CSVMLight : public CSVM
232 {
233  public:
235  CSVMLight();
236 
243  CSVMLight(float64_t C, CKernel* k, CLabels* lab);
244  virtual ~CSVMLight();
245 
247  void init();
248 
254 
259  int32_t get_runtime();
260 
261 
263  void svm_learn();
264 
281  int32_t optimize_to_convergence(
282  int32_t* docs, int32_t* label, int32_t totdoc, SHRINK_STATE *shrink_state,
283  int32_t *inconsistent, float64_t *a, float64_t *lin, float64_t *c,
284  TIMING *timing_profile, float64_t *maxdiff, int32_t heldout,
285  int32_t retrain);
286 
298  float64_t *a, float64_t *lin, float64_t *c, float64_t* eps, int32_t *label,
299  int32_t totdoc);
300 
305  void clear_index(int32_t *index);
306 
312  void add_to_index(int32_t *index, int32_t elem);
313 
321  int32_t compute_index(int32_t *binfeature, int32_t range, int32_t *index);
322 
341  void optimize_svm(
342  int32_t* docs, int32_t* label, int32_t *exclude_from_eq_const,
343  float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t totdoc,
344  int32_t *working2dnum, int32_t varnum, float64_t *a, float64_t *lin,
345  float64_t *c, float64_t *aicache, QP *qp, float64_t *epsilon_crit_target);
346 
365  int32_t* docs, int32_t* label, int32_t *exclude_from_eq_const,
366  float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t *key,
367  float64_t *a, float64_t *lin, float64_t *c, int32_t varnum, int32_t totdoc,
368  float64_t *aicache, QP *qp);
369 
388  int32_t* docs, int32_t* label, int32_t *exclude_from_eq_const,
389  float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t *key,
390  float64_t *a, float64_t *lin, float64_t *c, int32_t varnum, int32_t totdoc,
391  float64_t *aicache, QP *qp);
392 
405  int32_t calculate_svm_model(
406  int32_t* docs, int32_t *label,float64_t *lin, float64_t *a,
407  float64_t* a_old, float64_t *c, int32_t *working2dnum, int32_t *active2dnum);
408 
425  int32_t check_optimality(
426  int32_t *label, float64_t *a, float64_t* lin, float64_t *c, int32_t totdoc,
427  float64_t *maxdiff, float64_t epsilon_crit_org, int32_t *misclassified,
428  int32_t *inconsistent,int32_t* active2dnum, int32_t *last_suboptimal_at,
429  int32_t iteration);
430 
444  virtual void update_linear_component(
445  int32_t* docs, int32_t *label, int32_t *active2dnum, float64_t *a,
446  float64_t* a_old, int32_t *working2dnum, int32_t totdoc, float64_t *lin,
447  float64_t *aicache, float64_t* c);
448 
453  static void* update_linear_component_mkl_linadd_helper(void* p);
454 
468  int32_t* docs, int32_t *label, int32_t *active2dnum, float64_t *a,
469  float64_t* a_old, int32_t *working2dnum, int32_t totdoc, float64_t *lin,
470  float64_t *aicache);
471 
485  int32_t* docs, int32_t *label, int32_t *active2dnum, float64_t *a,
486  float64_t* a_old, int32_t *working2dnum, int32_t totdoc, float64_t *lin,
487  float64_t *aicache);
488 
489  void call_mkl_callback(float64_t* a, int32_t* label, float64_t* lin);
490 
510  int32_t *label, float64_t *a, float64_t* lin, float64_t* c, int32_t totdoc,
511  int32_t qp_size, int32_t *inconsistent, int32_t* active2dnum,
512  int32_t* working2dnum, float64_t *selcrit, int32_t *select,
513  int32_t cache_only, int32_t *key, int32_t *chosen);
514 
534  int32_t* label, float64_t *a, float64_t *lin, float64_t *c,
535  int32_t totdoc, int32_t qp_size, int32_t *inconsistent,
536  int32_t *active2dnum, int32_t *working2dnum, float64_t *selcrit,
537  int32_t *select, int32_t *key, int32_t *chosen, int32_t iteration);
538 
546  void select_top_n(
547  float64_t *selcrit, int32_t range, int32_t *select, int32_t n);
548 
555  void init_shrink_state(
556  SHRINK_STATE *shrink_state, int32_t totdoc, int32_t maxhistory);
557 
562  void shrink_state_cleanup(SHRINK_STATE *shrink_state);
563 
579  int32_t shrink_problem(
580  SHRINK_STATE *shrink_state, int32_t *active2dnum,
581  int32_t *last_suboptimal_at, int32_t iteration, int32_t totdoc,
582  int32_t minshrink, float64_t *a, int32_t *inconsistent, float64_t* c,
583  float64_t* lin, int* label);
584 
599  virtual void reactivate_inactive_examples(
600  int32_t *label,float64_t *a,SHRINK_STATE *shrink_state, float64_t *lin,
601  float64_t *c, int32_t totdoc,int32_t iteration, int32_t *inconsistent,
602  int32_t *docs,float64_t *aicache, float64_t* maxdiff);
603 
604 protected:
611  virtual float64_t compute_kernel(int32_t i, int32_t j)
612  {
613  return kernel->kernel(i, j);
614  }
615 
620  static void* compute_kernel_helper(void* p);
621 
626  static void* update_linear_component_linadd_helper(void* p);
627 
632  static void* reactivate_inactive_examples_vanilla_helper(void* p);
633 
638  static void* reactivate_inactive_examples_linadd_helper(void* p);
639 
641  virtual const char* get_name() const { return "SVMLight"; }
642 
643  /* interface to QP-solver */
644  float64_t *optimize_qp( QP *qp,float64_t *epsilon_crit, int32_t nx,
645  float64_t *threshold, int32_t& svm_maxqpsize);
646 
655  virtual bool train_machine(CFeatures* data=NULL);
656 
657  protected:
659  MODEL* model;
661  LEARN_PARM* learn_parm;
663  int32_t verbosity;
664 
668  int32_t init_iter;
679 
680  // MKL stuff
681 
687  int32_t count;
694 };
695 }
696 #endif //USE_SVMLIGHT
697 #endif //_SVMLight_H___

SHOGUN Machine Learning Toolbox - Documentation