SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
kernel
string
GaussianMatchStringKernel.cpp
浏览该文件的文档.
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
机器学习工具包 - 项目文档