source: orange/source/orange/libsvm_interface.hpp @ 10389:8d7ba785b51c

Revision 10389:8d7ba785b51c, 6.0 KB checked in by Ales Erjavec <ales.erjavec@…>, 2 years ago (diff)

Removed redundant slist includes

Line 
1/*
2 
3 Copyright (c) 2000-2010 Chih-Chung Chang and Chih-Jen Lin
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 
17 3. Neither name of copyright holders nor the names of its contributors
18 may be used to endorse or promote products derived from this software
19 without specific prior written permission.
20 
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
26 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35
36#ifndef __SVM_HPP
37#define __SVM_HPP
38
39#include "table.hpp"
40
41#include "classify.hpp"
42#include "learn.hpp"
43#include "orange.hpp"
44#include "domain.hpp"
45#include "examplegen.hpp"
46#include "table.hpp"
47#include "examples.hpp"
48#include "distance.hpp"
49
50#include "libsvm/svm.h"
51
52svm_model *svm_load_model_alt(string& buffer);
53int svm_save_model_alt(string& buffer, const svm_model *model);
54
55WRAPPER(ExampleGenerator)
56WRAPPER(KernelFunc)
57WRAPPER(SVMLearner)
58WRAPPER(SVMClassifier)
59WRAPPER(ExampleTable)
60
61class ORANGE_API TKernelFunc: public TOrange{
62public:
63    __REGISTER_ABSTRACT_CLASS
64    virtual float operator()(const TExample &, const TExample &)=0;
65};
66
67WRAPPER(KernelFunc)
68
69//#include "callback.hpp"
70
71class ORANGE_API TSVMLearner : public TLearner{
72public:
73    __REGISTER_CLASS
74
75  CLASSCONSTANTS(SVMType: C_SVC=C_SVC; Nu_SVC=NU_SVC; OneClass=ONE_CLASS; Epsilon_SVR=EPSILON_SVR; Nu_SVR=NU_SVR)
76  CLASSCONSTANTS(Kernel: Linear=LINEAR; Polynomial=POLY; RBF=RBF; Sigmoid=SIGMOID; Custom=PRECOMPUTED)
77  CLASSCONSTANTS(LIBSVM_VERSION: VERSION=LIBSVM_VERSION)
78
79    //parameters
80    int svm_type; //P(&SVMLearner_SVMType)  SVM type (C_SVC=0, NU_SVC, ONE_CLASS, EPSILON_SVR=3, NU_SVR=4)
81    int kernel_type; //P(&SVMLearner_Kernel)  kernel type (LINEAR=0, POLY, RBF, SIGMOID, CUSTOM=4)
82    float degree;   //P polynomial kernel degree
83    float gamma;    //P poly/rbf/sigm parameter
84    float coef0;    //P poly/sigm parameter
85    float cache_size; //P cache size in MB
86    float eps;  //P stopping criteria
87    float C;    //P for C_SVC and C_SVR
88    float nu;   //P for NU_SVC and ONE_CLASS
89    float p;    //P for C_SVR
90    int shrinking;  //P shrinking
91    int probability;    //P probability
92    bool verbose;       //P verbose
93
94    int nr_weight;      /* for C_SVC */
95    int *weight_label;  /* for C_SVC */
96    double* weight;     /* for C_SVC */
97
98    PKernelFunc kernelFunc; //P custom kernel function
99
100    PExampleTable tempExamples;
101
102    TSVMLearner();
103    ~TSVMLearner();
104
105    PClassifier operator()(PExampleGenerator, const int & = 0);
106
107protected:
108    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);
109    virtual svm_node* init_problem(svm_problem &problem, PExampleTable examples, int n_elements);
110    virtual int getNumOfElements(PExampleGenerator examples);
111    virtual TSVMClassifier* createClassifier(PVariable var, PExampleTable ex, svm_model* model, svm_node* x_space);
112};
113
114class ORANGE_API TSVMLearnerSparse : public TSVMLearner{
115public:
116    __REGISTER_CLASS
117    bool useNonMeta; //P include non meta attributes in the learning process
118protected:
119    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);
120    virtual int getNumOfElements(PExampleGenerator examples);
121    virtual TSVMClassifier* createClassifier(PVariable var, PExampleTable ex, svm_model* model, svm_node* x_space);
122};
123
124
125class ORANGE_API TSVMClassifier : public TClassifierFD{
126public:
127    __REGISTER_CLASS
128    TSVMClassifier(){
129        this->model = NULL;
130        this->x_space = NULL;
131    };
132
133    TSVMClassifier(const PVariable & , PExampleTable, svm_model*, svm_node*, PKernelFunc);
134    ~TSVMClassifier();
135
136    TValue operator()(const TExample&);
137    PDistribution classDistribution(const TExample &);
138
139    PFloatList getDecisionValues(const TExample &);
140
141    PIntList nSV; //P nSV
142    PFloatList rho; //P rho
143    PFloatListList coef; //P coef
144    PFloatList probA; //P probA - pairwise probability information
145    PFloatList probB; //P probB - pairwise probability information
146    PExampleTable supportVectors; //P support vectors
147    PExampleTable examples; //P examples used to train the classifier
148    PKernelFunc kernelFunc; //P custom kernel function
149
150    int svm_type; //P(&SVMLearner_SVMType)  SVM type (C_SVC=0, NU_SVC, ONE_CLASS, EPSILON_SVR=3, NU_SVR=4)
151    int kernel_type; //P(&SVMLearner_Kernel)  kernel type (LINEAR=0, POLY, RBF, SIGMOID, CUSTOM=4)
152
153    svm_model* getModel() {return model;}
154
155protected:
156    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);
157    virtual int getNumOfElements(const TExample& example);
158
159private:
160    svm_model *model;
161    svm_node *x_space;
162};
163
164class ORANGE_API TSVMClassifierSparse : public TSVMClassifier{
165public:
166    __REGISTER_CLASS
167    TSVMClassifierSparse(){};
168    TSVMClassifierSparse(PVariable var , PExampleTable ex, svm_model* model,
169            svm_node* x_space, bool useNonMeta, PKernelFunc kernelFunc)
170    :TSVMClassifier(var, ex, model, x_space, kernelFunc){
171        this->useNonMeta=useNonMeta;
172    }
173    bool useNonMeta; //P include non meta attributes
174protected:
175    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);
176    virtual int getNumOfElements(const TExample& example);
177};
178
179#endif
180
Note: See TracBrowser for help on using the repository browser.