source: orange/source/orange/libsvm_interface.hpp @ 11606:f27665aa9f40

Revision 11606:f27665aa9f40, 6.0 KB checked in by Ales Erjavec <ales.erjavec@…>, 10 months ago (diff)

Changed TSVMClassifier constructor interface.

It no longer requires "examples" table unless using a custom kernel
and no longer keeps the training "x_space" array (the passed svm_model
needs to 'own' the *(model->SV) array).

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    TSVMLearner();
101    ~TSVMLearner();
102
103    PClassifier operator()(PExampleGenerator, const int & = 0);
104
105protected:
106    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);
107    virtual svm_node* init_problem(svm_problem &problem, PExampleTable examples, int n_elements);
108    virtual int getNumOfElements(PExampleGenerator examples);
109    virtual TSVMClassifier* createClassifier(
110            PVariable var, PExampleTable examples, PExampleTable supportVectors, svm_model* model);
111};
112
113class ORANGE_API TSVMLearnerSparse : public TSVMLearner{
114public:
115    __REGISTER_CLASS
116    bool useNonMeta; //P include non meta attributes in the learning process
117protected:
118    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);
119    virtual int getNumOfElements(PExampleGenerator examples);
120    virtual TSVMClassifier* createClassifier(
121            PVariable classVar, PExampleTable examples, PExampleTable supportVectors, svm_model* model);
122};
123
124
125class ORANGE_API TSVMClassifier : public TClassifierFD{
126public:
127    __REGISTER_CLASS
128    TSVMClassifier(){
129        this->model = NULL;
130    };
131
132    TSVMClassifier(const PVariable & , PExampleTable examples, PExampleTable supportVectors,
133            svm_model* model, PKernelFunc kernelFunc);
134
135    ~TSVMClassifier();
136
137    TValue operator()(const TExample&);
138    PDistribution classDistribution(const TExample &);
139
140    PFloatList getDecisionValues(const TExample &);
141
142    PIntList nSV; //P nSV
143    PFloatList rho; //P rho
144    PFloatListList coef; //P coef
145    PFloatList probA; //P probA - pairwise probability information
146    PFloatList probB; //P probB - pairwise probability information
147    PExampleTable supportVectors; //P support vectors
148    PExampleTable examples; //P (training instances when svm_type == Custom)
149    PKernelFunc kernelFunc; //P custom kernel function
150
151    int svm_type; //P(&SVMLearner_SVMType)  SVM type (C_SVC=0, NU_SVC, ONE_CLASS, EPSILON_SVR=3, NU_SVR=4)
152    int kernel_type; //P(&SVMLearner_Kernel)  kernel type (LINEAR=0, POLY, RBF, SIGMOID, CUSTOM=4)
153
154    svm_model* getModel() {return model;}
155
156protected:
157    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);
158    virtual int getNumOfElements(const TExample& example);
159
160private:
161    svm_model *model;
162};
163
164class ORANGE_API TSVMClassifierSparse : public TSVMClassifier{
165public:
166    __REGISTER_CLASS
167    TSVMClassifierSparse(){};
168    TSVMClassifierSparse(PVariable var, PExampleTable examples,  PExampleTable supportVectors,
169            svm_model* model, bool useNonMeta, PKernelFunc kernelFunc
170            ) :TSVMClassifier(var, examples, supportVectors, model, kernelFunc){
171        this->useNonMeta = useNonMeta;
172    }
173
174    bool useNonMeta; //P include non meta attributes
175
176protected:
177    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);
178    virtual int getNumOfElements(const TExample& example);
179};
180
181#endif
182
Note: See TracBrowser for help on using the repository browser.