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
TanimotoKernelNormalizer.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) 2008-2009 Soeren Sonnenburg
8
* Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9
*/
10
11
#ifndef _TANIMOTOKERNELNORMALIZER_H___
12
#define _TANIMOTOKERNELNORMALIZER_H___
13
14
#include <
shogun/kernel/normalizer/KernelNormalizer.h
>
15
#include <
shogun/kernel/string/CommWordStringKernel.h
>
16
17
namespace
shogun
18
{
26
class
CTanimotoKernelNormalizer
:
public
CKernelNormalizer
27
{
28
public
:
33
CTanimotoKernelNormalizer
(
bool
use_opt_diag=
false
)
34
:
CKernelNormalizer
(),
diag_lhs
(NULL),
diag_rhs
(NULL),
35
use_optimized_diagonal_computation
(use_opt_diag)
36
{
37
}
38
40
virtual
~CTanimotoKernelNormalizer
()
41
{
42
SG_FREE(
diag_lhs
);
43
SG_FREE(
diag_rhs
);
44
}
45
48
virtual
bool
init
(
CKernel
* k)
49
{
50
ASSERT
(k)
51
int32_t num_lhs=k->
get_num_vec_lhs
();
52
int32_t num_rhs=k->
get_num_vec_rhs
();
53
ASSERT
(num_lhs>0)
54
ASSERT
(num_rhs>0)
55
56
CFeatures
* old_lhs=k->
lhs
;
57
CFeatures
* old_rhs=k->
rhs
;
58
59
k->
lhs
=old_lhs;
60
k->
rhs
=old_lhs;
61
bool
r1=
alloc_and_compute_diag
(k,
diag_lhs
, num_lhs);
62
63
k->
lhs
=old_rhs;
64
k->
rhs
=old_rhs;
65
bool
r2=
alloc_and_compute_diag
(k,
diag_rhs
, num_rhs);
66
67
k->
lhs
=old_lhs;
68
k->
rhs
=old_rhs;
69
70
return
r1 && r2;
71
}
72
78
virtual
float64_t
normalize
(
79
float64_t
value, int32_t idx_lhs, int32_t idx_rhs)
80
{
81
float64_t
diag_sum=
diag_lhs
[idx_lhs]*
diag_rhs
[idx_rhs];
82
return
value/(diag_sum-value);
83
}
84
89
virtual
float64_t
normalize_lhs
(
float64_t
value, int32_t idx_lhs)
90
{
91
SG_ERROR
(
"linadd not supported with Tanimoto normalization.\n"
)
92
return
0;
93
}
94
99
virtual
float64_t
normalize_rhs
(
float64_t
value, int32_t idx_rhs)
100
{
101
SG_ERROR
(
"linadd not supported with Tanimoto normalization.\n"
)
102
return
0;
103
}
104
110
virtual
const
char
*
get_name
()
const
{
111
return
"TanimotoKernelNormalizer"
; }
112
113
public
:
118
bool
alloc_and_compute_diag
(
CKernel
* k,
float64_t
* &v, int32_t num)
119
{
120
SG_FREE(v);
121
v=SG_MALLOC(
float64_t
, num);
122
123
for
(int32_t i=0; i<num; i++)
124
{
125
if
(k->
get_kernel_type
() ==
K_COMMWORDSTRING
)
126
{
127
if
(
use_optimized_diagonal_computation
)
128
v[i]=((
CCommWordStringKernel
*) k)->compute_diag(i);
129
else
130
v[i]=((
CCommWordStringKernel
*) k)->compute_helper(i,i,
true
);
131
}
132
else
133
v[i]=k->
compute
(i,i);
134
135
if
(v[i]==0.0)
136
v[i]=1e-16;
/* avoid divide by zero exception */
137
}
138
139
return
(v!=NULL);
140
}
141
142
protected
:
144
float64_t
*
diag_lhs
;
146
float64_t
*
diag_rhs
;
148
bool
use_optimized_diagonal_computation
;
149
};
150
}
151
#endif
SHOGUN
Machine Learning Toolbox - Documentation