00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
#include "HyperOptimizer.h"
00044
00045
namespace PLearn {
00046
00047
00048
00049
PLEARN_IMPLEMENT_ABSTRACT_OBJECT(HyperOptimizer,
"ONE LINE DESCR",
"NO HELP");
00050
00051
void
00052 HyperOptimizer::declareOptions(
OptionList &ol)
00053 {
00054
declareOption(ol,
"objective", &HyperOptimizer::objective, OptionBase::buildoption,
00055
"Objective function to be optimized");
00056
declareOption(ol,
"substrategy", &HyperOptimizer::substrategy, OptionBase::buildoption,
00057
"Embedded strategies performed during optimization");
00058 inherited::declareOptions(ol);
00059 }
00060
00061
00062
00063
00064
PLEARN_IMPLEMENT_OBJECT(
HSetVal,
"ONE LINE DESCR",
"NO HELP");
00065
00066
void
00067 HSetVal::declareOptions(
OptionList &ol)
00068 {
00069
declareOption(ol,
"param", &HSetVal::param, OptionBase::buildoption,
00070
"Hyperparameters' alias to set the value for");
00071
declareOption(ol,
"value", &HSetVal::value, OptionBase::buildoption,
00072
"Value of the hyperparameters to be set");
00073 inherited::declareOptions(ol);
00074 }
00075
00076
void
00077 HSetVal::optimize(
PP<Learner> learner,
const VMat &dataset,
const HAliases &aliases)
00078 {
00079
string alias = const_cast<HAliases &>(aliases)[
param];
00080 learner->setOption(alias,
value);
00081 }
00082
00083
00084
00085
00086
PLEARN_IMPLEMENT_OBJECT(
HTryAll,
"ONE LINE DESCR",
"NO HELP");
00087
00088
void
00089 HTryAll::declareOptions(
OptionList &ol)
00090 {
00091
declareOption(ol,
"param", &HTryAll::param, OptionBase::buildoption,
00092
"Hyperparameters' alias to set the values for");
00093
declareOption(ol,
"values", &HTryAll::values, OptionBase::buildoption,
00094
"Values of the hyperparameters to try");
00095 inherited::declareOptions(ol);
00096 }
00097
00098
void
00099 HTryAll::optimize(
PP<Learner> learner,
const VMat &dataset,
const HAliases &aliases)
00100 {
00101
string alias = const_cast<HAliases &>(aliases)[
param];
00102
Vec results(
values.
size());
00103
00104
for (
int k = 0;
k <
values.
size(); ++
k) {
00105 learner->setOption(alias,
values[
k]);
00106 results[
k] = objective->test(learner, dataset);
00107 }
00108 learner->setOption(alias,
values[
argmin(results)]);
00109 }
00110
00111
00112
00113
00114
PLEARN_IMPLEMENT_OBJECT(
HCoordinateDescent,
"ONE LINE DESCR",
"NO HELP");
00115
00116
void
00117 HCoordinateDescent::declareOptions(
OptionList &ol)
00118 {
00119
declareOption(ol,
"substrategy", &HCoordinateDescent::substrategy, OptionBase::buildoption,
00120
"List of HyperOptimizers to perform optimization on");
00121
declareOption(ol,
"max_iterations", &HCoordinateDescent::max_iterations, OptionBase::buildoption,
00122
"Maximum number of iterations to perform");
00123 inherited::declareOptions(ol);
00124 }
00125
00126
void
00127 HCoordinateDescent::optimize(
PP<Learner> learner,
const VMat &dataset,
const HAliases &aliases)
00128 {
00129
for (
int i = 0; i <
max_iterations; ++i)
00130
for (
int j = 0; j < substrategy.
size(); ++j)
00131 substrategy[j]->optimize(learner, dataset, aliases);
00132 }
00133
00134
00135
00136
PLEARN_IMPLEMENT_OBJECT(
HTryCombinations,
"ONE LINE DESCR",
"NO HELP");
00137
00138
void
00139 HTryCombinations::declareOptions(
OptionList &ol)
00140 {
00141
declareOption(ol,
"params", &HTryCombinations::params, OptionBase::buildoption,
00142
"List of hyperparameters to optimize");
00143
declareOption(ol,
"values", &HTryCombinations::values, OptionBase::buildoption,
00144
"List of values to select for optimization of the given hyperparameters");
00145 inherited::declareOptions(ol);
00146 }
00147
00148
void
00149 HTryCombinations::optimize(
PP<Learner> learner,
const VMat &dataset,
const HAliases &aliases)
00150 {
00151
Array<string> str_aliases(
params.
size());
00152
for (
int i = 0; i <
params.
size(); ++i)
00153 str_aliases[i] = const_cast<HAliases &>(aliases)[
params[i]];
00154
recursive_optimize(learner, dataset, str_aliases, 0);
00155 }
00156
00157
real
00158 HTryCombinations::recursive_optimize(
PP<Learner> learner,
const VMat &dataset,
const Array<string> &aliases,
int i)
00159 {
00160
Vec results(
values[i].size());
00161
00162
for (
int k = 0;
k <
values[i].
size(); ++
k) {
00163 learner->setOption(aliases[i], values[i][
k]);
00164
if (i ==
params.
size() - 1)
00165 results[
k] = objective->test(learner, dataset);
00166
else
00167 results[
k] =
recursive_optimize(learner, dataset, aliases, i + 1);
00168 }
00169
int best_k =
argmin(results);
00170 learner->setOption(aliases[i], values[i][best_k]);
00171
return results[best_k];
00172 }
00173
00174
void
00175 HTryCombinations::build()
00176 {
00177 inherited::build();
00178
_build();
00179 }
00180
00181
void
00182 HTryCombinations::_build()
00183 {
00184
if (
values.
size() !=
params.
size())
00185
PLERROR(
"HTryCombinations::_build() - params.size() must match values.size()");
00186 }
00187
00188 };