optimisticadam_update.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_OPTIMIZERS_ADAM_OPTIMISTICADAM_UPDATE_HPP
14 #define MLPACK_CORE_OPTIMIZERS_ADAM_OPTIMISTICADAM_UPDATE_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace optimization {
20 
42 {
43  public:
52  OptimisticAdamUpdate(const double epsilon = 1e-8,
53  const double beta1 = 0.9,
54  const double beta2 = 0.999) :
55  epsilon(epsilon),
56  beta1(beta1),
57  beta2(beta2),
58  iteration(0)
59  {
60  // Nothing to do.
61  }
62 
70  void Initialize(const size_t rows, const size_t cols)
71  {
72  m = arma::zeros<arma::mat>(rows, cols);
73  v = arma::zeros<arma::mat>(rows, cols);
74  g = 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) * arma::square(gradient);
97 
98  arma::mat mCorrected = m / (1.0 - std::pow(beta1, iteration));
99  arma::mat vCorrected = v / (1.0 - std::pow(beta2, iteration));
100 
101  arma::mat update = mCorrected / (arma::sqrt(vCorrected) + epsilon);
102 
103  iterate -= (2 * stepSize * update - stepSize * g);
104 
105  g = std::move(update);
106  }
107 
109  double Epsilon() const { return epsilon; }
111  double& Epsilon() { return epsilon; }
112 
114  double Beta1() const { return beta1; }
116  double& Beta1() { return beta1; }
117 
119  double Beta2() const { return beta2; }
121  double& Beta2() { return beta2; }
122 
123  private:
124  // The epsilon value used to initialize the squared gradient parameter.
125  double epsilon;
126 
127  // The smoothing parameter.
128  double beta1;
129 
130  // The second moment coefficient.
131  double beta2;
132 
133  // The exponential moving average of gradient values.
134  arma::mat m;
135 
136  // The exponential moving average of squared gradient values.
137  arma::mat v;
138  // The previous update.
139  arma::mat g;
140 
141  // The number of iterations.
142  double iteration;
143 };
144 
145 } // namespace optimization
146 } // namespace mlpack
147 
148 #endif
double & Epsilon()
Modify the value used to initialize the squared gradient parameter.
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...
.hpp
Definition: add_to_po.hpp:21
double & Beta2()
Modify the second moment coefficient.
The core includes that mlpack expects; standard C++ includes and Armadillo.
double & Beta1()
Modify the smoothing parameter.
OptimisticAdamUpdate(const double epsilon=1e-8, const double beta1=0.9, const double beta2=0.999)
Construct the OptimisticAdam update policy with the given parameters.
void Update(arma::mat &iterate, const double stepSize, const arma::mat &gradient)
Update step for OptimisticAdam.
double Beta1() const
Get the smoothing parameter.
double Epsilon() const
Get the value used to initialize the squared gradient parameter.
OptimisticAdam is an optimizer which implements the Optimistic Adam algorithm which uses Optmistic Mi...
double Beta2() const
Get the second moment coefficient.