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
machine
KernelMachine.h
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
#ifndef _KERNEL_MACHINE_H__
12
#define _KERNEL_MACHINE_H__
13
14
#include <
shogun/lib/common.h
>
15
#include <
shogun/io/SGIO.h
>
16
#include <
shogun/kernel/Kernel.h
>
17
#include <
shogun/features/Labels.h
>
18
#include <
shogun/machine/Machine.h
>
19
20
#include <stdio.h>
21
22
namespace
shogun
23
{
24
class
CMachine;
25
class
CLabels;
26
class
CKernel;
27
43
class
CKernelMachine
:
public
CMachine
44
{
45
public
:
47
CKernelMachine
();
48
50
virtual
~CKernelMachine
();
51
57
virtual
const
char
*
get_name
()
const
{
58
return
"KernelMachine"
; }
59
64
inline
void
set_kernel
(
CKernel
* k)
65
{
66
SG_UNREF
(
kernel
);
67
SG_REF
(k);
68
kernel
=k;
69
}
70
75
inline
CKernel
*
get_kernel
()
76
{
77
SG_REF
(
kernel
);
78
return
kernel
;
79
}
80
85
inline
void
set_batch_computation_enabled
(
bool
enable)
86
{
87
use_batch_computation
=enable;
88
}
89
94
inline
bool
get_batch_computation_enabled
()
95
{
96
return
use_batch_computation
;
97
}
98
103
inline
void
set_linadd_enabled
(
bool
enable)
104
{
105
use_linadd
=enable;
106
}
107
112
inline
bool
get_linadd_enabled
()
113
{
114
return
use_linadd
;
115
}
116
121
inline
void
set_bias_enabled
(
bool
enable_bias) {
use_bias
=enable_bias; }
122
127
inline
bool
get_bias_enabled
() {
return
use_bias
; }
128
133
inline
float64_t
get_bias
()
134
{
135
return
m_bias
;
136
}
137
142
inline
void
set_bias
(
float64_t
bias)
143
{
144
m_bias
=bias;
145
}
146
152
inline
int32_t
get_support_vector
(int32_t idx)
153
{
154
ASSERT
(
m_svs
.
vector
&& idx<
m_svs
.
vlen
);
155
return
m_svs
.
vector
[idx];
156
}
157
163
inline
float64_t
get_alpha
(int32_t idx)
164
{
165
if
(!
m_alpha
.
vector
)
166
SG_ERROR
(
"No alphas set\n"
);
167
if
(idx>=
m_alpha
.
vlen
)
168
SG_ERROR
(
"Alphas index (%d) out of range (%d)\n"
, idx,
m_svs
.
vlen
);
169
return
m_alpha
.
vector
[idx];
170
}
171
178
inline
bool
set_support_vector
(int32_t idx, int32_t val)
179
{
180
if
(
m_svs
.
vector
&& idx<
m_svs
.
vlen
)
181
m_svs
.
vector
[idx]=val;
182
else
183
return
false
;
184
185
return
true
;
186
}
187
194
inline
bool
set_alpha
(int32_t idx,
float64_t
val)
195
{
196
if
(
m_alpha
.
vector
&& idx<
m_alpha
.
vlen
)
197
m_alpha
.
vector
[idx]=val;
198
else
199
return
false
;
200
201
return
true
;
202
}
203
208
inline
int32_t
get_num_support_vectors
()
209
{
210
return
m_svs
.
vlen
;
211
}
212
217
void
set_alphas
(
SGVector<float64_t>
alphas)
218
{
219
m_alpha
= alphas;
220
}
221
226
void
set_support_vectors
(
SGVector<int32_t>
svs)
227
{
228
m_svs
= svs;
229
}
230
234
SGVector<int32_t>
get_support_vectors
()
235
{
236
int32_t nsv =
get_num_support_vectors
();
237
int32_t* svs = NULL;
238
239
if
(nsv>0)
240
{
241
svs =
SG_MALLOC
(int32_t, nsv);
242
for
(int32_t i=0; i<nsv; i++)
243
svs[i] =
get_support_vector
(i);
244
}
245
246
return
SGVector<int32_t>
(svs,nsv);
247
}
248
252
SGVector<float64_t>
get_alphas
()
253
{
254
int32_t nsv =
get_num_support_vectors
();
255
float64_t
* alphas = NULL;
256
257
if
(nsv>0)
258
{
259
alphas =
SG_MALLOC
(
float64_t
, nsv);
260
for
(int32_t i=0; i<nsv; i++)
261
alphas[i] =
get_alpha
(i);
262
}
263
264
return
SGVector<float64_t>
(alphas,nsv);
265
}
266
271
inline
bool
create_new_model
(int32_t num)
272
{
273
m_alpha
.
destroy_vector
();
274
m_svs
.
destroy_vector
();
275
276
m_bias
=0;
277
278
if
(num>0)
279
{
280
m_alpha
=
SGVector<float64_t>
(num);
281
m_svs
=
SGVector<int32_t>
(num);
282
return
(
m_alpha
.
vector
!=NULL && m_svs.
vector
!=NULL);
283
}
284
else
285
return
true
;
286
}
287
292
bool
init_kernel_optimization
();
293
298
virtual
CLabels
*
apply
();
299
305
virtual
CLabels
*
apply
(
CFeatures
* data);
306
312
virtual
float64_t
apply
(int32_t num);
313
319
static
void
*
apply_helper
(
void
* p);
320
321
protected
:
325
virtual
void
store_model_features
();
326
327
protected
:
329
CKernel
*
kernel
;
331
bool
use_batch_computation
;
333
bool
use_linadd
;
335
bool
use_bias
;
337
float64_t
m_bias
;
338
340
SGVector<float64_t>
m_alpha
;
341
343
SGVector<int32_t>
m_svs
;
344
};
345
}
346
#endif
/* _KERNEL_MACHINE_H__ */
SHOGUN
Machine Learning Toolbox - Documentation