SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LBFGSMinimizer.cpp
Go to the documentation of this file.
1  /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2015 Wu Lin
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  *
30  */
33 #include <shogun/base/Parameter.h>
34 
35 namespace shogun
36 {
39 {
40  init();
41 }
42 
44 {
45 }
46 
49 {
50  FirstOrderBoundConstraintsCostFunction* bound_constraints_fun
52  if(m_fun && bound_constraints_fun)
53  {
54  SG_SWARNING("The minimizer does not support constrained minimization. All constraints will be ignored.\n")
55  }
56  init();
57 }
58 
59 void CLBFGSMinimizer::init()
60 {
62  m_min_step=1e-6;
63  m_xtol=1e-6;
64  SG_ADD(&m_linesearch_id, "CLBFGSMinimizer__m_linesearch_id",
65  "linesearch_id in CLBFGSMinimizer", MS_NOT_AVAILABLE);
66  SG_ADD(&m_m, "CLBFGSMinimizer__m_m",
67  "m in CLBFGSMinimizer", MS_NOT_AVAILABLE);
68  SG_ADD(&m_max_linesearch, "CLBFGSMinimizer__m_max_linesearch",
69  "max_linesearch in CLBFGSMinimizer", MS_NOT_AVAILABLE);
70  SG_ADD(&m_max_iterations, "CLBFGSMinimizer__m_max_iterations",
71  "max_iterations in CLBFGSMinimizer", MS_NOT_AVAILABLE);
72  SG_ADD(&m_delta, "CLBFGSMinimizer__m_delta",
73  "delta in CLBFGSMinimizer", MS_NOT_AVAILABLE);
74  SG_ADD(&m_past, "CLBFGSMinimizer__m_past",
75  "past in CLBFGSMinimizer", MS_NOT_AVAILABLE);
76  SG_ADD(&m_epsilon, "CLBFGSMinimizer__m_epsilon",
77  "epsilon in CLBFGSMinimizer", MS_NOT_AVAILABLE);
78  SG_ADD(&m_min_step, "CLBFGSMinimizer__m_min_step",
79  "min_step in CLBFGSMinimizer", MS_NOT_AVAILABLE);
80  SG_ADD(&m_max_step, "CLBFGSMinimizer__m_max_step",
81  "max_step in CLBFGSMinimizer", MS_NOT_AVAILABLE);
82  SG_ADD(&m_ftol, "CLBFGSMinimizer__m_ftol",
83  "ftol in CLBFGSMinimizer", MS_NOT_AVAILABLE);
84  SG_ADD(&m_wolfe, "CLBFGSMinimizer__m_wolfe",
85  "wolfe in CLBFGSMinimizer", MS_NOT_AVAILABLE);
86  SG_ADD(&m_gtol, "CLBFGSMinimizer__m_gtol",
87  "gtol in CLBFGSMinimizer", MS_NOT_AVAILABLE);
88  SG_ADD(&m_xtol, "CLBFGSMinimizer__m_xtol",
89  "xtol in CLBFGSMinimizer", MS_NOT_AVAILABLE);
90  SG_ADD(&m_orthantwise_c, "CLBFGSMinimizer__m_orthantwise_c",
91  "orthantwise_c in CLBFGSMinimizer", MS_NOT_AVAILABLE);
92  SG_ADD(&m_orthantwise_start, "CLBFGSMinimizer__m_orthantwise_start",
93  "orthantwise_start in CLBFGSMinimizer", MS_NOT_AVAILABLE);
94  SG_ADD(&m_orthantwise_end, "CLBFGSMinimizer__m_orthantwise_end",
95  "orthantwise_end in CLBFGSMinimizer", MS_NOT_AVAILABLE);
96  SG_ADD(&m_target_variable, "CLBFGSMinimizer__m_target_variable",
97  "m_target_variable in CLBFGSMinimizer", MS_NOT_AVAILABLE);
98 }
99 
101  int32_t m,
102  int32_t max_linesearch,
103  ELBFGSLineSearch linesearch,
104  int32_t max_iterations,
105  float64_t delta,
106  int32_t past,
107  float64_t epsilon,
108  float64_t min_step,
109  float64_t max_step,
110  float64_t ftol,
111  float64_t wolfe,
112  float64_t gtol,
113  float64_t xtol,
114  float64_t orthantwise_c,
115  int32_t orthantwise_start,
116  int32_t orthantwise_end)
117 {
118  m_m = m;
119  m_max_linesearch = max_linesearch;
120  m_linesearch_id = LBFGSLineSearchHelper::get_lbfgs_linear_search_id(linesearch);
121  m_max_iterations = max_iterations;
122  m_delta = delta;
123  m_past = past;
124  m_epsilon = epsilon;
125  m_min_step = min_step;
126  m_max_step = max_step;
127  m_ftol = ftol;
128  m_wolfe = wolfe;
129  m_gtol = gtol;
130  m_xtol = xtol;
131  m_orthantwise_c = orthantwise_c;
132  m_orthantwise_start = orthantwise_start;
133  m_orthantwise_end = orthantwise_end;
134 }
135 
137 {
138  REQUIRE(m_fun, "Cost function not set!\n");
140  REQUIRE(m_target_variable.vlen>0,"Target variable from cost function must not empty!\n");
141 }
142 
144 {
145  lbfgs_parameter_t lbfgs_param;
146  lbfgs_param.m = m_m;
147  lbfgs_param.max_linesearch = m_max_linesearch;
148  lbfgs_param.linesearch = LBFGSLineSearchHelper::get_lbfgs_linear_search(m_linesearch_id);
149  lbfgs_param.max_iterations = m_max_iterations;
150  lbfgs_param.delta = m_delta;
151  lbfgs_param.past = m_past;
152  lbfgs_param.epsilon = m_epsilon;
153  lbfgs_param.min_step = m_min_step;
154  lbfgs_param.max_step = m_max_step;
155  lbfgs_param.ftol = m_ftol;
156  lbfgs_param.wolfe = m_wolfe;
157  lbfgs_param.gtol = m_gtol;
158  lbfgs_param.xtol = m_xtol;
159  lbfgs_param.orthantwise_c = m_orthantwise_c;
161  lbfgs_param.orthantwise_end = m_orthantwise_end;
162 
164 
165  float64_t cost=0.0;
167  &cost, CLBFGSMinimizer::evaluate,
168  NULL, this, &lbfgs_param);
169 
170  if(error_code!=0 && error_code!=LBFGS_ALREADY_MINIMIZED)
171  {
172  SG_SWARNING("Error(s) happened during L-BFGS optimization (error code:%d)\n",
173  error_code);
174  }
175 
176  return cost;
177 }
178 
179 float64_t CLBFGSMinimizer::evaluate(void *obj, const float64_t *variable,
180  float64_t *gradient, const int32_t dim, const float64_t step)
181 {
182  /* Note that parameters = parameters_pre_iter - step * gradient_pre_iter */
183  CLBFGSMinimizer * obj_prt
184  = static_cast<CLBFGSMinimizer *>(obj);
185 
186  REQUIRE(obj_prt, "The instance object passed to L-BFGS optimizer should not be NULL\n");
187 
188  float64_t cost=obj_prt->m_fun->get_cost();
189 
190  if (CMath::is_nan(cost) || CMath::is_infinity(cost))
191  return cost;
192 
193  //get the gradient wrt variable_new
194  SGVector<float64_t> grad=obj_prt->m_fun->get_gradient();
195  REQUIRE(grad.vlen==dim,
196  "The length of gradient (%d) and the length of variable (%d) do not match\n",
197  grad.vlen,dim);
198 
199  std::copy(grad.vector,grad.vector+dim,gradient);
200  return cost;
201 }
202 
203 }
virtual SGVector< float64_t > get_gradient()=0
virtual float64_t minimize()
int32_t lbfgs(int32_t n, float64_t *x, float64_t *ptr_fx, lbfgs_evaluate_t proc_evaluate, lbfgs_progress_t proc_progress, void *instance, lbfgs_parameter_t *_param, lbfgs_adjust_step_t proc_adjust_step)
Definition: lbfgs.cpp:208
virtual void init_minimization()
#define SG_SWARNING(...)
Definition: SGIO.h:178
float64_t orthantwise_c
Definition: lbfgs.h:313
FirstOrderCostFunction * m_fun
#define REQUIRE(x,...)
Definition: SGIO.h:206
The first order cost function base class with bound constrains.
The class wraps the Shogun's C-style LBFGS minimizer.
index_t vlen
Definition: SGVector.h:494
double float64_t
Definition: common.h:50
The first order cost function base class.
virtual float64_t get_cost()=0
virtual void set_lbfgs_parameters(int32_t m=100, int32_t max_linesearch=1000, ELBFGSLineSearch linesearch=BACKTRACKING_STRONG_WOLFE, int32_t max_iterations=1000, float64_t delta=0.0, int32_t past=0, float64_t epsilon=1e-5, float64_t min_step=1e-20, float64_t max_step=1e+20, float64_t ftol=1e-4, float64_t wolfe=0.9, float64_t gtol=0.9, float64_t xtol=1e-16, float64_t orthantwise_c=0.0, int32_t orthantwise_start=0, int32_t orthantwise_end=1)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static int is_infinity(double f)
checks whether a float is infinity
Definition: Math.cpp:247
static int is_nan(double f)
checks whether a float is nan
Definition: Math.cpp:234
ELBFGSLineSearch
Definition: lbfgscommon.h:13
#define SG_ADD(...)
Definition: SGObject.h:84
virtual SGVector< float64_t > obtain_variable_reference()=0
SGVector< float64_t > m_target_variable
The first order minimizer base class.

SHOGUN Machine Learning Toolbox - Documentation