#
source:
orange/source/orange/libsvm_interface.hpp
@
11607:8ecd4831def9

Revision 11607:8ecd4831def9, 6.1 KB checked in by Ales Erjavec <ales.erjavec@…>, 10 months ago (diff) |
---|

Rev | Line | |
---|---|---|

[8978] | 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 | ||

52 | svm_model *svm_load_model_alt(string& buffer); | |

53 | int svm_save_model_alt(string& buffer, const svm_model *model); | |

54 | ||

55 | WRAPPER(ExampleGenerator) | |

56 | WRAPPER(KernelFunc) | |

57 | WRAPPER(SVMLearner) | |

58 | WRAPPER(SVMClassifier) | |

59 | WRAPPER(ExampleTable) | |

60 | ||

61 | class ORANGE_API TKernelFunc: public TOrange{ | |

62 | public: | |

63 | __REGISTER_ABSTRACT_CLASS | |

64 | virtual float operator()(const TExample &, const TExample &)=0; | |

65 | }; | |

66 | ||

67 | WRAPPER(KernelFunc) | |

68 | ||

69 | ||

70 | class ORANGE_API TSVMLearner : public TLearner{ | |

71 | public: | |

72 | __REGISTER_CLASS | |

73 | ||

74 | CLASSCONSTANTS(SVMType: C_SVC=C_SVC; Nu_SVC=NU_SVC; OneClass=ONE_CLASS; Epsilon_SVR=EPSILON_SVR; Nu_SVR=NU_SVR) | |

75 | CLASSCONSTANTS(Kernel: Linear=LINEAR; Polynomial=POLY; RBF=RBF; Sigmoid=SIGMOID; Custom=PRECOMPUTED) | |

76 | CLASSCONSTANTS(LIBSVM_VERSION: VERSION=LIBSVM_VERSION) | |

77 | ||

78 | //parameters | |

79 | int svm_type; //P(&SVMLearner_SVMType) SVM type (C_SVC=0, NU_SVC, ONE_CLASS, EPSILON_SVR=3, NU_SVR=4) | |

80 | int kernel_type; //P(&SVMLearner_Kernel) kernel type (LINEAR=0, POLY, RBF, SIGMOID, CUSTOM=4) | |

81 | float degree; //P polynomial kernel degree | |

82 | float gamma; //P poly/rbf/sigm parameter | |

83 | float coef0; //P poly/sigm parameter | |

84 | float cache_size; //P cache size in MB | |

85 | float eps; //P stopping criteria | |

86 | float C; //P for C_SVC and C_SVR | |

87 | float nu; //P for NU_SVC and ONE_CLASS | |

88 | float p; //P for C_SVR | |

89 | int shrinking; //P shrinking | |

90 | int probability; //P probability | |

91 | bool verbose; //P verbose | |

92 | ||

93 | int nr_weight; /* for C_SVC */ | |

94 | int *weight_label; /* for C_SVC */ | |

95 | double* weight; /* for C_SVC */ | |

96 | ||

97 | PKernelFunc kernelFunc; //P custom kernel function | |

98 | ||

99 | TSVMLearner(); | |

100 | ~TSVMLearner(); | |

101 | ||

102 | PClassifier operator()(PExampleGenerator, const int & = 0); | |

103 | ||

104 | protected: | |

105 | virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0); | |

[9187] | 106 | virtual svm_node* init_problem(svm_problem &problem, PExampleTable examples, int n_elements); |

[8978] | 107 | virtual int getNumOfElements(PExampleGenerator examples); |

[11606] | 108 | virtual TSVMClassifier* createClassifier( |

[11607] | 109 | PDomain domain, svm_model* model, PExampleTable supportVectors, PExampleTable examples); |

[8978] | 110 | }; |

111 | ||

112 | class ORANGE_API TSVMLearnerSparse : public TSVMLearner{ | |

113 | public: | |

114 | __REGISTER_CLASS | |

115 | bool useNonMeta; //P include non meta attributes in the learning process | |

116 | protected: | |

117 | virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0); | |

118 | virtual int getNumOfElements(PExampleGenerator examples); | |

[11606] | 119 | virtual TSVMClassifier* createClassifier( |

[11607] | 120 | PDomain domain, svm_model* model, PExampleTable supportVectors, PExampleTable examples); |

[8978] | 121 | }; |

122 | ||

123 | ||

[11607] | 124 | class ORANGE_API TSVMClassifier : public TClassifierFD { |

[8978] | 125 | public: |

126 | __REGISTER_CLASS | |

[11607] | 127 | TSVMClassifier() { |

[8978] | 128 | this->model = NULL; |

129 | }; | |

130 | ||

[11607] | 131 | TSVMClassifier(PDomain, svm_model * model, PExampleTable supportVectors, |

132 | PKernelFunc kernelFunc=NULL, PExampleTable examples=NULL); | |

[11606] | 133 | |

[8978] | 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 | |

[11607] | 147 | |

148 | PExampleTable examples; //P training instances when svm_type == Custom | |

149 | PKernelFunc kernelFunc; //P custom kernel function used when svm_type == Custom | |

[8978] | 150 | |

[9021] | 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) | |

[8978] | 153 | |

154 | svm_model* getModel() {return model;} | |

155 | ||

156 | protected: | |

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 | ||

160 | private: | |

161 | svm_model *model; | |

162 | }; | |

163 | ||

[11607] | 164 | class ORANGE_API TSVMClassifierSparse : public TSVMClassifier { |

[8978] | 165 | public: |

166 | __REGISTER_CLASS | |

[11607] | 167 | TSVMClassifierSparse() {}; |

168 | ||

169 | TSVMClassifierSparse( | |

170 | PDomain domain, svm_model * model, bool useNonMeta, | |

171 | PExampleTable supportVectors, | |

172 | PKernelFunc kernelFunc=NULL, | |

173 | PExampleTable examples=NULL | |

174 | ) : TSVMClassifier(domain, model, supportVectors, kernelFunc, examples) { | |

[11606] | 175 | this->useNonMeta = useNonMeta; |

[8978] | 176 | } |

[11606] | 177 | |

[11607] | 178 | bool useNonMeta; //PR include non meta attributes |

[11606] | 179 | |

[8978] | 180 | protected: |

181 | virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0); | |

182 | virtual int getNumOfElements(const TExample& example); | |

183 | }; | |

184 | ||

185 | #endif | |

186 |

**Note:**See TracBrowser for help on using the repository browser.