SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
AdaptMomentumCorrection.h
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 
32 #ifndef ADAPTMOMEMTUMCORRECTION_H
33 #define ADAPTMOMEMTUMCORRECTION_H
34 #include <shogun/lib/config.h>
35 #include <shogun/lib/SGVector.h>
37 namespace shogun
38 {
51 {
52 public:
53 
54  /* Constructor */
57  {
58  init();
59  }
60 
65  virtual void set_momentum_correction(MomentumCorrection* correction)
66  {
67  REQUIRE(correction,"MomentumCorrection must not NULL\n");
68  REQUIRE(correction != this, "MomentumCorrection can not be itself\n");
69  m_momentum_correction=correction;
70  }
71 
72  /* Destructor */
74 
79  virtual bool is_initialized()
80  {
81  REQUIRE(m_momentum_correction,"MomentumCorrection must set\n");
83  }
84 
89  virtual void set_correction_weight(float64_t weight)
90  {
91  REQUIRE(m_momentum_correction,"MomentumCorrection must set\n");
93  }
94 
100  {
101  REQUIRE(m_momentum_correction,"MomentumCorrection must set\n");
103  }
104 
111  virtual DescendPair get_corrected_descend_direction(float64_t negative_descend_direction,
112  index_t idx)
113  {
114  REQUIRE(m_momentum_correction,"MomentumCorrection must set\n");
115  REQUIRE(m_adapt_rate>0 && m_adapt_rate<1.0,"adaptive rate is invalid\n");
117  REQUIRE(idx>=0 && idx<len,
118  "The index (%d) is invalid\n", idx);
119  if(m_descend_rate.vlen==0)
120  {
123  }
124  float64_t rate=m_descend_rate[idx];
126  DescendPair pair=m_momentum_correction->get_corrected_descend_direction(rate*negative_descend_direction, idx);
128  if(pre*cur>0.0)
129  rate*=(1.0+m_adapt_rate);
130  else
131  {
132  if(pre*cur==0.0 && (cur>0.0 || pre>0.0))
133  rate*=(1.0+m_adapt_rate);
134  else
135  rate*=(1.0-m_adapt_rate);
136  }
137  if(rate<m_rate_min)
138  rate=m_rate_min;
139  if(rate>m_rate_max)
140  rate=m_rate_max;
141  m_descend_rate[idx]=rate;
142  return pair;
143  }
144 
153  virtual void update_context(CMinimizerContext* context)
154  {
155  REQUIRE(m_momentum_correction,"MomentumCorrection must set\n");
157  REQUIRE(context, "context must set\n");
159  std::copy(m_descend_rate.vector,
161  value.vector);
162  std::string key="AdaptMomentumCorrection::m_descend_rate";
163  context->save_data(key, value);
164  }
165 
173  virtual void load_from_context(CMinimizerContext* context)
174  {
175  REQUIRE(m_momentum_correction,"MomentumCorrection must set\n");
177  REQUIRE(context, "context must set\n");
178  std::string key="AdaptMomentumCorrection::m_descend_rate";
181  std::copy(value.vector, value.vector+value.vlen,
183  }
184 
191  virtual void set_adapt_rate(float64_t adapt_rate, float64_t rate_min=0.0, float64_t rate_max=CMath::INFTY)
192  {
193  REQUIRE(adapt_rate>0.0 && adapt_rate<1.0, "Adaptive rate (%f) must in (0,1)\n", adapt_rate);
194  REQUIRE(rate_min>=0, "Minimum speedup rate (%f) must be non-negative\n", rate_min);
195  REQUIRE(rate_max>rate_min, "Maximum speedup rate (%f) must greater than minimum speedup rate (%f)\n",
196  rate_max, rate_min);
197  m_adapt_rate=adapt_rate;
198  m_rate_min=rate_min;
199  m_rate_max=rate_max;
200  }
201 
206  virtual void set_init_descend_rate(float64_t init_descend_rate)
207  {
208  REQUIRE(init_descend_rate>0,"Init speedup rate (%f) must be positive\n", init_descend_rate);
209  m_init_descend_rate=init_descend_rate;
210  }
211 protected:
224 private:
226  void init()
227  {
228  m_momentum_correction=NULL;
229  m_descend_rate=SGVector<float64_t>();
230  m_adapt_rate=0;
231  m_rate_min=0;
232  m_rate_max=CMath::INFTY;
233  m_init_descend_rate=1.0;
234  }
235 };
236 
237 }
238 #endif
virtual void save_data(const std::string &key, SGVector< float64_t > value)
virtual float64_t get_previous_descend_direction(index_t idx)
int32_t index_t
Definition: common.h:62
The class is used to serialize and deserialize variables for the optimization framework.
virtual float64_t get_length_previous_descend_direction()
static const float64_t INFTY
infinity
Definition: Math.h:2048
virtual void update_context(CMinimizerContext *context)
virtual void set_momentum_correction(MomentumCorrection *correction)
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual void initialize_previous_direction(index_t len)
virtual SGVector< float64_t > get_data_sgvector_float64(const std::string &key)
virtual void set_adapt_rate(float64_t adapt_rate, float64_t rate_min=0.0, float64_t rate_max=CMath::INFTY)
virtual DescendPair get_corrected_descend_direction(float64_t negative_descend_direction, index_t idx)
virtual void set_correction_weight(float64_t weight)
index_t vlen
Definition: SGVector.h:494
virtual void update_context(CMinimizerContext *context)
double float64_t
Definition: common.h:50
virtual void load_from_context(CMinimizerContext *context)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void initialize_previous_direction(index_t len)
This is a base class for momentum correction methods.
virtual void set_init_descend_rate(float64_t init_descend_rate)
virtual void load_from_context(CMinimizerContext *context)
virtual void set_correction_weight(float64_t weight)
This implements the adaptive momentum correction method.
virtual DescendPair get_corrected_descend_direction(float64_t negative_descend_direction, index_t idx)=0
void set_const(T const_elem)
Definition: SGVector.cpp:152

SHOGUN Machine Learning Toolbox - Documentation