SHOGUN
v2.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
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/labels/RegressionLabels.h
>
13
#include <
shogun/io/SGIO.h
>
14
15
using namespace
shogun;
16
17
CLibSVR::CLibSVR
()
18
:
CSVM
()
19
{
20
model
=NULL;
21
}
22
23
CLibSVR::CLibSVR
(
float64_t
C,
float64_t
eps,
CKernel
* k,
CLabels
* lab)
24
:
CSVM
()
25
{
26
model
=NULL;
27
28
set_C
(C,C);
29
set_tube_epsilon
(eps);
30
set_labels
(lab);
31
set_kernel
(k);
32
}
33
34
CLibSVR::~CLibSVR
()
35
{
36
SG_FREE
(
model
);
37
}
38
39
EMachineType
CLibSVR::get_classifier_type
()
40
{
41
return
CT_LIBSVR
;
42
}
43
44
bool
CLibSVR::train_machine
(
CFeatures
* data)
45
{
46
ASSERT
(
kernel
);
47
ASSERT
(
m_labels
&&
m_labels
->
get_num_labels
());
48
ASSERT
(
m_labels
->
get_label_type
() ==
LT_REGRESSION
);
49
50
if
(data)
51
{
52
if
(
m_labels
->
get_num_labels
() != data->
get_num_vectors
())
53
SG_ERROR
(
"Number of training vectors does not match number of labels\n"
);
54
kernel
->
init
(data, data);
55
}
56
57
SG_FREE
(
model
);
58
59
struct
svm_node* x_space;
60
61
problem
.l=
m_labels
->
get_num_labels
();
62
SG_INFO
(
"%d trainlabels\n"
,
problem
.l);
63
64
problem
.y=
SG_MALLOC
(
float64_t
,
problem
.l);
65
problem
.x=
SG_MALLOC
(
struct
svm_node*,
problem
.l);
66
x_space=
SG_MALLOC
(
struct
svm_node, 2*
problem
.l);
67
68
for
(int32_t i=0; i<
problem
.l; i++)
69
{
70
problem
.y[i]=((
CRegressionLabels
*)
m_labels
)->get_label(i);
71
problem
.x[i]=&x_space[2*i];
72
x_space[2*i].index=i;
73
x_space[2*i+1].index=-1;
74
}
75
76
int32_t weights_label[2]={-1,+1};
77
float64_t
weights[2]={1.0,
get_C2
()/
get_C1
()};
78
79
param
.svm_type=EPSILON_SVR;
// epsilon SVR
80
param
.kernel_type = LINEAR;
81
param
.degree = 3;
82
param
.gamma = 0;
// 1/k
83
param
.coef0 = 0;
84
param
.nu = 0.5;
85
param
.kernel=
kernel
;
86
param
.cache_size =
kernel
->
get_cache_size
();
87
param
.max_train_time =
m_max_train_time
;
88
param
.C =
get_C1
();
89
param
.eps =
epsilon
;
90
param
.p =
tube_epsilon
;
91
param
.shrinking = 1;
92
param
.nr_weight = 2;
93
param
.weight_label = weights_label;
94
param
.weight = weights;
95
param
.use_bias =
get_bias_enabled
();
96
97
const
char
* error_msg = svm_check_parameter(&
problem
,&
param
);
98
99
if
(error_msg)
100
SG_ERROR
(
"Error: %s\n"
,error_msg);
101
102
model
= svm_train(&
problem
, &
param
);
103
104
if
(
model
)
105
{
106
ASSERT
(
model
->nr_class==2);
107
ASSERT
((
model
->l==0) || (
model
->l>0 &&
model
->SV &&
model
->sv_coef &&
model
->sv_coef[0]));
108
109
int32_t num_sv=
model
->l;
110
111
create_new_model
(num_sv);
112
113
CSVM::set_objective
(
model
->objective);
114
115
set_bias
(-
model
->rho[0]);
116
117
for
(int32_t i=0; i<num_sv; i++)
118
{
119
set_support_vector
(i, (
model
->SV[i])->index);
120
set_alpha
(i,
model
->sv_coef[0][i]);
121
}
122
123
SG_FREE
(
problem
.x);
124
SG_FREE
(
problem
.y);
125
SG_FREE
(x_space);
126
127
svm_destroy_model(
model
);
128
model
=NULL;
129
return
true
;
130
}
131
else
132
return
false
;
133
}
SHOGUN
Machine Learning Toolbox - Documentation