-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GridSearchModelSelection.cpp
100 lines (82 loc) · 2.61 KB
/
GridSearchModelSelection.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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/
#include <shogun/modelselection/GridSearchModelSelection.h>
#include <shogun/modelselection/ParameterCombination.h>
#include <shogun/modelselection/ModelSelectionParameters.h>
#include <shogun/evaluation/CrossValidation.h>
#include <shogun/machine/Machine.h>
using namespace shogun;
CGridSearchModelSelection::CGridSearchModelSelection() :
CModelSelection(NULL, NULL)
{
}
CGridSearchModelSelection::CGridSearchModelSelection(
CModelSelectionParameters* model_parameters,
CCrossValidation* cross_validation) :
CModelSelection(model_parameters, cross_validation)
{
}
CGridSearchModelSelection::~CGridSearchModelSelection()
{
}
CParameterCombination* CGridSearchModelSelection::select_model()
{
/* Retrieve all possible parameter combinations */
CDynamicObjectArray<CParameterCombination>* combinations=
m_model_parameters->get_combinations();
CrossValidationResult best_result;
CParameterCombination* best_combination=NULL;
if (m_cross_validation->get_evaluation_direction()==ED_MAXIMIZE)
best_result.mean=CMath::ALMOST_NEG_INFTY;
else
best_result.mean=CMath::ALMOST_INFTY;
/* apply all combinations and search for best one */
for (index_t i=0; i<combinations->get_num_elements(); ++i)
{
CParameterCombination* current_combination=combinations->get_element(i);
current_combination->apply_to_parameter(
m_cross_validation->get_machine_parameters());
CrossValidationResult result=m_cross_validation->evaluate();
/* check if current result is better, delete old combinations */
if (m_cross_validation->get_evaluation_direction()==ED_MAXIMIZE)
{
if (result.mean>best_result.mean)
{
if (best_combination)
SG_UNREF(best_combination);
best_combination=combinations->get_element(i);
best_result=result;
}
else
{
CParameterCombination* combination=combinations->get_element(i);
SG_UNREF(combination);
}
}
else
{
if (result.mean<best_result.mean)
{
if (best_combination)
SG_UNREF(best_combination);
best_combination=combinations->get_element(i);
best_result=result;
}
else
{
CParameterCombination* combination=combinations->get_element(i);
SG_UNREF(combination);
}
}
SG_UNREF(current_combination);
}
SG_UNREF(combinations);
return best_combination;
}