source: orange/source/orange/libsvm_interface.hpp @ 9187:efbe212e36e7

Revision 9187:efbe212e36e7, 6.0 KB checked in by ales_erjavec <ales.erjavec@…>, 2 years ago (diff)

Fixed problem initialization from a sparse dataset in the SVMLearnerSparse (completely broken since I changed the custom kernels to use PRECOMPUTED)

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#include "slist.hpp"
50
51#include "libsvm/svm.h"
52
53svm_model *svm_load_model_alt(string& buffer);
54int svm_save_model_alt(string& buffer, const svm_model *model);
55
56WRAPPER(ExampleGenerator)
57WRAPPER(KernelFunc)
58WRAPPER(SVMLearner)
59WRAPPER(SVMClassifier)
60WRAPPER(ExampleTable)
61
62class ORANGE_API TKernelFunc: public TOrange{
63public:
64    __REGISTER_ABSTRACT_CLASS
65    virtual float operator()(const TExample &, const TExample &)=0;
66};
67
68WRAPPER(KernelFunc)
69
70//#include "callback.hpp"
71
72class ORANGE_API TSVMLearner : public TLearner{
73public:
74    __REGISTER_CLASS
75
76  CLASSCONSTANTS(SVMType: C_SVC=C_SVC; Nu_SVC=NU_SVC; OneClass=ONE_CLASS; Epsilon_SVR=EPSILON_SVR; Nu_SVR=NU_SVR)
77  CLASSCONSTANTS(Kernel: Linear=LINEAR; Polynomial=POLY; RBF=RBF; Sigmoid=SIGMOID; Custom=PRECOMPUTED)
78  CLASSCONSTANTS(LIBSVM_VERSION: VERSION=LIBSVM_VERSION)
79
80    //parameters
81    int svm_type; //P(&SVMLearner_SVMType)  SVM type (C_SVC=0, NU_SVC, ONE_CLASS, EPSILON_SVR=3, NU_SVR=4)
82    int kernel_type; //P(&SVMLearner_Kernel)  kernel type (LINEAR=0, POLY, RBF, SIGMOID, CUSTOM=4)
83    float degree;   //P polynomial kernel degree
84    float gamma;    //P poly/rbf/sigm parameter
85    float coef0;    //P poly/sigm parameter
86    float cache_size; //P cache size in MB
87    float eps;  //P stopping criteria
88    float C;    //P for C_SVC and C_SVR
89    float nu;   //P for NU_SVC and ONE_CLASS
90    float p;    //P for C_SVR
91    int shrinking;  //P shrinking
92    int probability;    //P probability
93    bool verbose;       //P verbose
94
95    int nr_weight;      /* for C_SVC */
96    int *weight_label;  /* for C_SVC */
97    double* weight;     /* for C_SVC */
98
99    PKernelFunc kernelFunc; //P custom kernel function
100
101    PExampleTable tempExamples;
102
103    TSVMLearner();
104    ~TSVMLearner();
105
106    PClassifier operator()(PExampleGenerator, const int & = 0);
107
108protected:
109    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);
110    virtual svm_node* init_problem(svm_problem &problem, PExampleTable examples, int n_elements);
111    virtual int getNumOfElements(PExampleGenerator examples);
112    virtual TSVMClassifier* createClassifier(PVariable var, PExampleTable ex, svm_model* model, svm_node* x_space);
113};
114
115class ORANGE_API TSVMLearnerSparse : public TSVMLearner{
116public:
117    __REGISTER_CLASS
118    bool useNonMeta; //P include non meta attributes in the learning process
119protected:
120    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);
121    virtual int getNumOfElements(PExampleGenerator examples);
122    virtual TSVMClassifier* createClassifier(PVariable var, PExampleTable ex, svm_model* model, svm_node* x_space);
123};
124
125
126class ORANGE_API TSVMClassifier : public TClassifierFD{
127public:
128    __REGISTER_CLASS
129    TSVMClassifier(){
130        this->model = NULL;
131        this->x_space = NULL;
132    };
133
134    TSVMClassifier(const PVariable & , PExampleTable, svm_model*, svm_node*, PKernelFunc);
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 examples used to train the classifier
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    svm_node *x_space;
163};
164
165class ORANGE_API TSVMClassifierSparse : public TSVMClassifier{
166public:
167    __REGISTER_CLASS
168    TSVMClassifierSparse(){};
169    TSVMClassifierSparse(PVariable var , PExampleTable ex, svm_model* model,
170            svm_node* x_space, bool useNonMeta, PKernelFunc kernelFunc)
171    :TSVMClassifier(var, ex, model, x_space, kernelFunc){
172        this->useNonMeta=useNonMeta;
173    }
174    bool useNonMeta; //P include non meta attributes
175protected:
176    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);
177    virtual int getNumOfElements(const TExample& example);
178};
179
180#endif
181
Note: See TracBrowser for help on using the repository browser.