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