amsgrad_update.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_OPTIMIZERS_AMS_GRAD_AMS_GRAD_UPDATE_HPP
14 #define MLPACK_CORE_OPTIMIZERS_AMS_GRAD_AMS_GRAD_UPDATE_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace optimization {
20 
38 {
39  public:
48  AMSGradUpdate(const double epsilon = 1e-8,
49  const double beta1 = 0.9,
50  const double beta2 = 0.999) :
51  epsilon(epsilon),
52  beta1(beta1),
53  beta2(beta2),
54  iteration(0)
55  {
56  // Nothing to do.
57  }
58 
66  void Initialize(const size_t rows, const size_t cols)
67  {
68  m = arma::zeros<arma::mat>(rows, cols);
69  v = arma::zeros<arma::mat>(rows, cols);
70  vImproved = arma::zeros<arma::mat>(rows, cols);
71  }
72 
80  void Update(arma::mat& iterate,
81  const double stepSize,
82  const arma::mat& gradient)
83  {
84  // Increment the iteration counter variable.
85  ++iteration;
86 
87  // And update the iterate.
88  m *= beta1;
89  m += (1 - beta1) * gradient;
90 
91  v *= beta2;
92  v += (1 - beta2) * (gradient % gradient);
93 
94  const double biasCorrection1 = 1.0 - std::pow(beta1, iteration);
95  const double biasCorrection2 = 1.0 - std::pow(beta2, iteration);
96 
97  // Element wise maximum of past and present squared gradients.
98  vImproved = arma::max(vImproved, v);
99 
100  iterate -= (stepSize * std::sqrt(biasCorrection2) / biasCorrection1) *
101  m / (arma::sqrt(vImproved) + epsilon);
102  }
103 
105  double Epsilon() const { return epsilon; }
107  double& Epsilon() { return epsilon; }
108 
110  double Beta1() const { return beta1; }
112  double& Beta1() { return beta1; }
113 
115  double Beta2() const { return beta2; }
117  double& Beta2() { return beta2; }
118 
119  private:
120  // The epsilon value used to initialise the squared gradient parameter.
121  double epsilon;
122 
123  // The smoothing parameter.
124  double beta1;
125 
126  // The second moment coefficient.
127  double beta2;
128 
129  // The exponential moving average of gradient values.
130  arma::mat m;
131 
132  // The exponential moving average of squared gradient values.
133  arma::mat v;
134 
135  // The optimal sqaured gradient value.
136  arma::mat vImproved;
137 
138  // The number of iterations.
139  double iteration;
140 };
141 
142 } // namespace optimization
143 } // namespace mlpack
144 
145 #endif
double & Beta1()
Modify the smoothing parameter.
double Beta2() const
Get the second moment coefficient.
double Epsilon() const
Get the value used to initialise the squared gradient parameter.
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Beta1() const
Get the smoothing parameter.
AMSGradUpdate(const double epsilon=1e-8, const double beta1=0.9, const double beta2=0.999)
Construct the AMSGrad update policy with the given parameters.
void Initialize(const size_t rows, const size_t cols)
The Initialize method is called by SGD Optimizer method before the start of the iteration update proc...
AMSGrad is an exponential moving average variant which along with having benefits of optimizers like ...
void Update(arma::mat &iterate, const double stepSize, const arma::mat &gradient)
Update step for AMSGrad.
double & Beta2()
Modify the second moment coefficient.
double & Epsilon()
Modify the value used to initialise the squared gradient parameter.