SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LaplacianInferenceMethodWithLBFGS.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 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  * Code adapted from Gaussian Process Machine Learning Toolbox
31  * http://www.gaussianprocess.org/gpml/code/matlab/doc/
32  */
33 #ifndef CLAPLACIANINFERENCEMETHODWITHLBFGS_H_
34 #define CLAPLACIANINFERENCEMETHODWITHLBFGS_H_
35 
36 #include <shogun/lib/config.h>
37 
38 #ifdef HAVE_EIGEN3
39 
43 
44 
45 namespace shogun
46 {
47 
67 {
68 public:
69  /* default constructor */
71 
72  /* constructor
73  *
74  * @param kernel covariance function
75  * @param features features to use in inference
76  * @param mean mean function
77  * @param labels labels of the features
78  * @param model Likelihood model to use
79  */
81  CFeatures* features,
82  CMeanFunction* mean,
83  CLabels* labels,
84  CLikelihoodModel* model);
85 
87 
88  /* returns the name of the inference method
89  *
90  * @return name LaplacianWithLBFGS
91  */
92  virtual const char* get_name() const
93  {return "LaplacianInferenceMethodWithLBFGS";}
94 
95  /* set L-BFGS parameters
96  * For details please see shogun/optimization/lbfgs/lbfgs.h
97  * @param m The number of corrections to approximate the inverse hessian matrix.
98  * Default value is 100.
99  * @param max_linesearch The maximum number of trials to do line search for each L-BFGS update.
100  * Default value is 1000.
101  * @param linesearch The line search algorithm.
102  * Default value is using the Morethuente line search
103  * @param max_iterations The maximum number of iterations for L-BFGS update.
104  * Default value is 1000.
105  * @param delta Delta for convergence test based on the change of function value.
106  * Default value is 0.
107  * @param past Distance for delta-based convergence test.
108  * Default value is 0.
109  * @param epsilon Epsilon for convergence test based on the change of gradient.
110  * Default value is 1e-5
111  * @param enable_newton_if_fail if LBFGS optimizer fails, should we use Newton method.
112  * Default value is true.
113  * @param min_step The minimum step of the line search.
114  * The default value is 1e-20
115  * @param max_step The maximum step of the line search.
116  * The default value is 1e+20
117  * @param ftol A parameter used in Armijo condition.
118  * Default value is 1e-4
119  * @param wolfe A parameter used in curvature condition.
120  * Default value is 0.9
121  * @param gtol A parameter used in Morethuente linesearch to control the accuracy.
122  * Default value is 0.9
123  * @param xtol The machine precision for floating-point values.
124  * Default value is 1e-16.
125  * @param orthantwise_c Coeefficient for the L1 norm of variables.
126  * This parameter should be set to zero for standard minimization problems.
127  * Setting this parameter to a positive value activates
128  * Orthant-Wise Limited-memory Quasi-Newton (OWL-QN) method. Default value is 0.
129  * @param orthantwise_start Start index for computing L1 norm of the variables.
130  * This parameter is valid only for OWL-QN method. Default value is 0.
131  * @param orthantwise_end End index for computing L1 norm of the variables.
132  * Default value is 1.
133  */
134  virtual void set_lbfgs_parameters(int m = 100,
135  int max_linesearch = 1000,
136  int linesearch = LBFGS_LINESEARCH_DEFAULT,
137  int max_iterations = 1000,
138  float64_t delta = 0.0,
139  int past = 0,
140  float64_t epsilon = 1e-5,
141  bool enable_newton_if_fail = true,
142  float64_t min_step = 1e-20,
143  float64_t max_step = 1e+20,
144  float64_t ftol = 1e-4,
145  float64_t wolfe = 0.9,
146  float64_t gtol = 0.9,
147  float64_t xtol = 1e-16,
148  float64_t orthantwise_c = 0.0,
149  int orthantwise_start = 0,
150  int orthantwise_end = 1);
151 
152 
153 protected:
154  /* update alpha using the LBFGS method*/
155  virtual void update_alpha();
156 
157 private:
158  /* a parameter used to compute function value and gradient for LBFGS update*/
159  SGVector<float64_t> * m_mean_f;
160 
161  /* should we enable the original Newton method
162  * if the L-BFGS method fails
163  * */
164  bool m_enable_newton_if_fail;
165 
166  /* The number of corrections to approximate the inverse hessian matrix.*/
167  int m_m;
168 
169  /* The maximum number of trials to do line search for each L-BFGS update.*/
170  int m_max_linesearch;
171 
172  /* The line search algorithm.*/
173  int m_linesearch;
174 
175  /* The maximum number of iterations for L-BFGS update.*/
176  int m_max_iterations;
177 
178  /* Delta for convergence test based on the change of function value.*/
179  float64_t m_delta;
180 
181  /* Distance for delta-based convergence test.*/
182  int m_past;
183 
184  /* Epsilon for convergence test based on the change of gradient.*/
185  float64_t m_epsilon;
186 
187  /* The minimum step of the line search.*/
188  float64_t m_min_step;
189 
190  /* The maximum step of the line search.*/
191  float64_t m_max_step;
192 
193  /* A parameter used in Armijo condition.*/
194  float64_t m_ftol;
195 
196  /* A parameter used in curvature condition.*/
197  float64_t m_wolfe;
198 
199  /* A parameter used in Morethuente linesearch to control the accuracy.*/
200  float64_t m_gtol;
201 
202  /* The machine precision for floating-point values.*/
203  float64_t m_xtol;
204 
205  /* Coeefficient for the L1 norm of variables.*/
206  float64_t m_orthantwise_c;
207 
208  /* Start index for computing L1 norm of the variables.*/
209  int m_orthantwise_start;
210 
211  /* End index for computing L1 norm of the variables.*/
212  int m_orthantwise_end;
213 
214  void init();
215 
216  /* helper function is passed to the LBFGS API
217  * Note that this function should be static and
218  * private.
219  * */
220  static float64_t evaluate(void *obj,
221  const float64_t *alpha,
222  float64_t *gradient,
223  const int dim,
224  const float64_t step);
225 
226  /* compute the gradient given the current alpha*/
227  void get_gradient_wrt_alpha(Eigen::Map<Eigen::VectorXd>* alpha,
228  Eigen::Map<Eigen::VectorXd>* gradient);
229 
230  /* compute the function value given the current alpha*/
231  void get_psi_wrt_alpha(Eigen::Map<Eigen::VectorXd>* alpha,
232  float64_t* psi);
233 };
234 
235 } /* namespace shogun */
236 #endif /* HAVE_EIGEN3 */
237 #endif /* CLAPLACIANINFERENCEMETHODWITHLBFGS_H_ */

SHOGUN Machine Learning Toolbox - Documentation