SHOGUN
v1.1.0
Main Page
Related Pages
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
src
shogun
modelselection
GridSearchModelSelection.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) 2011 Heiko Strathmann
8
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
9
*/
10
11
#include <
shogun/modelselection/GridSearchModelSelection.h
>
12
#include <
shogun/modelselection/ParameterCombination.h
>
13
#include <
shogun/modelselection/ModelSelectionParameters.h
>
14
#include <
shogun/evaluation/CrossValidation.h
>
15
#include <
shogun/machine/Machine.h
>
16
17
using namespace
shogun;
18
19
CGridSearchModelSelection::CGridSearchModelSelection
() :
20
CModelSelection
(NULL, NULL)
21
{
22
23
}
24
25
CGridSearchModelSelection::CGridSearchModelSelection
(
26
CModelSelectionParameters
* model_parameters,
27
CCrossValidation
* cross_validation) :
28
CModelSelection
(model_parameters, cross_validation)
29
{
30
31
}
32
33
CGridSearchModelSelection::~CGridSearchModelSelection
()
34
{
35
}
36
37
CParameterCombination
*
CGridSearchModelSelection::select_model
()
38
{
39
/* Retrieve all possible parameter combinations */
40
CDynamicObjectArray<CParameterCombination>
* combinations=
41
m_model_parameters
->
get_combinations
();
42
43
CrossValidationResult
best_result;
44
45
CParameterCombination
* best_combination=NULL;
46
if
(
m_cross_validation
->
get_evaluation_direction
()==
ED_MAXIMIZE
)
47
best_result.
mean
=
CMath::ALMOST_NEG_INFTY
;
48
else
49
best_result.
mean
=
CMath::ALMOST_INFTY
;
50
51
/* underlying learning machine */
52
CMachine
* machine=
m_cross_validation
->
get_machine
();
53
54
/* apply all combinations and search for best one */
55
for
(
index_t
i=0; i<combinations->
get_num_elements
(); ++i)
56
{
57
CParameterCombination
* current_combination=combinations->
get_element
(i);
58
current_combination->
apply_to_modsel_parameter
(
59
machine->m_model_selection_parameters);
60
CrossValidationResult
result=
m_cross_validation
->
evaluate
();
61
62
/* check if current result is better, delete old combinations */
63
if
(
m_cross_validation
->
get_evaluation_direction
()==
ED_MAXIMIZE
)
64
{
65
if
(result.
mean
>best_result.
mean
)
66
{
67
if
(best_combination)
68
SG_UNREF
(best_combination);
69
70
best_combination=combinations->
get_element
(i);
71
best_result=result;
72
}
73
else
74
{
75
CParameterCombination
* combination=combinations->
get_element
(i);
76
SG_UNREF
(combination);
77
}
78
}
79
else
80
{
81
if
(result.
mean
<best_result.
mean
)
82
{
83
if
(best_combination)
84
SG_UNREF
(best_combination);
85
86
best_combination=combinations->
get_element
(i);
87
best_result=result;
88
}
89
else
90
{
91
CParameterCombination
* combination=combinations->
get_element
(i);
92
SG_UNREF
(combination);
93
}
94
}
95
96
SG_UNREF
(current_combination);
97
}
98
99
SG_UNREF
(machine);
100
SG_UNREF
(combinations);
101
102
return
best_combination;
103
}
104
SHOGUN
Machine Learning Toolbox - Documentation