-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
KernelSelectionStrategy.cpp
249 lines (219 loc) · 7.82 KB
/
KernelSelectionStrategy.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2012 - 2013 Heiko Strathmann
* Written (w) 2014 - 2016 Soumyajit De
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*/
#include <shogun/io/SGIO.h>
#include <shogun/lib/SGVector.h>
#include <shogun/lib/SGMatrix.h>
#include <shogun/distance/CustomDistance.h>
#include <shogun/statistical_testing/MMD.h>
#include <shogun/statistical_testing/internals/KernelManager.h>
#include <shogun/statistical_testing/kernelselection/KernelSelectionStrategy.h>
#include <shogun/statistical_testing/kernelselection/internals/KernelSelection.h>
#include <shogun/statistical_testing/kernelselection/internals/MaxMeasure.h>
#include <shogun/statistical_testing/kernelselection/internals/MaxTestPower.h>
#include <shogun/statistical_testing/kernelselection/internals/MaxXValidation.h>
#include <shogun/statistical_testing/kernelselection/internals/MedianHeuristic.h>
#include <shogun/statistical_testing/kernelselection/internals/WeightedMaxMeasure.h>
#include <shogun/statistical_testing/kernelselection/internals/WeightedMaxTestPower.h>
using namespace shogun;
using namespace internal;
struct CKernelSelectionStrategy::Self
{
Self();
KernelManager kernel_mgr;
std::unique_ptr<KernelSelection> policy;
EKernelSelectionMethod method;
bool weighted;
index_t num_runs;
float64_t alpha;
void init_policy(CMMD* estimator);
const static EKernelSelectionMethod default_method;
const static bool default_weighted;
const static index_t default_num_runs;
const static float64_t default_alpha;
};
const EKernelSelectionMethod CKernelSelectionStrategy::Self::default_method=KSM_AUTO;
const bool CKernelSelectionStrategy::Self::default_weighted=false;
const index_t CKernelSelectionStrategy::Self::default_num_runs=10;
const float64_t CKernelSelectionStrategy::Self::default_alpha=0.05;
CKernelSelectionStrategy::Self::Self() : policy(nullptr), method(default_method),
weighted(default_weighted), num_runs(default_num_runs), alpha(default_alpha)
{
}
void CKernelSelectionStrategy::Self::init_policy(CMMD* estimator)
{
switch (method)
{
case KSM_MEDIAN_HEURISTIC:
{
REQUIRE(!weighted, "Weighted kernel selection is not possible with MEDIAN_HEURISTIC!\n");
policy=std::unique_ptr<MedianHeuristic>(new MedianHeuristic(kernel_mgr, estimator));
}
break;
case KSM_MAXIMIZE_XVALIDATION:
{
REQUIRE(!weighted, "Weighted kernel selection is not possible with MAXIMIZE_XVALIDATION!\n");
policy=std::unique_ptr<MaxXValidation>(new MaxXValidation(kernel_mgr, estimator,
num_runs, alpha));
}
break;
case KSM_MAXIMIZE_MMD:
{
if (weighted)
policy=std::unique_ptr<WeightedMaxMeasure>(new WeightedMaxMeasure(kernel_mgr, estimator));
else
policy=std::unique_ptr<MaxMeasure>(new MaxMeasure(kernel_mgr, estimator));
}
break;
case KSM_MAXIMIZE_POWER:
{
if (weighted)
policy=std::unique_ptr<WeightedMaxTestPower>(new WeightedMaxTestPower(kernel_mgr, estimator));
else
policy=std::unique_ptr<MaxTestPower>(new MaxTestPower(kernel_mgr, estimator));
}
break;
default:
{
SG_SERROR("Unsupported kernel selection method specified! Accepted strategies are "
"MAXIMIZE_MMD (single, weighted), "
"MAXIMIZE_POWER (single, weighted), "
"MAXIMIZE_XVALIDATION (single) and "
"MEDIAN_HEURISTIC (single)!\n");
}
break;
}
}
CKernelSelectionStrategy::CKernelSelectionStrategy()
{
init();
}
CKernelSelectionStrategy::CKernelSelectionStrategy(EKernelSelectionMethod method)
{
init();
self->method=method;
}
CKernelSelectionStrategy::CKernelSelectionStrategy(EKernelSelectionMethod method, bool weighted)
{
init();
self->method=method;
self->weighted=weighted;
}
CKernelSelectionStrategy::CKernelSelectionStrategy(EKernelSelectionMethod method, index_t num_runs, float64_t alpha)
{
init();
self->method=method;
self->num_runs=num_runs;
self->alpha=alpha;
}
//CKernelSelectionStrategy::CKernelSelectionStrategy(const CKernelSelectionStrategy& other)
//{
// init();
// self->method=other.self->method;
// self->num_runs=other.self->num_runs;
// self->alpha=other.self->alpha;
// for (size_t i=0; i<other.self->kernel_mgr.num_kernels(); ++i)
// self->kernel_mgr.push_back(other.self->kernel_mgr.kernel_at(i));
//}
//
//CKernelSelectionStrategy& CKernelSelectionStrategy::operator=(const CKernelSelectionStrategy& other)
//{
// init();
// self->method=other.self->method;
// self->num_runs=other.self->num_runs;
// self->alpha=other.self->alpha;
// for (size_t i=0; i<other.self->kernel_mgr.num_kernels(); ++i)
// self->kernel_mgr.push_back(other.self->kernel_mgr.kernel_at(i));
// return *this;
//}
void CKernelSelectionStrategy::init()
{
self=std::unique_ptr<Self>(new Self());
}
CKernelSelectionStrategy::~CKernelSelectionStrategy()
{
self->kernel_mgr.clear();
}
CKernelSelectionStrategy& CKernelSelectionStrategy::use_method(EKernelSelectionMethod method)
{
self->method=method;
return *this;
}
CKernelSelectionStrategy& CKernelSelectionStrategy::use_num_runs(index_t num_runs)
{
self->num_runs=num_runs;
return *this;
}
CKernelSelectionStrategy& CKernelSelectionStrategy::use_alpha(float64_t alpha)
{
self->alpha=alpha;
return *this;
}
CKernelSelectionStrategy& CKernelSelectionStrategy::use_weighted(bool weighted)
{
self->weighted=weighted;
return *this;
}
void CKernelSelectionStrategy::add_kernel(CKernel* kernel)
{
self->kernel_mgr.push_back(kernel);
}
CKernel* CKernelSelectionStrategy::select_kernel(CMMD* estimator)
{
auto num_kernels=self->kernel_mgr.num_kernels();
REQUIRE(num_kernels>0, "Number of kernels is 0. Please add kernels using add_kernel method!\n");
SG_DEBUG("Selecting kernels from a total of %d kernels!\n", num_kernels);
self->init_policy(estimator);
ASSERT(self->policy!=nullptr);
return self->policy->select_kernel();
}
void CKernelSelectionStrategy::erase_intermediate_results()
{
self->policy=nullptr;
self->kernel_mgr.clear();
}
SGMatrix<float64_t> CKernelSelectionStrategy::get_measure_matrix()
{
REQUIRE(self->policy!=nullptr, "The kernel selection policy is not initialized!\n");
return self->policy->get_measure_matrix();
}
SGVector<float64_t> CKernelSelectionStrategy::get_measure_vector()
{
REQUIRE(self->policy!=nullptr, "The kernel selection policy is not initialized!\n");
return self->policy->get_measure_vector();
}
const char* CKernelSelectionStrategy::get_name() const
{
return "KernelSelectionStrategy";
}
const KernelManager& CKernelSelectionStrategy::get_kernel_mgr() const
{
return self->kernel_mgr;
}