|
| 1 | +/** |
| 2 | + * @file ada_belief.hpp |
| 3 | + * @author Marcus Edel |
| 4 | + * |
| 5 | + * Class wrapper for the AdaBelief update Policy. The intuition for AdaBelief is |
| 6 | + * to adapt the stepsize according to the "belief" in the current gradient |
| 7 | + * direction. |
| 8 | + * |
| 9 | + * ensmallen is free software; you may redistribute it and/or modify it under |
| 10 | + * the terms of the 3-clause BSD license. You should have received a copy of |
| 11 | + * the 3-clause BSD license along with ensmallen. If not, see |
| 12 | + * http://www.opensource.org/licenses/BSD-3-Clause for more information. |
| 13 | + */ |
| 14 | +#ifndef ENSMALLEN_ADA_BELIEF_HPP |
| 15 | +#define ENSMALLEN_ADA_BELIEF_HPP |
| 16 | + |
| 17 | +#include <ensmallen_bits/sgd/sgd.hpp> |
| 18 | +#include "ada_belief_update.hpp" |
| 19 | + |
| 20 | +namespace ens { |
| 21 | + |
| 22 | +/** |
| 23 | + * The intuition for AdaBelief is to adapt the stepsize according to the |
| 24 | + * "belief" in the current gradient direction. For more information, see the |
| 25 | + * following. |
| 26 | + * |
| 27 | + * @code |
| 28 | + * @misc{zhuang2020adabelief, |
| 29 | + * title = {AdaBelief Optimizer: Adapting Stepsizes by the Belief in |
| 30 | + * Observed Gradients}, |
| 31 | + * author = {Juntang Zhuang and Tommy Tang and Sekhar Tatikonda and |
| 32 | + * Nicha Dvornek and Yifan Ding and Xenophon Papademetris |
| 33 | + * and James S. Duncan}, |
| 34 | + * year = {2020}, |
| 35 | + * eprint = {2010.07468}, |
| 36 | + * archivePrefix = {arXiv}, |
| 37 | + * } |
| 38 | + * @endcode |
| 39 | + * |
| 40 | + * AdaBelief can optimize differentiable separable functions. For more details, |
| 41 | + * see the documentation on function types included with this distribution or |
| 42 | + * on the ensmallen website. |
| 43 | + */ |
| 44 | +class AdaBelief |
| 45 | +{ |
| 46 | + public: |
| 47 | + /** |
| 48 | + * Construct the AdaBelief optimizer with the given function and parameters. |
| 49 | + * AdaBelief is sensitive to its parameters and hence a good hyperparameter |
| 50 | + * selection is necessary as its default may not fit every case. |
| 51 | + * |
| 52 | + * The maximum number of iterations refers to the maximum number of |
| 53 | + * points that are processed (i.e., one iteration equals one point; one |
| 54 | + * iteration does not equal one pass over the dataset). |
| 55 | + * |
| 56 | + * @param stepSize Step size for each iteration. |
| 57 | + * @param batchSize Number of points to process in a single step. |
| 58 | + * @param beta1 The exponential decay rate for the 1st moment estimates. |
| 59 | + * @param beta2 The exponential decay rate for the 2nd moment estimates. |
| 60 | + * @param epsilon A small constant for numerical stability. |
| 61 | + * @param maxIterations Maximum number of iterations allowed (0 means no |
| 62 | + * limit). |
| 63 | + * @param tolerance Maximum absolute tolerance to terminate algorithm. |
| 64 | + * @param shuffle If true, the function order is shuffled; otherwise, each |
| 65 | + * function is visited in linear order. |
| 66 | + * @param resetPolicy If true, parameters are reset before every Optimize |
| 67 | + * call; otherwise, their values are retained. |
| 68 | + * @param exactObjective Calculate the exact objective (Default: estimate the |
| 69 | + * final objective obtained on the last pass over the data). |
| 70 | + */ |
| 71 | + AdaBelief(const double stepSize = 0.001, |
| 72 | + const size_t batchSize = 32, |
| 73 | + const double beta1 = 0.9, |
| 74 | + const double beta2 = 0.999, |
| 75 | + const double epsilon = 1e-12, |
| 76 | + const size_t maxIterations = 100000, |
| 77 | + const double tolerance = 1e-5, |
| 78 | + const bool shuffle = true, |
| 79 | + const bool resetPolicy = true, |
| 80 | + const bool exactObjective = false); |
| 81 | + |
| 82 | + /** |
| 83 | + * Optimize the given function using AdaBelief. The given starting point will |
| 84 | + * be modified to store the finishing point of the algorithm, and the final |
| 85 | + * objective value is returned. |
| 86 | + * |
| 87 | + * @tparam SeparableFunctionType Type of the function to optimize. |
| 88 | + * @tparam MatType Type of matrix to optimize with. |
| 89 | + * @tparam GradType Type of matrix to use to represent function gradients. |
| 90 | + * @tparam CallbackTypes Types of callback functions. |
| 91 | + * @param function Function to optimize. |
| 92 | + * @param iterate Starting point (will be modified). |
| 93 | + * @param callbacks Callback functions. |
| 94 | + * @return Objective value of the final point. |
| 95 | + */ |
| 96 | + template<typename SeparableFunctionType, |
| 97 | + typename MatType, |
| 98 | + typename GradType, |
| 99 | + typename... CallbackTypes> |
| 100 | + typename std::enable_if<IsArmaType<GradType>::value, |
| 101 | + typename MatType::elem_type>::type |
| 102 | + Optimize(SeparableFunctionType& function, |
| 103 | + MatType& iterate, |
| 104 | + CallbackTypes&&... callbacks) |
| 105 | + { |
| 106 | + return optimizer.Optimize<SeparableFunctionType, MatType, GradType, |
| 107 | + CallbackTypes...>(function, iterate, |
| 108 | + std::forward<CallbackTypes>(callbacks)...); |
| 109 | + } |
| 110 | + |
| 111 | + //! Forward the MatType as GradType. |
| 112 | + template<typename SeparableFunctionType, |
| 113 | + typename MatType, |
| 114 | + typename... CallbackTypes> |
| 115 | + typename MatType::elem_type Optimize(SeparableFunctionType& function, |
| 116 | + MatType& iterate, |
| 117 | + CallbackTypes&&... callbacks) |
| 118 | + { |
| 119 | + return Optimize<SeparableFunctionType, MatType, MatType, |
| 120 | + CallbackTypes...>(function, iterate, |
| 121 | + std::forward<CallbackTypes>(callbacks)...); |
| 122 | + } |
| 123 | + |
| 124 | + //! Get the step size. |
| 125 | + double StepSize() const { return optimizer.StepSize(); } |
| 126 | + //! Modify the step size. |
| 127 | + double& StepSize() { return optimizer.StepSize(); } |
| 128 | + |
| 129 | + //! Get the batch size. |
| 130 | + size_t BatchSize() const { return optimizer.BatchSize(); } |
| 131 | + //! Modify the batch size. |
| 132 | + size_t& BatchSize() { return optimizer.BatchSize(); } |
| 133 | + |
| 134 | + //! Get the exponential decay rate for the 1st moment estimates. |
| 135 | + double Beta1() const { return optimizer.UpdatePolicy().Beta1(); } |
| 136 | + //! Modify the exponential decay rate for the 1st moment estimates. |
| 137 | + double& Beta1() { return optimizer.UpdatePolicy().Beta1(); } |
| 138 | + |
| 139 | + //! Get the exponential decay rate for the 2nd moment estimates. |
| 140 | + double Beta2() const { return optimizer.UpdatePolicy().Beta2(); } |
| 141 | + //! Get the second moment coefficient. |
| 142 | + double& Beta2() { return optimizer.UpdatePolicy().Beta2(); } |
| 143 | + |
| 144 | + //! Get the value for numerical stability. |
| 145 | + double Epsilon() const { return optimizer.UpdatePolicy().Epsilon(); } |
| 146 | + //! Modify the value used for numerical stability. |
| 147 | + double& Epsilon() { return optimizer.UpdatePolicy().Epsilon(); } |
| 148 | + |
| 149 | + //! Get the maximum number of iterations (0 indicates no limit). |
| 150 | + size_t MaxIterations() const { return optimizer.MaxIterations(); } |
| 151 | + //! Modify the maximum number of iterations (0 indicates no limit). |
| 152 | + size_t& MaxIterations() { return optimizer.MaxIterations(); } |
| 153 | + |
| 154 | + //! Get the tolerance for termination. |
| 155 | + double Tolerance() const { return optimizer.Tolerance(); } |
| 156 | + //! Modify the tolerance for termination. |
| 157 | + double& Tolerance() { return optimizer.Tolerance(); } |
| 158 | + |
| 159 | + //! Get whether or not the individual functions are shuffled. |
| 160 | + bool Shuffle() const { return optimizer.Shuffle(); } |
| 161 | + //! Modify whether or not the individual functions are shuffled. |
| 162 | + bool& Shuffle() { return optimizer.Shuffle(); } |
| 163 | + |
| 164 | + //! Get whether or not the actual objective is calculated. |
| 165 | + bool ExactObjective() const { return optimizer.ExactObjective(); } |
| 166 | + //! Modify whether or not the actual objective is calculated. |
| 167 | + bool& ExactObjective() { return optimizer.ExactObjective(); } |
| 168 | + |
| 169 | + //! Get whether or not the update policy parameters are reset before |
| 170 | + //! Optimize call. |
| 171 | + bool ResetPolicy() const { return optimizer.ResetPolicy(); } |
| 172 | + //! Modify whether or not the update policy parameters |
| 173 | + //! are reset before Optimize call. |
| 174 | + bool& ResetPolicy() { return optimizer.ResetPolicy(); } |
| 175 | + |
| 176 | + private: |
| 177 | + //! The Stochastic Gradient Descent object with AdaBelief policy. |
| 178 | + SGD<AdaBeliefUpdate> optimizer; |
| 179 | +}; |
| 180 | + |
| 181 | +} // namespace ens |
| 182 | + |
| 183 | +// Include implementation. |
| 184 | +#include "ada_belief_impl.hpp" |
| 185 | + |
| 186 | +#endif |
0 commit comments