/
PolyKernel.cpp
84 lines (73 loc) · 1.97 KB
/
PolyKernel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Heiko Strathmann, Kyle McQuisten
*/
#include <shogun/features/DenseFeatures.h>
#include <shogun/features/DotFeatures.h>
#include <shogun/io/SGIO.h>
#include <shogun/kernel/PolyKernel.h>
#include <shogun/kernel/normalizer/SqrtDiagKernelNormalizer.h>
#include <shogun/lib/auto_initialiser.h>
#include <shogun/lib/common.h>
#include <shogun/lib/config.h>
using namespace shogun;
PolyKernel::PolyKernel() : DotKernel(0)
{
init();
}
PolyKernel::PolyKernel(int32_t size, int32_t d, float64_t c, float64_t gamma)
: DotKernel(size)
{
require(c >= 0.0, "c parameter must be positive!");
init();
degree = d;
m_c = c;
m_gamma = gamma;
}
PolyKernel::PolyKernel(
const std::shared_ptr<DotFeatures>& l, const std::shared_ptr<DotFeatures>& r, int32_t d, float64_t c, float64_t gamma,
int32_t size)
: DotKernel(size)
{
require(c >= 0.0, "c parameter must be positive!");
init();
degree = d;
m_c = c;
init(l, r);
m_gamma = gamma;
}
PolyKernel::~PolyKernel()
{
cleanup();
}
bool PolyKernel::init(std::shared_ptr<Features> l, std::shared_ptr<Features> r)
{
DotKernel::init(l, r);
return init_normalizer();
}
void PolyKernel::cleanup()
{
Kernel::cleanup();
}
float64_t PolyKernel::compute(int32_t idx_a, int32_t idx_b)
{
auto result = std::get<float64_t>(m_gamma) * DotKernel::compute(idx_a, idx_b) + m_c;
return Math::pow(result, degree);
}
void PolyKernel::init()
{
degree = 0;
m_c = 0.0;
set_normalizer(std::make_shared<SqrtDiagKernelNormalizer>());
SG_ADD(
°ree, "degree", "Degree of polynomial kernel",
ParameterProperties::HYPER);
SG_ADD(
&m_c, "c", "The kernel is inhomogeneous if the value is higher than 0",
ParameterProperties::HYPER);
SG_ADD(
&m_gamma, "gamma", "Scaler for the dot product",
ParameterProperties::HYPER | ParameterProperties::AUTO,
std::make_shared<params::GammaFeatureNumberInit<PolyKernel>>(*this, 1.0));
}