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
VarianceKernelNormalizer.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 _VARIANCEKERNELNORMALIZER_H___
12
#define _VARIANCEKERNELNORMALIZER_H___
13
14
#include <
shogun/kernel/normalizer/KernelNormalizer.h
>
15
16
namespace
shogun
17
{
27
class
CVarianceKernelNormalizer
:
public
CKernelNormalizer
28
{
29
public
:
32
CVarianceKernelNormalizer
()
33
:
CKernelNormalizer
(),
meandiff
(1.0),
sqrt_meandiff
(1.0)
34
{
35
SG_ADD
(&
meandiff
,
"meandiff"
,
"Scaling constant."
,
MS_AVAILABLE
);
36
SG_ADD
(&
sqrt_meandiff
,
"sqrt_meandiff"
,
37
"Square root of scaling constant."
,
MS_AVAILABLE
);
38
}
39
41
virtual
~CVarianceKernelNormalizer
()
42
{
43
}
44
47
virtual
bool
init
(
CKernel
* k)
48
{
49
ASSERT
(k)
50
int32_t n=k->
get_num_vec_lhs
();
51
ASSERT
(n>0)
52
53
CFeatures
* old_lhs=k->
lhs
;
54
CFeatures
* old_rhs=k->
rhs
;
55
k->
lhs
=old_lhs;
56
k->
rhs
=old_lhs;
57
58
float64_t
diag_mean=0;
59
float64_t
overall_mean=0;
60
for
(int32_t i=0; i<n; i++)
61
{
62
diag_mean+=k->
compute
(i, i);
63
64
for
(int32_t j=0; j<n; j++)
65
overall_mean+=k->
compute
(i, j);
66
}
67
diag_mean/=n;
68
overall_mean/=((
float64_t
) n)*n;
69
70
k->
lhs
=old_lhs;
71
k->
rhs
=old_rhs;
72
73
meandiff
=1.0/(diag_mean-overall_mean);
74
sqrt_meandiff
=
CMath::sqrt
(
meandiff
);
75
76
return
true
;
77
}
78
84
virtual
float64_t
normalize
(
85
float64_t
value, int32_t idx_lhs, int32_t idx_rhs)
86
{
87
return
value*
meandiff
;
88
}
89
94
virtual
float64_t
normalize_lhs
(
float64_t
value, int32_t idx_lhs)
95
{
96
return
value*
sqrt_meandiff
;
97
}
98
103
virtual
float64_t
normalize_rhs
(
float64_t
value, int32_t idx_rhs)
104
{
105
return
value*
sqrt_meandiff
;
106
}
107
109
virtual
const
char
*
get_name
()
const
{
return
"VarianceKernelNormalizer"
; }
110
111
protected
:
113
float64_t
meandiff
;
115
float64_t
sqrt_meandiff
;
116
};
117
}
118
#endif
SHOGUN
Machine Learning Toolbox - Documentation