-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GradientModelSelection.cpp
382 lines (311 loc) · 9.89 KB
/
GradientModelSelection.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* * 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) 2016 Wu Lin
* Written (W) 2013 Roman Votyakov
* Copyright (C) 2012 Jacob Walker
*/
#include <shogun/modelselection/GradientModelSelection.h>
#include <shogun/evaluation/GradientResult.h>
#include <shogun/modelselection/ParameterCombination.h>
#include <shogun/modelselection/ModelSelectionParameters.h>
#include <shogun/machine/Machine.h>
#include <shogun/optimization/FirstOrderCostFunction.h>
#include <shogun/optimization/lbfgs/LBFGSMinimizer.h>
#include <shogun/mathematics/Math.h>
using namespace shogun;
namespace shogun
{
#ifndef DOXYGEN_SHOULD_SKIP_THIS
class GradientModelSelectionCostFunction: public FirstOrderCostFunction
{
public:
GradientModelSelectionCostFunction():FirstOrderCostFunction() { init(); }
virtual ~GradientModelSelectionCostFunction() { SG_UNREF(m_obj); }
void set_target(CGradientModelSelection *obj)
{
REQUIRE(obj,"Obj must set\n");
if(m_obj!=obj)
{
SG_REF(obj);
SG_UNREF(m_obj);
m_obj=obj;
}
}
void unset_target(bool is_unref)
{
if(is_unref)
{
SG_UNREF(m_obj);
}
m_obj=NULL;
}
virtual float64_t get_cost()
{
REQUIRE(m_obj,"Object not set\n");
return m_obj->get_cost(m_val, m_grad, m_func_data);
}
virtual SGVector<float64_t> obtain_variable_reference()
{
REQUIRE(m_obj,"Object not set\n");
return m_val;
}
virtual SGVector<float64_t> get_gradient()
{
REQUIRE(m_obj,"Object not set\n");
return m_grad;
}
virtual const char* get_name() const { return "GradientModelSelectionCostFunction"; }
virtual void set_func_data(void *func_data)
{
REQUIRE(func_data != NULL, "func_data must set\n");
m_func_data = func_data;
}
virtual void set_variables(SGVector<float64_t> val)
{
m_val = SGVector<float64_t>(val.vlen);
m_grad = SGVector<float64_t>(val.vlen);
std::copy(val.vector,val.vector+val.vlen,m_val.vector);
}
private:
void init()
{
m_obj=NULL;
SG_ADD((CSGObject **)&m_obj, "GradientModelSelectionCostFunction__m_obj",
"obj in GradientModelSelectionCostFunction", MS_NOT_AVAILABLE);
m_func_data = NULL;
m_val = SGVector<float64_t>();
SG_ADD(
&m_val, "GradientModelSelectionCostFunction__m_val",
"val in GradientModelSelectionCostFunction", MS_NOT_AVAILABLE);
m_grad = SGVector<float64_t>();
SG_ADD(
&m_grad, "GradientModelSelectionCostFunction__m_grad",
"grad in GradientModelSelectionCostFunction", MS_NOT_AVAILABLE);
}
CGradientModelSelection *m_obj;
void* m_func_data;
SGVector<float64_t> m_val;
SGVector<float64_t> m_grad;
};
/** structure used for NLopt callback function */
struct nlopt_params
{
/** pointer to current combination */
CParameterCombination* current_combination;
/** pointer to parmeter dictionary */
CMap<TParameter*, CSGObject*>* parameter_dictionary;
/** do we want to print the state? */
bool print_state;
};
float64_t CGradientModelSelection::get_cost(SGVector<float64_t> model_vars, SGVector<float64_t> model_grads, void* func_data)
{
REQUIRE(func_data!=NULL, "func_data must set\n");
REQUIRE(model_vars.vlen==model_grads.vlen, "length of variable (%d) and gradient (%d) must equal\n",
model_vars.vlen, model_grads.vlen);
nlopt_params* params=(nlopt_params*)func_data;
CParameterCombination* current_combination=params->current_combination;
CMap<TParameter*, CSGObject*>* parameter_dictionary=params->parameter_dictionary;
bool print_state=params->print_state;
index_t offset=0;
// set parameters from vector model_vars
for (index_t i=0; i<parameter_dictionary->get_num_elements(); i++)
{
CMapNode<TParameter*, CSGObject*>* node=parameter_dictionary->get_node_ptr(i);
TParameter* param=node->key;
CSGObject* parent=node->data;
if (param->m_datatype.m_ctype==CT_VECTOR ||
param->m_datatype.m_ctype==CT_SGVECTOR ||
param->m_datatype.m_ctype==CT_SGMATRIX ||
param->m_datatype.m_ctype==CT_MATRIX)
{
for (index_t j=0; j<param->m_datatype.get_num_elements(); j++)
{
bool result=current_combination->set_parameter(param->m_name,
model_vars[offset++], parent, j);
REQUIRE(result, "Parameter %s not found in combination tree\n",
param->m_name)
}
}
else
{
bool result=current_combination->set_parameter(param->m_name,
model_vars[offset++], parent);
REQUIRE(result, "Parameter %s not found in combination tree\n",
param->m_name)
}
}
// apply current combination to the machine
CMachine* machine=m_machine_eval->get_machine();
current_combination->apply_to_machine(machine);
if (print_state)
{
SG_SPRINT("Current combination\n");
current_combination->print_tree();
}
SG_UNREF(machine);
// evaluate the machine
CEvaluationResult* evaluation_result=m_machine_eval->evaluate();
CGradientResult* gradient_result=CGradientResult::obtain_from_generic(
evaluation_result);
SG_UNREF(evaluation_result);
if (print_state)
{
SG_SPRINT("Current result\n");
gradient_result->print_result();
}
// get value of the function, gradients and parameter dictionary
SGVector<float64_t> value=gradient_result->get_value();
float64_t cost = SGVector<float64_t>::sum(value);
if (CMath::is_nan(cost) || CMath::is_infinity(cost))
{
if (m_machine_eval->get_evaluation_direction()==ED_MINIMIZE)
return cost;
else
return -cost;
}
CMap<TParameter*, SGVector<float64_t> >* gradient=gradient_result->get_gradient();
CMap<TParameter*, CSGObject*>* gradient_dictionary=
gradient_result->get_paramter_dictionary();
SG_UNREF(gradient_result);
offset=0;
// set derivative for each parameter from parameter dictionary
for (index_t i=0; i<parameter_dictionary->get_num_elements(); i++)
{
CMapNode<TParameter*, CSGObject*>* node=parameter_dictionary->get_node_ptr(i);
SGVector<float64_t> derivative;
for (index_t j=0; j<gradient_dictionary->get_num_elements(); j++)
{
CMapNode<TParameter*, CSGObject*>* gradient_node=
gradient_dictionary->get_node_ptr(j);
if (gradient_node->data==node->data &&
!strcmp(gradient_node->key->m_name, node->key->m_name))
{
derivative=gradient->get_element(gradient_node->key);
}
}
REQUIRE(derivative.vlen, "Can't find gradient wrt %s parameter!\n",
node->key->m_name);
sg_memcpy(model_grads.vector+offset, derivative.vector, sizeof(float64_t)*derivative.vlen);
offset+=derivative.vlen;
}
SG_UNREF(gradient);
SG_UNREF(gradient_dictionary);
if (m_machine_eval->get_evaluation_direction()==ED_MINIMIZE)
{
return cost;
}
else
{
model_grads.scale(-1);
return -cost;
}
}
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
void CGradientModelSelection::set_minimizer(FirstOrderMinimizer* minimizer)
{
REQUIRE(minimizer!=NULL, "Minimizer must set\n");
SG_REF(minimizer);
SG_UNREF(m_mode_minimizer);
m_mode_minimizer=minimizer;
}
CGradientModelSelection::CGradientModelSelection() : CModelSelection()
{
init();
}
CGradientModelSelection::CGradientModelSelection(CMachineEvaluation* machine_eval,
CModelSelectionParameters* model_parameters)
: CModelSelection(machine_eval, model_parameters)
{
init();
}
CGradientModelSelection::~CGradientModelSelection()
{
SG_UNREF(m_mode_minimizer);
}
void CGradientModelSelection::init()
{
m_mode_minimizer = new CLBFGSMinimizer();
SG_REF(m_mode_minimizer);
SG_ADD((CSGObject **)&m_mode_minimizer,
"mode_minimizer", "Minimizer used in mode selection", MS_NOT_AVAILABLE);
}
CParameterCombination* CGradientModelSelection::select_model(bool print_state)
{
if (!m_model_parameters)
{
CMachine* machine=m_machine_eval->get_machine();
CParameterCombination* current_combination=new CParameterCombination(machine);
SG_REF(current_combination);
if (print_state)
{
SG_PRINT("Initial combination:\n");
current_combination->print_tree();
}
// get total length of variables
index_t total_variables=current_combination->get_parameters_length();
// build parameter->value map
CMap<TParameter*, SGVector<float64_t> >* argument=
new CMap<TParameter*, SGVector<float64_t> >();
current_combination->build_parameter_values_map(argument);
// unroll current parameter combination into vector
SGVector<float64_t> model_vars = SGVector<float64_t>(total_variables);
index_t offset=0;
for (index_t i=0; i<argument->get_num_elements(); i++)
{
CMapNode<TParameter*, SGVector<float64_t> >* node=argument->get_node_ptr(i);
sg_memcpy(model_vars.vector+offset, node->data.vector, sizeof(float64_t)*node->data.vlen);
offset+=node->data.vlen;
}
SG_UNREF(argument);
// build parameter->sgobject map from current parameter combination
CMap<TParameter*, CSGObject*>* parameter_dictionary=
new CMap<TParameter*, CSGObject*>();
current_combination->build_parameter_parent_map(parameter_dictionary);
//data for computing the gradient
nlopt_params params;
params.current_combination=current_combination;
params.print_state=print_state;
params.parameter_dictionary=parameter_dictionary;
// choose evaluation direction (minimize or maximize objective function)
if (print_state)
{
if (m_machine_eval->get_evaluation_direction()==ED_MINIMIZE)
{
SG_PRINT("Minimizing objective function:\n");
}
else
{
SG_PRINT("Maximizing objective function:\n");
}
}
GradientModelSelectionCostFunction *cost_fun=new GradientModelSelectionCostFunction();
cost_fun->set_target(this);
cost_fun->set_variables(model_vars);
cost_fun->set_func_data(¶ms);
bool cleanup=false;
if(this->ref_count()>1)
cleanup=true;
m_mode_minimizer->set_cost_function(cost_fun);
m_mode_minimizer->minimize();
m_mode_minimizer->unset_cost_function(false);
cost_fun->unset_target(cleanup);
SG_UNREF(cost_fun);
if (print_state)
{
SG_PRINT("Best combination:\n");
current_combination->print_tree();
}
SG_UNREF(machine);
SG_UNREF(parameter_dictionary);
return current_combination;
}
else
{
SG_NOTIMPLEMENTED
return NULL;
}
}
}