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
string
GaussianMatchStringKernel.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
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9
*/
10
11
#include <
shogun/lib/common.h
>
12
#include <
shogun/io/SGIO.h
>
13
#include <
shogun/kernel/string/GaussianMatchStringKernel.h
>
14
#include <
shogun/kernel/normalizer/SqrtDiagKernelNormalizer.h
>
15
#include <
shogun/features/Features.h
>
16
#include <
shogun/features/StringFeatures.h
>
17
18
using namespace
shogun;
19
20
CGaussianMatchStringKernel::CGaussianMatchStringKernel
()
21
:
CStringKernel
<char>(0), width(0.0)
22
{
23
set_normalizer
(
new
CSqrtDiagKernelNormalizer
());
24
register_params
();
25
}
26
27
CGaussianMatchStringKernel::CGaussianMatchStringKernel
(int32_t size,
float64_t
w)
28
:
CStringKernel
<char>(size), width(w)
29
{
30
set_normalizer
(
new
CSqrtDiagKernelNormalizer
());
31
register_params
();
32
}
33
34
CGaussianMatchStringKernel::CGaussianMatchStringKernel
(
35
CStringFeatures<char>
* l,
CStringFeatures<char>
* r,
float64_t
w)
36
:
CStringKernel
<char>(10), width(w)
37
{
38
set_normalizer
(
new
CSqrtDiagKernelNormalizer
());
39
init
(l, r);
40
register_params
();
41
}
42
43
CGaussianMatchStringKernel::~CGaussianMatchStringKernel
()
44
{
45
cleanup
();
46
}
47
48
bool
CGaussianMatchStringKernel::init(
CFeatures
* l,
CFeatures
* r)
49
{
50
CStringKernel<char>::init
(l, r);
51
return
init_normalizer
();
52
}
53
54
void
CGaussianMatchStringKernel::cleanup
()
55
{
56
CKernel::cleanup
();
57
}
58
59
float64_t
CGaussianMatchStringKernel::compute
(int32_t idx_a, int32_t idx_b)
60
{
61
int32_t i, alen, blen ;
62
bool
free_avec, free_bvec;
63
64
char
* avec = ((
CStringFeatures<char>
*)
lhs
)->get_feature_vector(idx_a, alen, free_avec);
65
char
* bvec = ((
CStringFeatures<char>
*)
rhs
)->get_feature_vector(idx_b, blen, free_bvec);
66
67
float64_t
result=0;
68
69
ASSERT
(alen==blen)
70
71
for
(i = 0; i<alen; i++)
72
result+=(avec[i]==bvec[i]) ? 0:4;
73
74
result=exp(-result/
width
);
75
76
77
((
CStringFeatures<char>
*)
lhs
)->free_feature_vector(avec, idx_a, free_avec);
78
((
CStringFeatures<char>
*)
rhs
)->free_feature_vector(bvec, idx_b, free_bvec);
79
return
result;
80
}
81
82
void
CGaussianMatchStringKernel::register_params
()
83
{
84
SG_ADD
(&
width
,
"width"
,
"kernel width"
,
MS_AVAILABLE
);
85
}
SHOGUN
Machine Learning Toolbox - Documentation