1 | /*
|
---|
2 | * SVM.NET Library
|
---|
3 | * Copyright (C) 2008 Matthew Johnson
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | using System;
|
---|
21 | using System.Collections.Generic;
|
---|
22 |
|
---|
23 | namespace SVM {
|
---|
24 | /// <summary>
|
---|
25 | /// Contains all of the types of SVM this library can model.
|
---|
26 | /// </summary>
|
---|
27 | public enum SvmType {
|
---|
28 | /// <summary>
|
---|
29 | /// C-SVC.
|
---|
30 | /// </summary>
|
---|
31 | C_SVC,
|
---|
32 | /// <summary>
|
---|
33 | /// nu-SVC.
|
---|
34 | /// </summary>
|
---|
35 | NU_SVC,
|
---|
36 | /// <summary>
|
---|
37 | /// one-class SVM
|
---|
38 | /// </summary>
|
---|
39 | ONE_CLASS,
|
---|
40 | /// <summary>
|
---|
41 | /// epsilon-SVR
|
---|
42 | /// </summary>
|
---|
43 | EPSILON_SVR,
|
---|
44 | /// <summary>
|
---|
45 | /// nu-SVR
|
---|
46 | /// </summary>
|
---|
47 | NU_SVR
|
---|
48 | };
|
---|
49 | /// <summary>
|
---|
50 | /// Contains the various kernel types this library can use.
|
---|
51 | /// </summary>
|
---|
52 | public enum KernelType {
|
---|
53 | /// <summary>
|
---|
54 | /// Linear: u'*v
|
---|
55 | /// </summary>
|
---|
56 | LINEAR,
|
---|
57 | /// <summary>
|
---|
58 | /// Polynomial: (gamma*u'*v + coef0)^degree
|
---|
59 | /// </summary>
|
---|
60 | POLY,
|
---|
61 | /// <summary>
|
---|
62 | /// Radial basis function: exp(-gamma*|u-v|^2)
|
---|
63 | /// </summary>
|
---|
64 | RBF,
|
---|
65 | /// <summary>
|
---|
66 | /// Sigmoid: tanh(gamma*u'*v + coef0)
|
---|
67 | /// </summary>
|
---|
68 | SIGMOID,
|
---|
69 | /// <summary>
|
---|
70 | /// Precomputed kernel
|
---|
71 | /// </summary>
|
---|
72 | PRECOMPUTED,
|
---|
73 | };
|
---|
74 |
|
---|
75 | /// <summary>
|
---|
76 | /// This class contains the various parameters which can affect the way in which an SVM
|
---|
77 | /// is learned. Unless you know what you are doing, chances are you are best off using
|
---|
78 | /// the default values.
|
---|
79 | /// </summary>
|
---|
80 | [Serializable]
|
---|
81 | public class Parameter : ICloneable {
|
---|
82 | private SvmType _svmType;
|
---|
83 | private KernelType _kernelType;
|
---|
84 | private int _degree;
|
---|
85 | private double _gamma;
|
---|
86 | private double _coef0;
|
---|
87 |
|
---|
88 | private double _cacheSize;
|
---|
89 | private double _C;
|
---|
90 | private double _eps;
|
---|
91 |
|
---|
92 | private Dictionary<int, double> _weights;
|
---|
93 | private double _nu;
|
---|
94 | private double _p;
|
---|
95 | private bool _shrinking;
|
---|
96 | private bool _probability;
|
---|
97 |
|
---|
98 | /// <summary>
|
---|
99 | /// Default Constructor. Gives good default values to all parameters.
|
---|
100 | /// </summary>
|
---|
101 | public Parameter() {
|
---|
102 | _svmType = SvmType.C_SVC;
|
---|
103 | _kernelType = KernelType.RBF;
|
---|
104 | _degree = 3;
|
---|
105 | _gamma = 0; // 1/k
|
---|
106 | _coef0 = 0;
|
---|
107 | _nu = 0.5;
|
---|
108 | _cacheSize = 40;
|
---|
109 | _C = 1;
|
---|
110 | _eps = 1e-3;
|
---|
111 | _p = 0.1;
|
---|
112 | _shrinking = true;
|
---|
113 | _probability = false;
|
---|
114 | _weights = new Dictionary<int, double>();
|
---|
115 | }
|
---|
116 |
|
---|
117 | /// <summary>
|
---|
118 | /// Type of SVM (default C-SVC)
|
---|
119 | /// </summary>
|
---|
120 | public SvmType SvmType {
|
---|
121 | get {
|
---|
122 | return _svmType;
|
---|
123 | }
|
---|
124 | set {
|
---|
125 | _svmType = value;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | /// <summary>
|
---|
129 | /// Type of kernel function (default Polynomial)
|
---|
130 | /// </summary>
|
---|
131 | public KernelType KernelType {
|
---|
132 | get {
|
---|
133 | return _kernelType;
|
---|
134 | }
|
---|
135 | set {
|
---|
136 | _kernelType = value;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | /// <summary>
|
---|
140 | /// Degree in kernel function (default 3).
|
---|
141 | /// </summary>
|
---|
142 | public int Degree {
|
---|
143 | get {
|
---|
144 | return _degree;
|
---|
145 | }
|
---|
146 | set {
|
---|
147 | _degree = value;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | /// <summary>
|
---|
151 | /// Gamma in kernel function (default 1/k)
|
---|
152 | /// </summary>
|
---|
153 | public double Gamma {
|
---|
154 | get {
|
---|
155 | return _gamma;
|
---|
156 | }
|
---|
157 | set {
|
---|
158 | _gamma = value;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | /// <summary>
|
---|
162 | /// Zeroeth coefficient in kernel function (default 0)
|
---|
163 | /// </summary>
|
---|
164 | public double Coefficient0 {
|
---|
165 | get {
|
---|
166 | return _coef0;
|
---|
167 | }
|
---|
168 | set {
|
---|
169 | _coef0 = value;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | /// <summary>
|
---|
174 | /// Cache memory size in MB (default 100)
|
---|
175 | /// </summary>
|
---|
176 | public double CacheSize {
|
---|
177 | get {
|
---|
178 | return _cacheSize;
|
---|
179 | }
|
---|
180 | set {
|
---|
181 | _cacheSize = value;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | /// <summary>
|
---|
185 | /// Tolerance of termination criterion (default 0.001)
|
---|
186 | /// </summary>
|
---|
187 | public double EPS {
|
---|
188 | get {
|
---|
189 | return _eps;
|
---|
190 | }
|
---|
191 | set {
|
---|
192 | _eps = value;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | /// <summary>
|
---|
196 | /// The parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
|
---|
197 | /// </summary>
|
---|
198 | public double C {
|
---|
199 | get {
|
---|
200 | return _C;
|
---|
201 | }
|
---|
202 | set {
|
---|
203 | _C = value;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | /// <summary>
|
---|
208 | /// Contains custom weights for class labels. Default weight value is 1.
|
---|
209 | /// </summary>
|
---|
210 | public Dictionary<int, double> Weights {
|
---|
211 | get {
|
---|
212 | return _weights;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | /// <summary>
|
---|
217 | /// The parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
|
---|
218 | /// </summary>
|
---|
219 | public double Nu {
|
---|
220 | get {
|
---|
221 | return _nu;
|
---|
222 | }
|
---|
223 | set {
|
---|
224 | _nu = value;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | /// <summary>
|
---|
228 | /// The epsilon in loss function of epsilon-SVR (default 0.1)
|
---|
229 | /// </summary>
|
---|
230 | public double P {
|
---|
231 | get {
|
---|
232 | return _p;
|
---|
233 | }
|
---|
234 | set {
|
---|
235 | _p = value;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | /// <summary>
|
---|
239 | /// Whether to use the shrinking heuristics, (default True)
|
---|
240 | /// </summary>
|
---|
241 | public bool Shrinking {
|
---|
242 | get {
|
---|
243 | return _shrinking;
|
---|
244 | }
|
---|
245 | set {
|
---|
246 | _shrinking = value;
|
---|
247 | }
|
---|
248 | }
|
---|
249 | /// <summary>
|
---|
250 | /// Whether to train an SVC or SVR model for probability estimates, (default False)
|
---|
251 | /// </summary>
|
---|
252 | public bool Probability {
|
---|
253 | get {
|
---|
254 | return _probability;
|
---|
255 | }
|
---|
256 | set {
|
---|
257 | _probability = value;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | #region ICloneable Members
|
---|
263 | /// <summary>
|
---|
264 | /// Creates a memberwise clone of this parameters object.
|
---|
265 | /// </summary>
|
---|
266 | /// <returns>The clone (as type Parameter)</returns>
|
---|
267 | public object Clone() {
|
---|
268 | return base.MemberwiseClone();
|
---|
269 | }
|
---|
270 |
|
---|
271 | #endregion
|
---|
272 | }
|
---|
273 | } |
---|