SHOGUN
v3.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
normalizer
DiceKernelNormalizer.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) 2009 Soeren Sonnenburg
8
* Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9
*/
10
11
#ifndef _DICEKERNELNORMALIZER_H___
12
#define _DICEKERNELNORMALIZER_H___
13
14
#include <
shogun/kernel/normalizer/KernelNormalizer.h
>
15
#include <
shogun/kernel/string/CommWordStringKernel.h
>
16
17
namespace
shogun
18
{
26
class
CDiceKernelNormalizer
:
public
CKernelNormalizer
27
{
28
public
:
33
CDiceKernelNormalizer
(
bool
use_opt_diag=
false
) :
CKernelNormalizer
(),
34
diag_lhs
(NULL),
num_diag_lhs
(0),
diag_rhs
(NULL),
num_diag_rhs
(0),
35
use_optimized_diagonal_computation
(use_opt_diag)
36
{
37
m_parameters
->
add_vector
(&
diag_lhs
, &
num_diag_lhs
,
"diag_lhs"
,
38
"K(x,x) for left hand side examples."
);
39
m_parameters
->
add_vector
(&
diag_rhs
, &
num_diag_rhs
,
"diag_rhs"
,
40
"K(x,x) for right hand side examples."
);
41
SG_ADD
(&
use_optimized_diagonal_computation
,
42
"use_optimized_diagonal_computation"
,
43
"flat if optimized diagonal computation is used"
,
MS_NOT_AVAILABLE
);
44
}
45
47
virtual
~CDiceKernelNormalizer
()
48
{
49
SG_FREE(
diag_lhs
);
50
SG_FREE(
diag_rhs
);
51
}
52
55
virtual
bool
init
(
CKernel
* k)
56
{
57
ASSERT
(k)
58
num_diag_lhs
=k->
get_num_vec_lhs
();
59
num_diag_rhs
=k->
get_num_vec_rhs
();
60
ASSERT
(
num_diag_lhs
>0)
61
ASSERT
(
num_diag_rhs
>0)
62
63
CFeatures
* old_lhs=k->
lhs
;
64
CFeatures
* old_rhs=k->
rhs
;
65
66
k->
lhs
=old_lhs;
67
k->
rhs
=old_lhs;
68
bool
r1=
alloc_and_compute_diag
(k,
diag_lhs
,
num_diag_lhs
);
69
70
k->
lhs
=old_rhs;
71
k->
rhs
=old_rhs;
72
bool
r2=
alloc_and_compute_diag
(k,
diag_rhs
,
num_diag_rhs
);
73
74
k->
lhs
=old_lhs;
75
k->
rhs
=old_rhs;
76
77
return
r1 && r2;
78
}
79
85
virtual
float64_t
normalize
(
86
float64_t
value, int32_t idx_lhs, int32_t idx_rhs)
87
{
88
float64_t
diag_sum=
diag_lhs
[idx_lhs]*
diag_rhs
[idx_rhs];
89
return
2*value/diag_sum;
90
}
91
96
virtual
float64_t
normalize_lhs
(
float64_t
value, int32_t idx_lhs)
97
{
98
SG_ERROR
(
"linadd not supported with Dice normalization.\n"
)
99
return
0;
100
}
101
106
virtual
float64_t
normalize_rhs
(
float64_t
value, int32_t idx_rhs)
107
{
108
SG_ERROR
(
"linadd not supported with Dice normalization.\n"
)
109
return
0;
110
}
111
117
virtual
const
char
*
get_name
()
const
{
118
return
"DiceKernelNormalizer"
; }
119
120
public
:
125
bool
alloc_and_compute_diag
(
CKernel
* k,
float64_t
* &v, int32_t num)
126
{
127
SG_FREE(v);
128
v=SG_MALLOC(
float64_t
, num);
129
130
for
(int32_t i=0; i<num; i++)
131
{
132
if
(k->
get_kernel_type
() ==
K_COMMWORDSTRING
)
133
{
134
if
(
use_optimized_diagonal_computation
)
135
v[i]=((
CCommWordStringKernel
*) k)->compute_diag(i);
136
else
137
v[i]=((
CCommWordStringKernel
*) k)->compute_helper(i,i,
true
);
138
}
139
else
140
v[i]=k->
compute
(i,i);
141
142
if
(v[i]==0.0)
143
v[i]=1e-16;
/* avoid divide by zero exception */
144
}
145
146
return
(v!=NULL);
147
}
148
149
protected
:
151
float64_t
*
diag_lhs
;
153
int32_t
num_diag_lhs
;
154
156
float64_t
*
diag_rhs
;
158
int32_t
num_diag_rhs
;
159
161
bool
use_optimized_diagonal_computation
;
162
};
163
}
164
#endif
SHOGUN
Machine Learning Toolbox - Documentation