SHOGUN
v2.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
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-2012 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
CMachineEvaluation
* machine_eval) :
28
CModelSelection
(model_parameters, machine_eval)
29
{
30
31
}
32
33
CGridSearchModelSelection::~CGridSearchModelSelection
()
34
{
35
}
36
37
CParameterCombination
*
CGridSearchModelSelection::select_model
(
bool
print_state)
38
{
39
if
(print_state)
40
SG_PRINT
(
"Generating parameter combinations\n"
);
41
42
/* Retrieve all possible parameter combinations */
43
CDynamicObjectArray
* combinations=
44
(
CDynamicObjectArray
*)
m_model_parameters
->
get_combinations
();
45
46
CCrossValidationResult
* best_result =
new
CCrossValidationResult
();
47
48
CParameterCombination
* best_combination=NULL;
49
if
(
m_machine_eval
->
get_evaluation_direction
()==
ED_MAXIMIZE
)
50
{
51
if
(print_state)
SG_PRINT
(
"Direction is maximize\n"
);
52
best_result->
mean
=
CMath::ALMOST_NEG_INFTY
;
53
}
54
else
55
{
56
if
(print_state)
SG_PRINT
(
"Direction is maximize\n"
);
57
best_result->
mean
=
CMath::ALMOST_INFTY
;
58
}
59
60
/* underlying learning machine */
61
CMachine
* machine=
m_machine_eval
->
get_machine
();
62
63
/* apply all combinations and search for best one */
64
for
(
index_t
i=0; i<combinations->
get_num_elements
(); ++i)
65
{
66
CParameterCombination
* current_combination=(
CParameterCombination
*)
67
combinations->
get_element
(i);
68
69
/* eventually print */
70
if
(print_state)
71
{
72
SG_PRINT
(
"trying combination:\n"
);
73
current_combination->
print_tree
();
74
}
75
76
current_combination->
apply_to_modsel_parameter
(
77
machine->
m_model_selection_parameters
);
78
79
/* note that this may implicitly lock and unlockthe machine */
80
CCrossValidationResult
* result =
81
(
CCrossValidationResult
*)(
m_machine_eval
->
evaluate
());
82
83
if
(result->
get_result_type
() !=
CROSSVALIDATION_RESULT
)
84
SG_ERROR
(
"Evaluation result is not of type CCrossValidationResult!"
);
85
86
if
(print_state)
87
result->
print_result
();
88
89
/* check if current result is better, delete old combinations */
90
if
(
m_machine_eval
->
get_evaluation_direction
()==
ED_MAXIMIZE
)
91
{
92
if
(result->
mean
>best_result->
mean
)
93
{
94
if
(best_combination)
95
SG_UNREF
(best_combination);
96
97
best_combination=(
CParameterCombination
*)
98
combinations->
get_element
(i);
99
100
SG_UNREF
(best_result);
101
SG_REF
(result);
102
best_result = result;
103
}
104
else
105
{
106
CParameterCombination
* combination=(
CParameterCombination
*)
107
combinations->
get_element
(i);
108
SG_UNREF
(combination);
109
}
110
}
111
else
112
{
113
if
(result->
mean
<best_result->
mean
)
114
{
115
if
(best_combination)
116
SG_UNREF
(best_combination);
117
118
best_combination=(
CParameterCombination
*)
119
combinations->
get_element
(i);
120
121
SG_UNREF
(best_result);
122
SG_REF
(result);
123
best_result = result;
124
}
125
else
126
{
127
CParameterCombination
* combination=(
CParameterCombination
*)
128
combinations->
get_element
(i);
129
SG_UNREF
(combination);
130
}
131
}
132
133
SG_UNREF
(result);
134
SG_UNREF
(current_combination);
135
}
136
137
SG_UNREF
(best_result);
138
SG_UNREF
(machine);
139
SG_UNREF
(combinations);
140
141
return
best_combination;
142
}
143
SHOGUN
Machine Learning Toolbox - Documentation