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
kernel
Kernel.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
* Written (W) 1999-2008 Gunnar Raetsch
9
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10
*/
11
12
#include <
shogun/lib/config.h
>
13
#include <
shogun/lib/common.h
>
14
#include <
shogun/io/SGIO.h
>
15
#include <
shogun/io/File.h
>
16
#include <
shogun/lib/Time.h
>
17
#include <
shogun/lib/Signal.h
>
18
19
#include <
shogun/base/Parallel.h
>
20
21
#include <
shogun/kernel/Kernel.h
>
22
#include <
shogun/kernel/IdentityKernelNormalizer.h
>
23
#include <
shogun/features/Features.h
>
24
#include <
shogun/base/Parameter.h
>
25
26
#include <
shogun/classifier/svm/SVM.h
>
27
28
#include <string.h>
29
#include <unistd.h>
30
#include <math.h>
31
32
#ifdef HAVE_PTHREAD
33
#include <pthread.h>
34
#endif
35
36
using namespace
shogun;
37
38
CKernel::CKernel
() :
CSGObject
()
39
{
40
init
();
41
register_params
();
42
}
43
44
CKernel::CKernel
(int32_t size) :
CSGObject
()
45
{
46
init
();
47
48
if
(size<10)
49
size=10;
50
51
cache_size
=size;
52
register_params
();
53
}
54
55
56
CKernel::CKernel
(
CFeatures
* p_lhs,
CFeatures
* p_rhs, int32_t size) :
CSGObject
()
57
{
58
init
();
59
60
if
(size<10)
61
size=10;
62
63
cache_size
=size;
64
65
set_normalizer
(
new
CIdentityKernelNormalizer
());
66
init
(p_lhs, p_rhs);
67
register_params
();
68
}
69
70
CKernel::~CKernel
()
71
{
72
if
(
get_is_initialized
())
73
SG_ERROR
(
"Kernel still initialized on destruction.\n"
);
74
75
remove_lhs_and_rhs
();
76
SG_UNREF
(
normalizer
);
77
78
SG_INFO
(
"Kernel deleted (%p).\n"
,
this
);
79
}
80
81
82
83
bool
CKernel::init(
CFeatures
* l,
CFeatures
* r)
84
{
85
//make sure features were indeed supplied
86
ASSERT
(l);
87
ASSERT
(r);
88
89
//make sure features are compatible
90
ASSERT
(l->
get_feature_class
()==r->
get_feature_class
());
91
ASSERT
(l->
get_feature_type
()==r->
get_feature_type
());
92
93
//remove references to previous features
94
remove_lhs_and_rhs
();
95
96
//increase reference counts
97
SG_REF
(l);
98
if
(l==r)
99
lhs_equals_rhs
=
true
;
100
else
// l!=r
101
SG_REF
(r);
102
103
lhs
=l;
104
rhs
=r;
105
106
ASSERT
(!
num_lhs
||
num_lhs
==l->
get_num_vectors
());
107
ASSERT
(!
num_rhs
||
num_rhs
==l->
get_num_vectors
());
108
109
num_lhs
=l->
get_num_vectors
();
110
num_rhs
=r->
get_num_vectors
();
111
112
return
true
;
113
}
114
115
bool
CKernel::set_normalizer
(
CKernelNormalizer
* n)
116
{
117
SG_REF
(n);
118
if
(
lhs
&&
rhs
)
119
n->
init
(
this
);
120
121
SG_UNREF
(
normalizer
);
122
normalizer
=n;
123
124
return
(
normalizer
!=NULL);
125
}
126
127
CKernelNormalizer
*
CKernel::get_normalizer
()
128
{
129
SG_REF
(
normalizer
)
130
return
normalizer
;
131
}
132
133
bool
CKernel::init_normalizer
()
134
{
135
return
normalizer
->
init
(
this
);
136
}
137
138
void
CKernel::cleanup
()
139
{
140
remove_lhs_and_rhs
();
141
}
142
143
144
145
void
CKernel::load
(
CFile
* loader)
146
{
147
SG_SET_LOCALE_C
;
148
SG_RESET_LOCALE
;
149
}
150
151
void
CKernel::save
(
CFile
* writer)
152
{
153
SGMatrix<float64_t>
k_matrix=get_kernel_matrix<float64_t>();
154
SG_SET_LOCALE_C
;
155
writer->
set_matrix
(k_matrix.
matrix
, k_matrix.
num_rows
, k_matrix.
num_cols
);
156
SG_FREE
(k_matrix.
matrix
);
157
SG_RESET_LOCALE
;
158
}
159
160
void
CKernel::remove_lhs_and_rhs
()
161
{
162
if
(
rhs
!=
lhs
)
163
SG_UNREF
(
rhs
);
164
rhs
= NULL;
165
num_rhs
=0;
166
167
SG_UNREF
(
lhs
);
168
lhs
= NULL;
169
num_lhs
=0;
170
lhs_equals_rhs
=
false
;
171
172
173
}
174
175
void
CKernel::remove_lhs
()
176
{
177
if
(
rhs
==
lhs
)
178
rhs
=NULL;
179
SG_UNREF
(
lhs
);
180
lhs
= NULL;
181
num_lhs
=0;
182
lhs_equals_rhs
=
false
;
183
184
}
185
187
void
CKernel::remove_rhs
()
188
{
189
if
(
rhs
!=
lhs
)
190
SG_UNREF
(
rhs
);
191
rhs
= NULL;
192
num_rhs
=0;
193
lhs_equals_rhs
=
false
;
194
195
196
}
197
198
#define ENUM_CASE(n) case n: SG_INFO(#n " "); break;
199
200
void
CKernel::list_kernel
()
201
{
202
SG_INFO
(
"%p - \"%s\" weight=%1.2f OPT:%s"
,
this
,
get_name
(),
203
get_combined_kernel_weight
(),
204
get_optimization_type
()==
FASTBUTMEMHUNGRY
?
"FASTBUTMEMHUNGRY"
:
205
"SLOWBUTMEMEFFICIENT"
);
206
207
switch
(
get_kernel_type
())
208
{
209
ENUM_CASE
(
K_UNKNOWN
)
210
ENUM_CASE
(
K_LINEAR
)
211
ENUM_CASE
(
K_POLY
)
212
ENUM_CASE
(
K_GAUSSIAN
)
213
ENUM_CASE
(
K_GAUSSIANSHIFT
)
214
ENUM_CASE
(
K_GAUSSIANMATCH
)
215
ENUM_CASE
(
K_HISTOGRAM
)
216
ENUM_CASE
(
K_SALZBERG
)
217
ENUM_CASE
(
K_LOCALITYIMPROVED
)
218
ENUM_CASE
(
K_SIMPLELOCALITYIMPROVED
)
219
ENUM_CASE
(
K_FIXEDDEGREE
)
220
ENUM_CASE
(
K_WEIGHTEDDEGREE
)
221
ENUM_CASE
(
K_WEIGHTEDDEGREEPOS
)
222
ENUM_CASE
(
K_WEIGHTEDDEGREERBF
)
223
ENUM_CASE
(
K_WEIGHTEDCOMMWORDSTRING
)
224
ENUM_CASE
(
K_POLYMATCH
)
225
ENUM_CASE
(
K_ALIGNMENT
)
226
ENUM_CASE
(
K_COMMWORDSTRING
)
227
ENUM_CASE
(
K_COMMULONGSTRING
)
228
ENUM_CASE
(
K_SPECTRUMRBF
)
229
ENUM_CASE
(
K_COMBINED
)
230
ENUM_CASE
(
K_AUC
)
231
ENUM_CASE
(
K_CUSTOM
)
232
ENUM_CASE
(
K_SIGMOID
)
233
ENUM_CASE
(
K_CHI2
)
234
ENUM_CASE
(
K_DIAG
)
235
ENUM_CASE
(
K_CONST
)
236
ENUM_CASE
(
K_DISTANCE
)
237
ENUM_CASE
(
K_LOCALALIGNMENT
)
238
ENUM_CASE
(
K_PYRAMIDCHI2
)
239
ENUM_CASE
(
K_OLIGO
)
240
ENUM_CASE
(
K_MATCHWORD
)
241
ENUM_CASE
(
K_TPPK
)
242
ENUM_CASE
(
K_REGULATORYMODULES
)
243
ENUM_CASE
(
K_SPARSESPATIALSAMPLE
)
244
ENUM_CASE
(
K_HISTOGRAMINTERSECTION
)
245
ENUM_CASE
(
K_WAVELET
)
246
ENUM_CASE
(
K_WAVE
)
247
ENUM_CASE
(
K_CAUCHY
)
248
ENUM_CASE
(
K_TSTUDENT
)
249
ENUM_CASE
(
K_MULTIQUADRIC
)
250
ENUM_CASE
(
K_EXPONENTIAL
)
251
ENUM_CASE
(
K_RATIONAL_QUADRATIC
)
252
ENUM_CASE
(
K_POWER
)
253
ENUM_CASE
(
K_SPHERICAL
)
254
ENUM_CASE
(
K_LOG
)
255
ENUM_CASE
(
K_SPLINE
)
256
ENUM_CASE
(
K_ANOVA
)
257
ENUM_CASE
(
K_CIRCULAR
)
258
ENUM_CASE
(
K_INVERSEMULTIQUADRIC
)
259
ENUM_CASE
(
K_SPECTRUMMISMATCHRBF
)
260
ENUM_CASE
(
K_DISTANTSEGMENTS
)
261
ENUM_CASE
(
K_BESSEL
)
262
}
263
264
switch
(
get_feature_class
())
265
{
266
ENUM_CASE
(
C_UNKNOWN
)
267
ENUM_CASE
(
C_SIMPLE
)
268
ENUM_CASE
(
C_SPARSE
)
269
ENUM_CASE
(
C_STRING
)
270
ENUM_CASE
(
C_STREAMING_SIMPLE
)
271
ENUM_CASE
(
C_STREAMING_SPARSE
)
272
ENUM_CASE
(
C_STREAMING_STRING
)
273
ENUM_CASE
(
C_STREAMING_VW
)
274
ENUM_CASE
(
C_COMBINED
)
275
ENUM_CASE
(
C_COMBINED_DOT
)
276
ENUM_CASE
(
C_WD
)
277
ENUM_CASE
(
C_SPEC
)
278
ENUM_CASE
(
C_WEIGHTEDSPEC
)
279
ENUM_CASE
(
C_POLY
)
280
ENUM_CASE
(
C_ANY
)
281
}
282
283
switch
(
get_feature_type
())
284
{
285
ENUM_CASE
(
F_UNKNOWN
)
286
ENUM_CASE
(
F_BOOL
)
287
ENUM_CASE
(
F_CHAR
)
288
ENUM_CASE
(
F_BYTE
)
289
ENUM_CASE
(
F_SHORT
)
290
ENUM_CASE
(
F_WORD
)
291
ENUM_CASE
(
F_INT
)
292
ENUM_CASE
(
F_UINT
)
293
ENUM_CASE
(
F_LONG
)
294
ENUM_CASE
(
F_ULONG
)
295
ENUM_CASE
(
F_SHORTREAL
)
296
ENUM_CASE
(
F_DREAL
)
297
ENUM_CASE
(
F_LONGREAL
)
298
ENUM_CASE
(
F_ANY
)
299
}
300
SG_INFO
(
"\n"
);
301
}
302
#undef ENUM_CASE
303
304
bool
CKernel::init_optimization
(
305
int32_t count, int32_t *IDX,
float64_t
* weights)
306
{
307
SG_ERROR
(
"kernel does not support linadd optimization\n"
);
308
return
false ;
309
}
310
311
bool
CKernel::delete_optimization
()
312
{
313
SG_ERROR
(
"kernel does not support linadd optimization\n"
);
314
return
false
;
315
}
316
317
float64_t
CKernel::compute_optimized
(int32_t vector_idx)
318
{
319
SG_ERROR
(
"kernel does not support linadd optimization\n"
);
320
return
0;
321
}
322
323
void
CKernel::compute_batch
(
324
int32_t num_vec, int32_t* vec_idx,
float64_t
* target, int32_t num_suppvec,
325
int32_t* IDX,
float64_t
* weights,
float64_t
factor)
326
{
327
SG_ERROR
(
"kernel does not support batch computation\n"
);
328
}
329
330
void
CKernel::add_to_normal
(int32_t vector_idx,
float64_t
weight)
331
{
332
SG_ERROR
(
"kernel does not support linadd optimization, add_to_normal not implemented\n"
);
333
}
334
335
void
CKernel::clear_normal
()
336
{
337
SG_ERROR
(
"kernel does not support linadd optimization, clear_normal not implemented\n"
);
338
}
339
340
int32_t
CKernel::get_num_subkernels
()
341
{
342
return
1;
343
}
344
345
void
CKernel::compute_by_subkernel
(
346
int32_t vector_idx,
float64_t
* subkernel_contrib)
347
{
348
SG_ERROR
(
"kernel compute_by_subkernel not implemented\n"
);
349
}
350
351
const
float64_t
*
CKernel::get_subkernel_weights
(int32_t &num_weights)
352
{
353
num_weights=1 ;
354
return
&
combined_kernel_weight
;
355
}
356
357
void
CKernel::set_subkernel_weights
(
SGVector<float64_t>
weights)
358
{
359
ASSERT
(weights.
vector
);
360
if
(weights.
vlen
!=1)
361
SG_ERROR
(
"number of subkernel weights should be one ...\n"
);
362
363
combined_kernel_weight
= weights.
vector
[0] ;
364
}
365
366
bool
CKernel::init_optimization_svm
(
CSVM
* svm)
367
{
368
int32_t num_suppvec=svm->
get_num_support_vectors
();
369
int32_t* sv_idx=
SG_MALLOC
(int32_t, num_suppvec);
370
float64_t
* sv_weight=
SG_MALLOC
(
float64_t
, num_suppvec);
371
372
for
(int32_t i=0; i<num_suppvec; i++)
373
{
374
sv_idx[i] = svm->
get_support_vector
(i);
375
sv_weight[i] = svm->
get_alpha
(i);
376
}
377
bool
ret =
init_optimization
(num_suppvec, sv_idx, sv_weight);
378
379
SG_FREE
(sv_idx);
380
SG_FREE
(sv_weight);
381
return
ret;
382
}
383
384
void
CKernel::load_serializable_post
() throw (
ShogunException
)
385
{
386
CSGObject::load_serializable_post
();
387
if
(
lhs_equals_rhs
)
388
rhs
=
lhs
;
389
}
390
391
void
CKernel::save_serializable_pre
() throw (
ShogunException
)
392
{
393
CSGObject::save_serializable_pre
();
394
395
if
(
lhs_equals_rhs
)
396
rhs
=NULL;
397
}
398
399
void
CKernel::save_serializable_post
() throw (
ShogunException
)
400
{
401
CSGObject::save_serializable_post
();
402
403
if
(
lhs_equals_rhs
)
404
rhs
=
lhs
;
405
}
406
407
void
CKernel::register_params
() {
408
m_parameters
->
add
(&
cache_size
,
"cache_size"
,
409
"Cache size in MB."
);
410
m_parameters
->
add
((
CSGObject
**) &
lhs
,
"lhs"
,
411
"Feature vectors to occur on left hand side."
);
412
m_parameters
->
add
((
CSGObject
**) &
rhs
,
"rhs"
,
413
"Feature vectors to occur on right hand side."
);
414
m_parameters
->
add
(&
lhs_equals_rhs
,
"lhs_equals_rhs"
,
415
"If features on lhs are the same as on rhs."
);
416
m_parameters
->
add
(&
num_lhs
,
"num_lhs"
,
417
"Number of feature vectors on left hand side."
);
418
m_parameters
->
add
(&
num_rhs
,
"num_rhs"
,
419
"Number of feature vectors on right hand side."
);
420
m_parameters
->
add
(&
combined_kernel_weight
,
"combined_kernel_weight"
,
421
"Combined kernel weight."
);
422
m_parameters
->
add
(&
optimization_initialized
,
423
"optimization_initialized"
,
424
"Optimization is initialized."
);
425
m_parameters
->
add
((
machine_int_t
*) &
opt_type
,
"opt_type"
,
426
"Optimization type."
);
427
m_parameters
->
add
(&
properties
,
"properties"
,
428
"Kernel properties."
);
429
m_parameters
->
add
((
CSGObject
**) &
normalizer
,
"normalizer"
,
430
"Normalize the kernel."
);
431
}
432
433
434
void
CKernel::init()
435
{
436
cache_size
=10;
437
kernel_matrix
=NULL;
438
lhs
=NULL;
439
rhs
=NULL;
440
num_lhs
=0;
441
num_rhs
=0;
442
combined_kernel_weight
=1;
443
optimization_initialized
=
false
;
444
opt_type
=
FASTBUTMEMHUNGRY
;
445
properties
=
KP_NONE
;
446
normalizer
=NULL;
447
448
449
450
set_normalizer
(
new
CIdentityKernelNormalizer
());
451
}
SHOGUN
Machine Learning Toolbox - Documentation