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
kernel
ProductKernel.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
* Copyright (C) 2012 Jacob Walker
8
*
9
* Code adapted from CCombinedKernel
10
*/
11
12
#ifndef _PRODUCTKERNEL_H___
13
#define _PRODUCTKERNEL_H___
14
15
#include <
shogun/lib/List.h
>
16
#include <
shogun/io/SGIO.h
>
17
#include <
shogun/kernel/Kernel.h
>
18
19
#include <
shogun/features/Features.h
>
20
#include <
shogun/features/CombinedFeatures.h
>
21
22
namespace
shogun
23
{
24
class
CFeatures;
25
class
CCombinedFeatures;
26
class
CList;
27
class
CListElement;
42
class
CProductKernel
:
public
CKernel
43
{
44
public
:
50
CProductKernel
(int32_t size=10);
51
52
virtual
~CProductKernel
();
53
60
virtual
bool
init
(
CFeatures
*
lhs
,
CFeatures
*
rhs
);
61
63
virtual
void
cleanup
();
64
69
virtual
EKernelType
get_kernel_type
()
70
{
71
return
K_PRODUCT
;
72
}
73
78
virtual
EFeatureType
get_feature_type
()
79
{
80
return
F_UNKNOWN
;
81
}
82
87
virtual
EFeatureClass
get_feature_class
()
88
{
89
return
C_COMBINED
;
90
}
91
96
virtual
const
char
*
get_name
()
const
{
return
"ProductKernel"
; }
97
99
void
list_kernels
();
100
105
inline
CKernel
*
get_first_kernel
()
106
{
107
return
(
CKernel
*)
kernel_list
->
get_first_element
();
108
}
109
115
inline
CKernel
*
get_first_kernel
(
CListElement
*& current)
116
{
117
return
(
CKernel
*)
kernel_list
->
get_first_element
(current);
118
}
119
125
inline
CKernel
*
get_kernel
(int32_t idx)
126
{
127
CKernel
* k =
get_first_kernel
();
128
for
(int32_t i=0; i<idx; i++)
129
{
130
SG_UNREF
(k);
131
k =
get_next_kernel
();
132
}
133
return
k;
134
}
135
140
inline
CKernel
*
get_last_kernel
()
141
{
142
return
(
CKernel
*)
kernel_list
->
get_last_element
();
143
}
144
149
inline
CKernel
*
get_next_kernel
()
150
{
151
return
(
CKernel
*)
kernel_list
->
get_next_element
();
152
}
153
159
inline
CKernel
*
get_next_kernel
(
CListElement
*& current)
160
{
161
return
(
CKernel
*)
kernel_list
->
get_next_element
(current);
162
}
163
169
inline
bool
insert_kernel
(
CKernel
* k)
170
{
171
ASSERT
(k);
172
adjust_num_lhs_rhs_initialized
(k);
173
174
if
(!(k->
has_property
(
KP_LINADD
)))
175
unset_property
(
KP_LINADD
);
176
177
return
kernel_list
->
insert_element
(k);
178
}
179
185
inline
bool
append_kernel
(
CKernel
* k)
186
{
187
ASSERT
(k);
188
adjust_num_lhs_rhs_initialized
(k);
189
190
if
(!(k->
has_property
(
KP_LINADD
)))
191
unset_property
(
KP_LINADD
);
192
193
return
kernel_list
->
append_element
(k);
194
}
195
196
201
inline
bool
delete_kernel
()
202
{
203
CKernel
* k=(
CKernel
*)
kernel_list
->
delete_element
();
204
SG_UNREF
(k);
205
206
if
(!k)
207
{
208
num_lhs
=0;
209
num_rhs
=0;
210
}
211
212
return
(k!=NULL);
213
}
214
215
220
inline
int32_t
get_num_subkernels
()
221
{
222
return
kernel_list
->
get_num_elements
();
223
}
224
229
virtual
inline
bool
has_features
()
230
{
231
return
initialized
;
232
}
233
235
virtual
void
remove_lhs
();
236
238
virtual
void
remove_rhs
();
239
241
virtual
void
remove_lhs_and_rhs
();
242
244
bool
precompute_subkernels
();
245
249
CProductKernel
*
KernelToProductKernel
(
shogun::CKernel
* n)
250
{
251
return
dynamic_cast<
CProductKernel
*
>
(n);
252
}
253
262
SGMatrix<float64_t>
get_parameter_gradient
(
TParameter
* param,
263
CSGObject
* obj,
index_t
index);
264
269
inline
CList
*
get_list
() {
SG_REF
(
kernel_list
);
return
kernel_list
;}
270
271
protected
:
278
virtual
float64_t
compute
(int32_t x, int32_t y);
279
285
inline
void
adjust_num_lhs_rhs_initialized
(
CKernel
* k)
286
{
287
ASSERT
(k);
288
289
if
(k->
get_num_vec_lhs
())
290
{
291
if
(
num_lhs
)
292
ASSERT
(
num_lhs
==k->
get_num_vec_lhs
());
293
num_lhs
=k->
get_num_vec_lhs
();
294
295
if
(!
get_num_subkernels
())
296
{
297
initialized
=
true
;
298
#ifdef USE_SVMLIGHT
299
cache_reset
();
300
#endif //USE_SVMLIGHT
301
}
302
}
303
else
304
initialized
=
false
;
305
306
if
(k->
get_num_vec_rhs
())
307
{
308
if
(
num_rhs
)
309
ASSERT
(
num_rhs
==k->
get_num_vec_rhs
());
310
num_rhs
=k->
get_num_vec_rhs
();
311
312
if
(!
get_num_subkernels
())
313
{
314
initialized
=
true
;
315
#ifdef USE_SVMLIGHT
316
cache_reset
();
317
#endif //USE_SVMLIGHT
318
}
319
}
320
else
321
initialized
=
false
;
322
}
323
324
private
:
325
void
init();
326
327
protected
:
329
CList
*
kernel_list
;
331
bool
initialized
;
332
};
333
}
334
#endif
/* _PRODUCTKERNEL_H__ */
SHOGUN
Machine Learning Toolbox - Documentation