Machine Learning Library
CSVM.h
Go to the documentation of this file.
1 /*
2  COPYRIGHT (C) 2003 APPLIED NEUROINFORMATIC GROUP - UNIVERSITY OF BIELEFELD.
3 
4  ALL RIGHTS RESERVED.
5 
6  REDISTRIBUTION AND USE IN SOURCE AND BINARY FORM, WITH OR WITHOUT
7  MODIFICATION, REQUIRE THE PERMISSION OF THE COPYRIGHT HOLDERS.
8 
9  COMMERCIAL USE WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT HOLDERS
10  IS FORBIDDEN
11 
12  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
13  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 
25 
26 #ifndef CSVM_H
27 #define CSVM_H
28 
29 #include "CDenseVector.h"
30 #include "CDataset.h"
31 #include "CClassifier.h"
32 #include "CSVCandidate.h"
33 #include "CKernelCache.h"
34 
35 #include "CKernel.h"
36 #include <vector>
37 
38 #define SVEXCP_BAD_LABEL "Illegal label detected! Label must be '+1' or '-1'."
39 #define SVEXCP_BAD_DATASET "Number of classes differs from two!"
40 
41 #define WARNING_THRESHOLD 1e-11
42 
43 enum TSVMType {
46 };
47 
59 template <class Type>
60 class CSVM : public CClassifier<Type> {
61 
62 
63 
64 protected:
65  vector< CSVCandidate<Type> > tWorkingSet;
68  Type tC;
71  Type tBias;
75  bool bFoldKernel;
81  /* some status flags */
82  vector<int> tTransformedAccess;
83 
84 public:
90  CSVM(TSVMType = L1SVM);
91 
95  ~CSVM ();
96 
101  void setMaxIterations (long lMax) {
102  lMaxIterations = lMax;
103  };
104 
105 
106 
114  void classify (const CVector < Type > &tVec, Type* ptClass,Type* ptMargin) const;
115  void classify (const CVector < Type > &tVec, Type& rtClass,Type& rtMargin) const;
123  void classify (Type * ptVec, Type * ptClass, Type * ptMargin) const;
124 
125  int classify (const CVector < Type > &tVec) const ;
126 
127  string className() const { return "CSVM";};
128 
129  bool isA(const char* acClass) const { if(string(acClass) == "CSVM") return true; else return CClassifier<Type>::isA(acClass);};
130 
131 
132  void train (const CDataset<Type>& tTrainSet);
133 
140  void setRegularization (Type tC);
141 
150  void setWeight(Type tWPos, Type tWNeg);
151 
152 
159  void setTerminationError (Type tMinError) {
160  this->tTerminationError = tMinError;
161  };
162 
163 
164 
165  void trainChunking(CDataset<Type>& rtTrainset, int iStartSetSize,int iMaxChunkSize);
166 
167 
175 
176 
177 
185  return ptKernel;
186  };
187 
188 
192  void setCacheSize (Type tSize) {
193  iCacheSize = (int) tSize;
194  };
195 
196 
197 
198 
207  CDataset<Type> getSupportData (bool bDropNonSV);
209 
210 
211 
212 
214 
215 
220  Type bias() const { return tBias;};
221 
222 
223 
224 
225 
226 
227  bool serialize(fstream& stream, IO_MODE mode);
228  bool serialize2(CArchiv& tA);
229 
230 
231 
232 protected:
238  void analyticSolve (int i1, int i2);
239 
240  inline Type l1Regularizer(Type tClass);
241  inline Type l2Regularizer(Type tClass);
242 
250  void updateGradient (int& a, double& tDeltaA, int& b, double& tDeltaB);
251 
256  void init();
257 
261  void optimizeWorkingSet ();
262 
263  bool checkShrinking (int, int);
264 
270  void rebuildGradient();
271 
272 
277  Type calculateBias ();
278 
279 
280  bool selectPair (int *piI1, int *piI2, Type & tError);
281 
282 
283 
284 
285 };
286 
287 #endif
Type tWeightPos
Definition: CSVM.h:69
Type l2Regularizer(Type tClass)
TSVMType tSVMType
Definition: CSVM.h:78
Type tBias
Definition: CSVM.h:71
vector< int > tTransformedAccess
Definition: CSVM.h:82
string className() const
Definition: CSVM.h:127
IO_MODE
Definition: CObject.h:38
void optimizeWorkingSet()
void updateGradient(int &a, double &tDeltaA, int &b, double &tDeltaB)
Support Vector Machine algorithm for binary classifcation.
Definition: CSVM.h:60
void setMaxIterations(long lMax)
Definition: CSVM.h:101
bool serialize2(CArchiv &tA)
Type tC
Definition: CSVM.h:68
CDataset< Type > getNonSupportData() const
void setTerminationError(Type tMinError)
Definition: CSVM.h:159
bool bUnshrinked
Definition: CSVM.h:74
vector< CSVCandidate< Type > > tWorkingSet
Definition: CSVM.h:65
void init()
void setRegularization(Type tC)
void rebuildGradient()
CDenseVector< Type > tWeight
Definition: CSVM.h:79
bool checkShrinking(int, int)
long lNextShrink
Definition: CSVM.h:73
Type tWeightNeg
Definition: CSVM.h:69
bool selectPair(int *piI1, int *piI2, Type &tError)
CDataset< Type > getSupportData(bool bDropNonSV)
void setWeight(Type tWPos, Type tWNeg)
Definition: CSVM.h:45
CKernelCache< Type > * ptCache
Definition: CSVM.h:66
TSVMType
Definition: CSVM.h:43
void analyticSolve(int i1, int i2)
Definition: CKernelCache.h:39
void trainChunking(CDataset< Type > &rtTrainset, int iStartSetSize, int iMaxChunkSize)
Type l1Regularizer(Type tClass)
CSVM(TSVMType=L1SVM)
CDenseVector< Type > getWeightVector() const
Definition: CSVM.h:213
Base class of object serialization.
Definition: CArchiv.h:19
long lMaxIterations
Definition: CSVM.h:72
bool isA(const char *acClass) const
Definition: CSVM.h:129
Base class for kernel functions Base class for kernel function used for example by the support vector...
Definition: CKernel.h:46
Template object for vectors of single and double precision and integer.
Definition: CDenseVector.h:37
templatized vector for numerical applications
Definition: CMatrix.h:39
Type bias() const
Definition: CSVM.h:220
Manages pairs of input and output vectors.
Definition: CDataset.h:110
Type tTerminationError
Definition: CSVM.h:70
int iCacheSize
Definition: CSVM.h:80
void setKernel(CKernel< Type > *ptKernel)
void train(const CDataset< Type > &tTrainSet)
Definition: CSVM.h:44
void classify(const CVector< Type > &tVec, Type *ptClass, Type *ptMargin) const
Type calculateBias()
bool bFoldKernel
Definition: CSVM.h:75
bool serialize(fstream &stream, IO_MODE mode)
CKernel< Type > * ptKernel
Definition: CSVM.h:67
const CKernel< Type > * kernel()
Definition: CSVM.h:184
int iNumIteration
Definition: CSVM.h:76
void setCacheSize(Type tSize)
Definition: CSVM.h:192
int iNumActiveItems
Definition: CSVM.h:77