/
BinaryNuSvmClassification.java
265 lines (230 loc) · 6.78 KB
/
BinaryNuSvmClassification.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright 2014 Simone Filice and Giuseppe Castellucci and Danilo Croce and Roberto Basili
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package it.uniroma2.sag.kelp.learningalgorithm.classification.libsvm;
import it.uniroma2.sag.kelp.data.dataset.Dataset;
import it.uniroma2.sag.kelp.data.label.Label;
import it.uniroma2.sag.kelp.kernel.Kernel;
import it.uniroma2.sag.kelp.learningalgorithm.KernelMethod;
import it.uniroma2.sag.kelp.learningalgorithm.classification.ClassificationLearningAlgorithm;
import it.uniroma2.sag.kelp.learningalgorithm.classification.libsvm.solver.LibNuSvmSolver;
import it.uniroma2.sag.kelp.learningalgorithm.classification.libsvm.solver.SvmSolution;
import it.uniroma2.sag.kelp.predictionfunction.PredictionFunction;
import it.uniroma2.sag.kelp.predictionfunction.classifier.BinaryKernelMachineClassifier;
import it.uniroma2.sag.kelp.predictionfunction.classifier.Classifier;
import it.uniroma2.sag.kelp.predictionfunction.model.BinaryKernelMachineModel;
import com.fasterxml.jackson.annotation.JsonTypeName;
/**
* It implements the \(\nu\)-SVM learning algorithm discussed in [CC Chang & CJ
* Lin, 2011]. It is a learning algorithm for binary linear classification and
* it relies on kernel functions.
* <p>
* It is a Java porting of the library LIBSVM v3.17, written in C++.
* <p>
* Further details can be found in:
* <p>
* [CC Chang & CJ Lin, 2011] Chih-Chung Chang and Chih-Jen Lin. LIBSVM: A
* library for support vector machines. ACM Transactions on Intelligent Systems
* and Technology, 2:27:1-27:27, 2011.
* <p>
* and
* <p>
* <code>http://www.csie.ntu.edu.tw/~cjlin/libsvm/</code>
*
* @author Danilo Croce
*/
@JsonTypeName("binaryNuSvmClassification")
public class BinaryNuSvmClassification extends LibNuSvmSolver implements
ClassificationLearningAlgorithm, KernelMethod {
/**
* The \(\nu\) parameter
*/
private float nu = 0.5f;
/**
* The label to be learned
*/
private Label label;
/**
* The classifier to be returned
*/
private BinaryKernelMachineClassifier classifier;
public BinaryNuSvmClassification() {
super();
initializeClassifier();
}
/**
* @param kernel
* The kernel function
* @param label
* The label to be learned
* @param nu
* The \(\nu\) parameter
*/
public BinaryNuSvmClassification(Kernel kernel, Label label, float nu) {
super(kernel, 1, 1);
this.label = label;
this.nu = checkNu(nu);
initializeClassifier();
this.setLabel(label);
}
/**
* Check that 0<=\(\nu\)<=1
*
* @param nu
* @return True if \(\nu\) is valid. False otherwise
*/
private float checkNu(float nu) {
if (nu <= 0 || nu >= 1) {
System.err
.println("Nu must be in the (0,1) interval. Nu is set to 0.5");
return 0.5f;
}
return nu;
}
/*
* (non-Javadoc)
*
* @see it.uniroma2.sag.kelp.learningalgorithm.LearningAlgorithm#duplicate()
*/
@Override
public BinaryNuSvmClassification duplicate() {
return new BinaryNuSvmClassification(kernel, label, nu);
}
/**
* @return The \(\nu\) parameter
*/
public float getNu() {
return nu;
}
/*
* (non-Javadoc)
*
* @see it.uniroma2.sag.kelp.learningalgorithm.LearningAlgorithm#
* getPredictionFunction()
*/
@Override
public BinaryKernelMachineClassifier getPredictionFunction() {
return this.classifier;
}
/**
* Initialize the classifier
*/
private void initializeClassifier() {
BinaryKernelMachineModel model = new BinaryKernelMachineModel();
this.classifier = new BinaryKernelMachineClassifier();
this.classifier.setModel(model);
}
/*
* (non-Javadoc)
*
* @see
* it.uniroma2.sag.kelp.learningalgorithm.LearningAlgorithm#learn(it.uniroma2
* .sag.kelp.data.dataset.Dataset)
*/
public void learn(Dataset trainingSet) {
int l = trainingSet.getNumberOfExamples();
/*
* CHECK
*/
int[] y = new int[l];
for (int i = 0; i < y.length; i++) {
if (trainingSet.getExamples().get(i).isExampleOf(this.label))
y[i] = +1;
else
y[i] = -1;
}
float sum_pos = nu * (float) l / 2f;
float sum_neg = nu * (float) l / 2f;
float[] initialAlpha = new float[l];
for (int i = 0; i < l; i++)
if (y[i] == +1) {
initialAlpha[i] = Math.min(1.0f, sum_pos);
sum_pos -= initialAlpha[i];
} else {
initialAlpha[i] = Math.min(1.0f, sum_neg);
sum_neg -= initialAlpha[i];
}
classifier.getModel().setKernel(kernel);
learn(trainingSet, initialAlpha);
}
/**
* It starts the training process exploiting the provided
* <code>dataset</code> and initial values of the Support Vector weights
*
* @param trainingSet
* the initial dataset
* @param initialAlpha
* initial values of the Support Vector weights
* @return the classifier
*/
private Classifier learn(Dataset trainingSet, float[] initialAlpha) {
l = trainingSet.getNumberOfExamples();
float[] zeros = new float[l];
for (int i = 0; i < l; i++)
zeros[i] = 0;
int[] y = new int[trainingSet.getNumberOfExamples()];
for (int i = 0; i < y.length; i++) {
if (trainingSet.getExamples().get(i).isExampleOf(this.label))
y[i] = +1;
else
y[i] = -1;
}
SvmSolution solution = solve(trainingSet.getNumberOfExamples(),
trainingSet, zeros, y, initialAlpha);
float r = calculate_r();
float[] alphas = solution.getAlphas();
for (int i = 0; i < trainingSet.getNumberOfExamples(); i++) {
if (alphas[i] != 0) {
this.classifier.getModel().addExample(y[i] * alphas[i] / r,
trainingSet.getExamples().get(i));
}
}
this.classifier.getModel().setBias(-solution.getRho() / r);
info("C = " + 1 / r);
info("obj = " + r * r);
info("rho = " + -solution.getRho() / r);
return this.classifier;
}
/*
* (non-Javadoc)
*
* @see it.uniroma2.sag.kelp.learningalgorithm.LearningAlgorithm#reset()
*/
@Override
public void reset() {
this.classifier.reset();
}
/**
* @param nu
* The \(\nu\) parameter
*/
public void setNu(float nu) {
this.nu = checkNu(nu);
}
/*
* (non-Javadoc)
*
* @see
* it.uniroma2.sag.kelp.learningalgorithm.KernelMethod#setKernel(it.uniroma2
* .sag.kelp.kernel.Kernel)
*/
@Override
public void setKernel(Kernel kernel) {
this.kernel = kernel;
}
@Override
public void setPredictionFunction(PredictionFunction predictionFunction) {
this.classifier = (BinaryKernelMachineClassifier) predictionFunction;
}
}