Shark machine learning library
About Shark
News!
Contribute
Credits and copyright
Downloads
Getting Started
Installation
Using the docs
Documentation
Tutorials
Quick references
Class list
Global functions
FAQ
Showroom
examples
Supervised
KernelSelection.cpp
Go to the documentation of this file.
1
#include <
shark/Data/DataDistribution.h
>
2
#include <
shark/Models/Kernels/GaussianRbfKernel.h
>
3
#include <
shark/ObjectiveFunctions/RadiusMarginQuotient.h
>
4
#include <
shark/Algorithms/GradientDescent/Rprop.h
>
5
6
7
using namespace
shark
;
8
9
10
int
main
(
int
argc,
char
** argv)
11
{
12
// generate dataset
13
Chessboard
problem;
14
ClassificationDataset
data = problem.
generateDataset
(100);
15
16
// brute force search in [1.0, 10000.0] on log scale
17
GaussianRbfKernel<>
kernel;
18
RadiusMarginQuotient<RealVector>
rm(data, &kernel);
19
RealVector param(1);
20
double
best_value = 1e100;
21
double
best_gamma = 0.0;
22
23
std::cout<<
"Grid search in the range [1, 10000] on log scale:"
<<std::endl;
24
for
(
unsigned
i=0; i<=400; i++)
25
{
26
double
gamma = pow(10.0, i / 100.0);
27
param(0) = gamma;
28
double
f = rm.
eval
(param);
29
if
(f < best_value)
30
{
31
best_value = f;
32
best_gamma = gamma;
33
}
34
}
35
std::cout<<
"best gamma: "
<< best_gamma<<
" radius margin quotient: "
<<best_value<<std::endl;
36
37
// gradient-based alternative
38
IRpropPlus
rprop;
39
rprop.
init
(rm, RealVector(1, 100.0), 1.0);
40
std::cout<<
"\nGradient-based optimization (IRprop+, 50 steps):"
<<std::endl;
41
for
(
unsigned
i=0; i<50; i++) rprop.
step
(rm);
42
best_gamma = rprop.
solution
().
point
(0);
43
best_value = rm.
eval
(RealVector(1, best_gamma));
44
std::cout<<
"best gamma: "
<< best_gamma<<
" radius margin quotient: "
<<best_value<<std::endl;
45
}