-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
LibSVM.cpp
179 lines (142 loc) · 3.9 KB
/
LibSVM.cpp
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
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Heiko Strathmann, Leon Kuchenbecker,
* Sergey Lisitsyn
*/
#include <shogun/classifier/svm/LibSVM.h>
#include <shogun/io/SGIO.h>
#include <shogun/labels/BinaryLabels.h>
using namespace shogun;
CLibSVM::CLibSVM()
: CSVM(), solver_type(LIBSVM_C_SVC)
{
register_params();
}
CLibSVM::CLibSVM(LIBSVM_SOLVER_TYPE st)
: CSVM(), solver_type(st)
{
register_params();
}
CLibSVM::CLibSVM(float64_t C, CKernel* k, CLabels* lab, LIBSVM_SOLVER_TYPE st)
: CSVM(C, k, lab), solver_type(st)
{
register_params();
}
CLibSVM::~CLibSVM()
{
}
void CLibSVM::register_params()
{
SG_ADD((machine_int_t*) &solver_type, "libsvm_solver_type", "LibSVM Solver type", MS_NOT_AVAILABLE);
}
bool CLibSVM::train_machine(CFeatures* data)
{
svm_problem problem;
svm_parameter param;
struct svm_model* model = nullptr;
struct svm_node* x_space;
ASSERT(m_labels && m_labels->get_num_labels())
ASSERT(m_labels->get_label_type() == LT_BINARY)
if (data)
{
if (m_labels->get_num_labels() != data->get_num_vectors())
{
SG_ERROR("%s::train_machine(): Number of training vectors (%d) does"
" not match number of labels (%d)\n", get_name(),
data->get_num_vectors(), m_labels->get_num_labels());
}
kernel->init(data, data);
}
REQUIRE(kernel->get_num_vec_lhs()==m_labels->get_num_labels(),
"Number of training data (%d) must match number of labels (%d)\n",
kernel->get_num_vec_lhs(), m_labels->get_num_labels())
problem.l=m_labels->get_num_labels();
SG_INFO("%d trainlabels\n", problem.l)
// set linear term
if (m_linear_term.vlen>0)
{
if (m_labels->get_num_labels()!=m_linear_term.vlen)
SG_ERROR("Number of training vectors does not match length of linear term\n")
// set with linear term from base class
problem.pv = get_linear_term_array();
}
else
{
// fill with minus ones
problem.pv = SG_MALLOC(float64_t, problem.l);
for (int i=0; i!=problem.l; i++)
problem.pv[i] = -1.0;
}
problem.y=SG_MALLOC(float64_t, problem.l);
problem.x=SG_MALLOC(struct svm_node*, problem.l);
problem.C=SG_MALLOC(float64_t, problem.l);
x_space=SG_MALLOC(struct svm_node, 2*problem.l);
for (int32_t i=0; i<problem.l; i++)
{
problem.y[i]=((CBinaryLabels*) m_labels)->get_label(i);
problem.x[i]=&x_space[2*i];
x_space[2*i].index=i;
x_space[2*i+1].index=-1;
}
int32_t weights_label[2]={-1,+1};
float64_t weights[2]={1.0,get_C2()/get_C1()};
ASSERT(kernel && kernel->has_features())
switch (solver_type)
{
case LIBSVM_C_SVC:
param.svm_type=C_SVC;
break;
case LIBSVM_NU_SVC:
param.svm_type=NU_SVC;
break;
default:
SG_ERROR("%s::train_machine(): Unknown solver type!\n", get_name());
break;
}
param.kernel_type = LINEAR;
param.degree = 3;
param.gamma = 0; // 1/k
param.coef0 = 0;
param.nu = get_nu();
param.kernel=kernel;
param.cache_size = kernel->get_cache_size();
param.max_train_time = m_max_train_time;
param.C = get_C1();
param.eps = epsilon;
param.p = 0.1;
param.shrinking = 1;
param.nr_weight = 2;
param.weight_label = weights_label;
param.weight = weights;
param.use_bias = get_bias_enabled();
const char* error_msg = svm_check_parameter(&problem, ¶m);
if(error_msg)
SG_ERROR("Error: %s\n",error_msg)
model = svm_train(&problem, ¶m);
if (model)
{
ASSERT(model->nr_class==2)
ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef[0]))
int32_t num_sv=model->l;
create_new_model(num_sv);
CSVM::set_objective(model->objective);
float64_t sgn=model->label[0];
set_bias(-sgn*model->rho[0]);
for (int32_t i=0; i<num_sv; i++)
{
set_support_vector(i, (model->SV[i])->index);
set_alpha(i, sgn*model->sv_coef[0][i]);
}
SG_FREE(problem.x);
SG_FREE(problem.y);
SG_FREE(problem.pv);
SG_FREE(problem.C);
SG_FREE(x_space);
svm_destroy_model(model);
model=NULL;
return true;
}
else
return false;
}