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
statistics
MMDKernelSelectionCombOpt.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) 2012-2013 Heiko Strathmann
8
*/
9
10
#include <
shogun/statistics/MMDKernelSelectionCombOpt.h
>
11
#include <
shogun/statistics/LinearTimeMMD.h
>
12
#include <
shogun/kernel/CombinedKernel.h
>
13
14
15
using namespace
shogun;
16
17
CMMDKernelSelectionCombOpt::CMMDKernelSelectionCombOpt
() :
18
CMMDKernelSelectionComb
()
19
{
20
init();
21
}
22
23
CMMDKernelSelectionCombOpt::CMMDKernelSelectionCombOpt
(
24
CKernelTwoSampleTestStatistic
* mmd,
float64_t
lambda) :
25
CMMDKernelSelectionComb
(mmd)
26
{
27
/* currently, this method is only developed for the linear time MMD */
28
REQUIRE
(dynamic_cast<CLinearTimeMMD*>(mmd),
"%s::%s(): Only "
29
"CLinearTimeMMD is currently supported! Provided instance is "
30
"\"%s\"\n"
,
get_name
(),
get_name
(), mmd->
get_name
());
31
32
init();
33
34
m_lambda
=lambda;
35
}
36
37
CMMDKernelSelectionCombOpt::~CMMDKernelSelectionCombOpt
()
38
{
39
}
40
41
void
CMMDKernelSelectionCombOpt::init()
42
{
43
/* set to a sensible standard value that proved to be useful in
44
* experiments, see NIPS paper */
45
m_lambda
=1E-5;
46
47
SG_ADD
(&
m_lambda
,
"lambda"
,
"Regularization parameter lambda"
,
48
MS_NOT_AVAILABLE
);
49
}
50
51
#ifdef HAVE_LAPACK
52
SGVector<float64_t>
CMMDKernelSelectionCombOpt::compute_measures
()
53
{
54
/* cast is safe due to assertion in constructor */
55
CCombinedKernel
* kernel=(
CCombinedKernel
*)
m_mmd
->
get_kernel
();
56
index_t
num_kernels=kernel->
get_num_subkernels
();
57
SG_UNREF
(kernel);
58
59
/* allocate space for MMDs and Q matrix */
60
SGVector<float64_t>
mmds(num_kernels);
61
62
/* free matrix by hand since it is static */
63
SG_FREE(
m_Q
.
matrix
);
64
m_Q
.
matrix
=NULL;
65
m_Q
.
num_rows
=0;
66
m_Q
.
num_cols
=0;
67
m_Q
=
SGMatrix<float64_t>
(num_kernels, num_kernels,
false
);
68
69
/* online compute mmds and covariance matrix Q of kernels */
70
((
CLinearTimeMMD
*)
m_mmd
)->compute_statistic_and_Q(mmds,
m_Q
);
71
72
/* evtl regularize to avoid numerical problems (see NIPS paper) */
73
if
(
m_lambda
)
74
{
75
SG_DEBUG
(
"regularizing matrix Q by adding %f to diagonal\n"
,
m_lambda
)
76
for
(
index_t
i=0; i<num_kernels; ++i)
77
m_Q
(i,i)+=
m_lambda
;
78
}
79
80
if
(
sg_io
->
get_loglevel
()==
MSG_DEBUG
)
81
{
82
m_Q
.
display_matrix
(
"(regularized) Q"
);
83
mmds.
display_vector
(
"mmds"
);
84
}
85
86
/* solve the generated problem */
87
SGVector<float64_t>
result=
CMMDKernelSelectionComb::solve_optimization
(mmds);
88
89
/* free matrix by hand since it is static (again) */
90
SG_FREE(
m_Q
.
matrix
);
91
m_Q
.
matrix
=NULL;
92
m_Q
.
num_rows
=0;
93
m_Q
.
num_cols
=0;
94
95
return
result;
96
}
97
98
#endif
SHOGUN
Machine Learning Toolbox - Documentation