KSquare Utilities
svm289_BFS.h
Go to the documentation of this file.
1 #ifndef _SVM289_BFS_
2 #define _SVM289_BFS_
3 
4 #define LIBSVM_VERSION 289
5 
6 #include "FeatureNumList.h"
7 #include "FeatureVector.h"
8 #include "KKStr.h"
9 
10 using namespace KKMLL;
11 
12 
13 /**
14  *@namespace SVM289_BFS
15  *@brief Namespace used to wrap implementation of libSVM version 2.89 to be used as a pair-wise SVM.
16  *@details There is more than one version of libSVM implemented in the library. To prevent
17  * name conflicts between them each one was wrapped in their own namespace.
18  *<br/>
19  * libSVM is a Support Vector Machine implemented done by "Chih-Chung Chang" and "Chih-Jen Lin". It
20  * was downloaded from http://www.csie.ntu.edu.tw/~cjlin/libsvm/. The source code was modified by
21  * Kurt Kramer. The primary changes to this implementation involves the replacement of the sparse data-structure
22  * in the original implementation with fixed length array implemented through the "FeatureVector" class and
23  * the ability to specify a sub-set of features to be utilized via the "FeatureNumList" class. This allows
24  * us to load in a single set of training data with all its features that can then be used for multiple Support
25  * Vector Machine instances where each instance utilizes a different set of features. This particular implementation
26  * SVM289_BFS was meant to work with the Binary-Feature-Selection (Pair-Wise) version of the support vector machine as
27  * described by "Increased classification accuracy and speedup through pair-wise feature selection for support vector machines."
28  */
29 namespace SVM289_BFS
30 {
31  //#ifdef __cplusplus
32  //extern "C" {
33  //#endif
34 
35  extern kkint32 libsvm_version;
36 
37 
38  struct svm_problem
39  {
40  svm_problem (const svm_problem& _prob);
41 
42  svm_problem (const FeatureVectorList& _x,
43  const float* _y,
44  const FeatureNumList& _selFeatures
45  );
46 
47  svm_problem (const FeatureNumList& _selFeatures,
48  FileDescPtr _fileDesc,
49  RunLog& _log
50  );
51 
52  ~svm_problem ();
53 
54  FileDescPtr FileDesc () const;
55 
56  const FeatureNumList& SelFeatures () const {return selFeatures;}
57 
62  double* y;
63  }; /* svm_problem */
64 
65 
66 
67  enum class SVM_Type
68  {
69  SVM_NULL,
70  C_SVC,
71  NU_SVC,
72  ONE_CLASS,
74  NU_SVR
75  }; /* svm_type */
76 
77  typedef enum { Kernel_NULL, LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED } Kernel_Type; /* kernel_type */
78 
79 
81  KKStr SVM_Type_ToStr (SVM_Type svmType);
82 
83  Kernel_Type Kernel_Type_FromStr (KKStr s);
84  KKStr Kernel_Type_ToStr (Kernel_Type kernelType);
85 
86 
88  {
89  svm_parameter ();
90  svm_parameter (const svm_parameter& _param);
91  svm_parameter (KKStr& paramStr);
92 
93  ~svm_parameter ();
94 
95  void Cost (double _cost) {C = _cost;}
96  void Gamma (double _gamma) {gamma = _gamma;}
97  void KernalType (Kernel_Type _kernalType) {kernel_type = _kernalType;}
98  void SvmType (SVM_Type _svm_type) {svm_type = _svm_type;}
99 
100  double Cost () const {return C;}
101  double Gamma () const {return gamma;}
102  Kernel_Type KernalType () const {return kernel_type;}
103  SVM_Type SvmType () const {return svm_type;}
104 
105 
106  svm_parameter& operator= (const svm_parameter& right);
107 
108  KKStr ToCmdLineStr () const;
109  KKStr ToTabDelStr () const;
110  void ParseTabDelStr (const KKStr& _str);
111 
112  void ProcessSvmParameter (const KKStr& cmd,
113  const KKStr& value,
114  bool& parmUsed
115  );
116 
118  Kernel_Type kernel_type;
119  kkint32 degree; /* for poly */
120  double gamma; /* for poly/rbf/sigmoid */
121  double coef0; /* for poly/sigmoid */
122 
123  /* these are for training only */
124  double cache_size; /* in MB */
125  double eps; /* stopping criteria */
126  double C; /* for C_SVC, EPSILON_SVR and NU_SVR */
127  kkint32 nr_weight; /* for C_SVC */
128  kkint32* weight_label; /* for C_SVC */
129  double* weight; /* for C_SVC */
130  double nu; /* for NU_SVC, ONE_CLASS, and NU_SVR */
131  double p; /* for EPSILON_SVR */
132  kkint32 shrinking; /* use the shrinking heuristics */
133  kkint32 probability; /* do probability estimates */
134 
135  double probParam; /* probability parameter as done using USF multi class prob calculation */
136 
137  static const char* svm_type_table[];
138 
139  static const char* kernel_type_table[];
140 
141  }; /* svm_parameter */
142 
143 
144 
145 
146  struct svm_model
147  {
148  svm_model (const svm_model& _model,
149  FileDescPtr _fileDesc,
150  RunLog& _log
151  );
152 
153  svm_model (FileDescPtr _fileDesc,
154  RunLog& _log
155  );
156 
157  svm_model (const svm_parameter& _param,
158  const FeatureNumList& _selFeatures,
159  FileDescPtr _fileDesc,
160  RunLog& _log
161  );
162 
163  svm_model (istream& _fileName,
164  FileDescPtr _fileDesc,
165  RunLog& _log
166  );
167 
168  ~svm_model ();
169 
170  double* DecValues ();
171  double* ProbEstimates ();
172  double** PairwiseProb ();
173 
174  void Write (ostream& o);
175 
176  void Read (istream& i,
177  FileDescPtr fileDesc,
178  RunLog& log
179  );
180 
181  void NormalizeProbability ();
182 
183 
184  svm_parameter param; // parameter
185  kkint32 nr_class; /**< number of classes, = 2 in regression/one class svm */
186  kkint32 l; /**< total #SV */
187  FeatureVectorList SV; /**< SVs (SV[l]) */
188  double** sv_coef; /**< Coefficients for SVs in decision functions (sv_coef[k-1][l]) */
189  double* rho; /**< constants in decision functions (rho[k*(k-1)/2]) */
190  double* probA; /**< pair-wise probability information */
191  double* probB;
193 
194  // for classification only
195 
196  kkint32* label; /**< label of each class (label[k]) */
197  kkint32* nSV; /**< number of SVs for each class (nSV[k]) */
198  // nSV[0] + nSV[1] + ... + nSV[k-1] = l
199  // XXX
200  bool weOwnSupportVectors; // 1 if svm_model is created by svm_load_model
201  // 0 if svm_model is created by svm_train
202 
203 
204  // Support Prediction Calculations
205  double* dec_values;
206  double** pairwise_prob;
207  double* prob_estimates;
208  };
209 
210 
211  svm_model* svm_train (const svm_problem& prob,
212  const svm_parameter& param,
213  RunLog& log
214  );
215 
216  SVM_Type svm_get_svm_type (const svm_model *model);
217 
218  kkint32 svm_get_nr_class (const svm_model *model);
219 
220  void svm_get_labels (const svm_model* model,
221  kkint32* label
222  );
223 
224  double svm_get_svr_probability (const svm_model *model);
225 
226 
227  void svm_predict_values (const svm_model* model,
228  const FeatureVector& x,
229  double* dec_values
230  );
231 
232 
233  double svm_predict (const struct svm_model* model,
234  const FeatureVector& x
235  );
236 
237 
238  double svm_predict_probability ( svm_model* model,
239  const FeatureVector& x,
240  double* prob_estimates,
241  kkint32* votes
242  );
243 
244  void svm_destroy_model (struct svm_model*& model);
245 
246 
247  void svm_destroy_param (struct svm_parameter*& param);
248 
249 
250  const char *svm_check_parameter (const struct svm_problem* prob,
251  const struct svm_parameter* param
252  );
253 
254 
255  kkint32 svm_check_probability_model(const struct svm_model *model);
256 
257  extern void (*svm_print_string) (const char *);
258 
259  //#ifdef __cplusplus
260  //}
261  //#endif
262 
263 
264 
265  template <class T> inline void swap(T& x, T& y) { T t=x; x=y; y=t; }
266 
267 
268 
269  typedef float Qfloat;
270 
271  typedef signed char schar;
272 
273 
274  template <class S, class T> inline void clone(T*& dst, S* src, kkint32 n)
275  {
276  dst = new T[n];
277  memcpy((void *)dst,(void *)src,sizeof(T)*n);
278  }
279 
280  inline double powi (double base, kkint32 times);
281 
282  class QMatrix;
283  class Cache;
284  class Kernel;
285  class Solver;
286  class Solver_NU;
287  class SVC_Q;
288  class ONE_CLASS_Q;
289  class SVR_Q;
290  struct decision_function;
291 
292 } /* SVM289_BFS */
293 
294 
295 #endif /* _LIBSVM_H */
FeatureVectorList SV
Definition: svm289_BFS.h:187
SVM_Type SvmType() const
Definition: svm289_BFS.h:103
void SvmType(SVM_Type _svm_type)
Definition: svm289_BFS.h:98
void KernalType(Kernel_Type _kernalType)
Definition: svm289_BFS.h:97
svm_parameter & operator=(const svm_parameter &right)
Definition: svm289_BFS.cpp:266
KKStr ToCmdLineStr() const
Definition: svm289_BFS.cpp:304
svm_model(const svm_model &_model, FileDescPtr _fileDesc, RunLog &_log)
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 libsvm_version
Keeps track of selected features.
kkint32 svm_check_probability_model(const struct svm_model *model)
Namespace used to wrap implementation of libSVM version 2.89 to be used as a pair-wise SVM...
Definition: svm289_BFS.cpp:42
KKStr SVM_Type_ToStr(SVM_Type svmType)
Definition: svm289_BFS.cpp:560
KKStr Kernel_Type_ToStr(Kernel_Type kernelType)
Definition: svm289_BFS.cpp:600
svm_model(const svm_parameter &_param, const FeatureNumList &_selFeatures, FileDescPtr _fileDesc, RunLog &_log)
double Gamma() const
Definition: svm289_BFS.h:101
FeatureNumList selFeatures
Definition: svm289_BFS.h:192
static const char * svm_type_table[]
Definition: svm289_BFS.h:137
void svm_get_labels(const svm_model *model, kkint32 *label)
void clone(T *&dst, S *src, kkint32 n)
Definition: svm289_BFS.h:274
Kernel_Type Kernel_Type_FromStr(KKStr s)
Definition: svm289_BFS.cpp:575
float Qfloat
Definition: svm289_BFS.h:269
static const char * kernel_type_table[]
Definition: svm289_BFS.h:139
double ** pairwise_prob
Definition: svm289_BFS.h:206
void svm_predict_values(const svm_model *model, const FeatureVector &x, double *dec_values)
svm_problem(const svm_problem &_prob)
Definition: svm289_BFS.cpp:115
FeatureVectorList x
Definition: svm289_BFS.h:61
void ProcessSvmParameter(const KKStr &cmd, const KKStr &value, bool &parmUsed)
Definition: svm289_BFS.cpp:332
double svm_predict_probability(svm_model *model, const FeatureVector &x, double *prob_estimates, kkint32 *votes)
void Read(istream &i, FileDescPtr fileDesc, RunLog &log)
kkint32 svm_get_nr_class(const svm_model *model)
Container class for FeatureVector derived objects.
svm_model(istream &_fileName, FileDescPtr _fileDesc, RunLog &_log)
void svm_destroy_param(struct svm_parameter *&param)
double Cost() const
Definition: svm289_BFS.h:100
void Gamma(double _gamma)
Definition: svm289_BFS.h:96
svm_parameter(const svm_parameter &_param)
Definition: svm289_BFS.cpp:200
KKStr ToTabDelStr() const
Definition: svm289_BFS.cpp:408
FileDescPtr FileDesc() const
Definition: svm289_BFS.cpp:172
double powi(double base, kkint32 times)
Definition: svm289_BFS.cpp:93
void ParseTabDelStr(const KKStr &_str)
Definition: svm289_BFS.cpp:454
FileDescPtr fileDesc
Definition: svm289_BFS.h:58
double svm_predict(const struct svm_model *model, const FeatureVector &x)
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void NormalizeProbability()
Derining multiclass probability as done in "Recognizing Plankton Images From the SIPPER".
svm_problem(const FeatureVectorList &_x, const float *_y, const FeatureNumList &_selFeatures)
Definition: svm289_BFS.cpp:129
FileDesc * FileDescPtr
svm_model * svm_train(const svm_problem &prob, const svm_parameter &param, RunLog &log)
SVM_Type svm_get_svm_type(const svm_model *model)
svm_parameter(KKStr &paramStr)
Definition: svm289_BFS.cpp:235
void svm_destroy_model(struct svm_model *&model)
SVM_Type SVM_Type_FromStr(KKStr s)
Definition: svm289_BFS.cpp:536
signed char schar
Definition: svm289_BFS.h:271
Used for logging messages.
Definition: RunLog.h:49
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
void swap(T &x, T &y)
Definition: svm289_BFS.h:265
svm_model(FileDescPtr _fileDesc, RunLog &_log)
Kernel_Type KernalType() const
Definition: svm289_BFS.h:102
FeatureNumList selFeatures
Definition: svm289_BFS.h:60
const char * svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param)
Represents a Feature Vector of a single example, labeled or unlabeled.
Definition: FeatureVector.h:59
double ** PairwiseProb()
svm_parameter param
Definition: svm289_BFS.h:184
void(* svm_print_string)(const char *)
Definition: svm289_BFS.cpp:624
svm_problem(const FeatureNumList &_selFeatures, FileDescPtr _fileDesc, RunLog &_log)
Definition: svm289_BFS.cpp:149
double svm_get_svr_probability(const svm_model *model)
const FeatureNumList & SelFeatures() const
Definition: svm289_BFS.h:56
void Write(ostream &o)
void Cost(double _cost)
Definition: svm289_BFS.h:95