cyclical_decay.hpp
Go to the documentation of this file.
1 
15 #ifndef MLPACK_CORE_OPTIMIZERS_SGDR_CYCLICAL_DECAY_HPP
16 #define MLPACK_CORE_OPTIMIZERS_SGDR_CYCLICAL_DECAY_HPP
17 
18 namespace mlpack {
19 namespace optimization {
20 
41 {
42  public:
55  CyclicalDecay(const size_t epochRestart,
56  const double multFactor,
57  const double stepSize) :
58  epochRestart(epochRestart),
59  multFactor(multFactor),
60  constStepSize(stepSize),
61  nextRestart(epochRestart),
62  batchRestart(0),
63  epochBatches(0),
64  epoch(0)
65  { /* Nothing to do here */ }
66 
74  void Update(arma::mat& /* iterate */,
75  double& stepSize,
76  const arma::mat& /* gradient */)
77  {
78  // Time to adjust the step size.
79  if (epoch >= epochRestart)
80  {
81  // n_t = n_min^i + 0.5(n_max^i - n_min^i)(1 + cos(T_cur/T_i * pi)).
82  stepSize = 0.5 * constStepSize * (1 + cos((batchRestart / epochBatches)
83  * M_PI));
84 
85  // Keep track of the number of batches since the last restart.
86  batchRestart++;
87  }
88 
89  // Time to restart.
90  if (epoch > nextRestart)
91  {
92  batchRestart = 0;
93 
94  // Adjust the period of restarts.
95  epochRestart *= multFactor;
96 
97  // Update the time for the next restart.
98  nextRestart += epochRestart;
99  }
100 
101  epoch++;
102  }
103 
105  double StepSize() const { return constStepSize; }
107  double& StepSize() { return constStepSize; }
108 
110  double EpochBatches() const { return epochBatches; }
112  double& EpochBatches() { return epochBatches; }
113 
114  private:
116  size_t epochRestart;
117 
119  double multFactor;
120 
122  double constStepSize;
123 
125  size_t nextRestart;
126 
128  size_t batchRestart;
129 
131  double epochBatches;
132 
134  size_t epoch;
135 };
136 
137 } // namespace optimization
138 } // namespace mlpack
139 
140 #endif // MLPACK_CORE_OPTIMIZERS_SGDR_CYCLICAL_DECAY_HPP
double & StepSize()
Modify the step size.
.hpp
Definition: add_to_po.hpp:21
#define M_PI
Definition: prereqs.hpp:39
double & EpochBatches()
Modify the restart fraction.
Simulate a new warm-started run/restart once a number of epochs are performed.
double EpochBatches() const
Get the restart fraction.
CyclicalDecay(const size_t epochRestart, const double multFactor, const double stepSize)
Construct the CyclicalDecay technique a restart method, where the step size decays after each batch a...
double StepSize() const
Get the step size.
void Update(arma::mat &, double &stepSize, const arma::mat &)
This function is called in each iteration after the policy update.