SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
kernel
GaussianShortRealKernel.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) 2008-2009 Soeren Sonnenburg
8
* Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9
*/
10
11
#include <
shogun/lib/common.h
>
12
#include <
shogun/kernel/GaussianShortRealKernel.h
>
13
#include <
shogun/features/Features.h
>
14
#include <
shogun/io/SGIO.h
>
15
16
using namespace
shogun;
17
18
CGaussianShortRealKernel::CGaussianShortRealKernel
()
19
:
CDotKernel
(0), width(0.0)
20
{
21
register_params
();
22
}
23
24
CGaussianShortRealKernel::CGaussianShortRealKernel
(int32_t size,
float64_t
w)
25
:
CDotKernel
(size), width(w)
26
{
27
register_params
();
28
}
29
30
CGaussianShortRealKernel::CGaussianShortRealKernel
(
31
CDenseFeatures<float32_t>
* l,
CDenseFeatures<float32_t>
* r,
float64_t
w, int32_t size)
32
:
CDotKernel
(size), width(w)
33
{
34
init
(l,r);
35
register_params
();
36
}
37
38
CGaussianShortRealKernel::~CGaussianShortRealKernel
()
39
{
40
}
41
42
bool
CGaussianShortRealKernel::init(
CFeatures
* l,
CFeatures
* r)
43
{
44
CDotKernel::init(l, r);
45
return
init_normalizer
();
46
}
47
48
float64_t
CGaussianShortRealKernel::compute
(int32_t idx_a, int32_t idx_b)
49
{
50
int32_t alen, blen;
51
bool
afree, bfree;
52
53
float32_t
* avec=((
CDenseFeatures<float32_t>
*)
lhs
)->get_feature_vector(idx_a, alen, afree);
54
float32_t
* bvec=((
CDenseFeatures<float32_t>
*)
rhs
)->get_feature_vector(idx_b, blen, bfree);
55
ASSERT
(alen==blen)
56
57
float64_t
result=0;
58
for
(int32_t i=0; i<alen; i++)
59
result+=
CMath::sq
(avec[i]-bvec[i]);
60
61
result=exp(-result/
width
);
62
63
((
CDenseFeatures<float32_t>
*)
lhs
)->free_feature_vector(avec, idx_a, afree);
64
((
CDenseFeatures<float32_t>
*)
rhs
)->free_feature_vector(bvec, idx_b, bfree);
65
66
return
result;
67
}
68
69
void
CGaussianShortRealKernel::register_params
()
70
{
71
SG_ADD
(&
width
,
"width"
,
"kernel width"
,
MS_AVAILABLE
);
72
}
SHOGUN
机器学习工具包 - 项目文档