SHOGUN
v1.1.0
Main Page
Related Pages
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
src
shogun
regression
svr
LibSVR.cpp
Go to the documentation of this file.
1
/*
2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 3 of the License, or
5
* (at your option) any later version.
6
*
7
* Written (W) 1999-2009 Soeren Sonnenburg
8
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9
*/
10
11
#include <
shogun/regression/svr/LibSVR.h
>
12
#include <
shogun/io/SGIO.h
>
13
14
using namespace
shogun;
15
16
CLibSVR::CLibSVR
()
17
:
CSVM
()
18
{
19
model
=NULL;
20
}
21
22
CLibSVR::CLibSVR
(
float64_t
C,
float64_t
eps,
CKernel
* k,
CLabels
* lab)
23
:
CSVM
()
24
{
25
model
=NULL;
26
27
set_C
(C,C);
28
set_tube_epsilon
(eps);
29
set_labels
(lab);
30
set_kernel
(k);
31
}
32
33
CLibSVR::~CLibSVR
()
34
{
35
SG_FREE
(
model
);
36
}
37
38
EClassifierType
CLibSVR::get_classifier_type
()
39
{
40
return
CT_LIBSVR
;
41
}
42
43
bool
CLibSVR::train_machine
(
CFeatures
* data)
44
{
45
ASSERT
(
kernel
);
46
ASSERT
(
labels
&&
labels
->
get_num_labels
());
47
48
if
(data)
49
{
50
if
(
labels
->
get_num_labels
() != data->
get_num_vectors
())
51
SG_ERROR
(
"Number of training vectors does not match number of labels\n"
);
52
kernel
->
init
(data, data);
53
}
54
55
SG_FREE
(
model
);
56
57
struct
svm_node* x_space;
58
59
problem
.l=
labels
->
get_num_labels
();
60
SG_INFO
(
"%d trainlabels\n"
,
problem
.l);
61
62
problem
.y=
SG_MALLOC
(
float64_t
,
problem
.l);
63
problem
.x=
SG_MALLOC
(
struct
svm_node*,
problem
.l);
64
x_space=
SG_MALLOC
(
struct
svm_node, 2*
problem
.l);
65
66
for
(int32_t i=0; i<
problem
.l; i++)
67
{
68
problem
.y[i]=
labels
->
get_label
(i);
69
problem
.x[i]=&x_space[2*i];
70
x_space[2*i].index=i;
71
x_space[2*i+1].index=-1;
72
}
73
74
int32_t weights_label[2]={-1,+1};
75
float64_t
weights[2]={1.0,
get_C2
()/
get_C1
()};
76
77
param
.svm_type=EPSILON_SVR;
// epsilon SVR
78
param
.kernel_type = LINEAR;
79
param
.degree = 3;
80
param
.gamma = 0;
// 1/k
81
param
.coef0 = 0;
82
param
.nu = 0.5;
83
param
.kernel=
kernel
;
84
param
.cache_size =
kernel
->
get_cache_size
();
85
param
.max_train_time =
max_train_time
;
86
param
.C =
get_C1
();
87
param
.eps =
epsilon
;
88
param
.p =
tube_epsilon
;
89
param
.shrinking = 1;
90
param
.nr_weight = 2;
91
param
.weight_label = weights_label;
92
param
.weight = weights;
93
param
.use_bias =
get_bias_enabled
();
94
95
const
char
* error_msg = svm_check_parameter(&
problem
,&
param
);
96
97
if
(error_msg)
98
SG_ERROR
(
"Error: %s\n"
,error_msg);
99
100
model
= svm_train(&
problem
, &
param
);
101
102
if
(
model
)
103
{
104
ASSERT
(
model
->nr_class==2);
105
ASSERT
((
model
->l==0) || (
model
->l>0 &&
model
->SV &&
model
->sv_coef &&
model
->sv_coef[0]));
106
107
int32_t num_sv=
model
->l;
108
109
create_new_model
(num_sv);
110
111
CSVM::set_objective
(
model
->objective);
112
113
set_bias
(-
model
->rho[0]);
114
115
for
(int32_t i=0; i<num_sv; i++)
116
{
117
set_support_vector
(i, (
model
->SV[i])->index);
118
set_alpha
(i,
model
->sv_coef[0][i]);
119
}
120
121
SG_FREE
(
problem
.x);
122
SG_FREE
(
problem
.y);
123
SG_FREE
(x_space);
124
125
svm_destroy_model(
model
);
126
model
=NULL;
127
return
true
;
128
}
129
else
130
return
false
;
131
}
SHOGUN
Machine Learning Toolbox - Documentation