adam_update.hpp
Go to the documentation of this file.
1 
17 #ifndef MLPACK_CORE_OPTIMIZERS_ADAM_ADAM_UPDATE_HPP
18 #define MLPACK_CORE_OPTIMIZERS_ADAM_ADAM_UPDATE_HPP
19 
20 #include <mlpack/prereqs.hpp>
21 
22 namespace mlpack {
23 namespace optimization {
24 
43 {
44  public:
53  AdamUpdate(const double epsilon = 1e-8,
54  const double beta1 = 0.9,
55  const double beta2 = 0.999) :
56  epsilon(epsilon),
57  beta1(beta1),
58  beta2(beta2),
59  iteration(0)
60  {
61  // Nothing to do.
62  }
63 
71  void Initialize(const size_t rows, const size_t cols)
72  {
73  m = arma::zeros<arma::mat>(rows, cols);
74  v = arma::zeros<arma::mat>(rows, cols);
75  }
76 
84  void Update(arma::mat& iterate,
85  const double stepSize,
86  const arma::mat& gradient)
87  {
88  // Increment the iteration counter variable.
89  ++iteration;
90 
91  // And update the iterate.
92  m *= beta1;
93  m += (1 - beta1) * gradient;
94 
95  v *= beta2;
96  v += (1 - beta2) * (gradient % gradient);
97 
98  const double biasCorrection1 = 1.0 - std::pow(beta1, iteration);
99  const double biasCorrection2 = 1.0 - std::pow(beta2, iteration);
100 
106  iterate -= (stepSize * std::sqrt(biasCorrection2) / biasCorrection1) *
107  m / (arma::sqrt(v) + epsilon);
108  }
109 
111  double Epsilon() const { return epsilon; }
113  double& Epsilon() { return epsilon; }
114 
116  double Beta1() const { return beta1; }
118  double& Beta1() { return beta1; }
119 
121  double Beta2() const { return beta2; }
123  double& Beta2() { return beta2; }
124 
125  private:
126  // The epsilon value used to initialise the squared gradient parameter.
127  double epsilon;
128 
129  // The smoothing parameter.
130  double beta1;
131 
132  // The second moment coefficient.
133  double beta2;
134 
135  // The exponential moving average of gradient values.
136  arma::mat m;
137 
138  // The exponential moving average of squared gradient values.
139  arma::mat v;
140 
141  // The number of iterations.
142  double iteration;
143 };
144 
145 } // namespace optimization
146 } // namespace mlpack
147 
148 #endif
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...
Definition: adam_update.hpp:71
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
AdamUpdate(const double epsilon=1e-8, const double beta1=0.9, const double beta2=0.999)
Construct the Adam update policy with the given parameters.
Definition: adam_update.hpp:53
Adam is an optimizer that computes individual adaptive learning rates for different parameters from e...
Definition: adam_update.hpp:42
double Epsilon() const
Get the value used to initialise the squared gradient parameter.
void Update(arma::mat &iterate, const double stepSize, const arma::mat &gradient)
Update step for Adam.
Definition: adam_update.hpp:84
double Beta1() const
Get the smoothing parameter.
double & Epsilon()
Modify the value used to initialise the squared gradient parameter.
double Beta2() const
Get the second moment coefficient.
double & Beta1()
Modify the smoothing parameter.
double & Beta2()
Modify the second moment coefficient.