SHOGUN  4.1.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  */
31 #include <shogun/lib/config.h>
32 #include <algorithm>
34 
35 using namespace shogun;
36 
39 {
40  init();
41 }
42 
44 {
45 }
46 
49 {
50  init();
51 }
52 
53 void LBFGSMinimizer::init()
54 {
56 }
57 
59  int m,
60  int max_linesearch,
61  ELBFGSLineSearch linesearch,
62  int max_iterations,
64  int past,
66  float64_t min_step,
67  float64_t max_step,
68  float64_t ftol,
69  float64_t wolfe,
70  float64_t gtol,
71  float64_t xtol,
72  float64_t orthantwise_c,
73  int orthantwise_start,
74  int orthantwise_end)
75 {
76  m_m = m;
77  m_max_linesearch = max_linesearch;
78  m_linesearch = linesearch;
79  m_max_iterations = max_iterations;
80  m_delta = delta;
81  m_past = past;
83  m_min_step = min_step;
84  m_max_step = max_step;
85  m_ftol = ftol;
86  m_wolfe = wolfe;
87  m_gtol = gtol;
88  m_xtol = xtol;
89  m_orthantwise_c = orthantwise_c;
90  m_orthantwise_start = orthantwise_start;
91  m_orthantwise_end = orthantwise_end;
92 }
93 
95 {
96  REQUIRE(m_fun, "Cost function not set!\n");
98  REQUIRE(m_target_variable.vlen>0,"Target variable from cost function must not empty!\n");
99 }
100 
102 {
103  lbfgs_parameter_t lbfgs_param;
104  lbfgs_param.m = m_m;
105  lbfgs_param.max_linesearch = m_max_linesearch;
106  lbfgs_param.linesearch = m_linesearch;
107  lbfgs_param.max_iterations = m_max_iterations;
108  lbfgs_param.delta = m_delta;
109  lbfgs_param.past = m_past;
110  lbfgs_param.epsilon = m_epsilon;
111  lbfgs_param.min_step = m_min_step;
112  lbfgs_param.max_step = m_max_step;
113  lbfgs_param.ftol = m_ftol;
114  lbfgs_param.wolfe = m_wolfe;
115  lbfgs_param.gtol = m_gtol;
116  lbfgs_param.xtol = m_xtol;
117  lbfgs_param.orthantwise_c = m_orthantwise_c;
119  lbfgs_param.orthantwise_end = m_orthantwise_end;
120 
122 
123  float64_t cost=0.0;
125  &cost, LBFGSMinimizer::evaluate,
126  NULL, this, &lbfgs_param);
127 
128  if(error_code!=0 && error_code!=LBFGS_ALREADY_MINIMIZED)
129  {
130  SG_SWARNING("Error(s) happened during L-BFGS optimization (error code:%d)\n",
131  error_code);
132  }
133 
134  return cost;
135 }
136 
137 float64_t LBFGSMinimizer::evaluate(void *obj, const float64_t *variable,
138  float64_t *gradient, const int dim, const float64_t step)
139 {
140  /* Note that parameters = parameters_pre_iter - step * gradient_pre_iter */
141  LBFGSMinimizer * obj_prt
142  = static_cast<LBFGSMinimizer *>(obj);
143 
144  REQUIRE(obj_prt, "The instance object passed to L-BFGS optimizer should not be NULL\n");
145 
146  //get the gradient wrt variable_new
147  SGVector<float64_t> grad=obj_prt->m_fun->get_gradient();
148  REQUIRE(grad.vlen==dim,
149  "The length of gradient (%d) and the length of variable (%d) do not match\n",
150  grad.vlen,dim);
151 
152  std::copy(grad.vector,grad.vector+dim,gradient);
153 
154  float64_t cost=obj_prt->m_fun->get_cost();
155  return cost;
156 }
virtual SGVector< float64_t > get_gradient()=0
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
#define SG_SWARNING(...)
Definition: SGIO.h:178
float64_t orthantwise_c
Definition: lbfgs.h:311
FirstOrderCostFunction * m_fun
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual void init_minimization()
virtual float64_t minimize()
static const float64_t epsilon
Definition: libbmrm.cpp:25
index_t vlen
Definition: SGVector.h:494
virtual void set_lbfgs_parameters(int m=100, int max_linesearch=1000, ELBFGSLineSearch linesearch=BACKTRACKING_STRONG_WOLFE, int max_iterations=1000, float64_t delta=0.0, int 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, int orthantwise_start=0, int orthantwise_end=1)
SGVector< float64_t > m_target_variable
double float64_t
Definition: common.h:50
The first order cost function base class.
virtual float64_t get_cost()=0
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class wraps the Shogun's C-style LBFGS minimizer.
virtual SGVector< float64_t > obtain_variable_reference()=0
#define delta
Definition: sfa.cpp:23
The first order minimizer base class.

SHOGUN Machine Learning Toolbox - Documentation