|
| 1 | +/** |
| 2 | + * @file qhadam.hpp |
| 3 | + * @author Niteya Shah |
| 4 | + * |
| 5 | + * Class wrapper for the QHAdam update Policy. QHAdam is a variant of the Adam |
| 6 | + * based on quasi hyperbolic moments. |
| 7 | + * |
| 8 | + * ensmallen is free software; you may redistribute it and/or modify it under |
| 9 | + * the terms of the 3-clause BSD license. You should have received a copy of |
| 10 | + * the 3-clause BSD license along with ensmallen. If not, see |
| 11 | + * http://www.opensource.org/licenses/BSD-3-Clause for more information. |
| 12 | + */ |
| 13 | +#ifndef ENSMALLEN_ADAM_QHADAM_HPP |
| 14 | +#define ENSMALLEN_ADAM_QHADAM_HPP |
| 15 | + |
| 16 | +#include <ensmallen_bits/sgd/sgd.hpp> |
| 17 | +#include "qhadam_update.hpp" |
| 18 | + |
| 19 | +namespace ens { |
| 20 | + |
| 21 | +/** |
| 22 | + * QHadam is an variation of Adam with Quasi-Hyperbolic step. It can be |
| 23 | + * a weighted mean of the momentum step. Due to its paramterisation it can |
| 24 | + * recover many other optimisation strategies. |
| 25 | + * |
| 26 | + * For more information, see the following. |
| 27 | + * |
| 28 | + * @code |
| 29 | + * @inproceedings{ma2019qh, |
| 30 | + * title={Quasi-hyperbolic momentum and Adam for deep learning}, |
| 31 | + * author={Jerry Ma and Denis Yarats}, |
| 32 | + * booktitle={International Conference on Learning Representations}, |
| 33 | + * year={2019} |
| 34 | + * } |
| 35 | + * @endcode |
| 36 | + * |
| 37 | + * QHAdam can optimize differentiable separable functions. For more details, |
| 38 | + * see the documentation on function types included with this distribution or |
| 39 | + * on the ensmallen website. |
| 40 | + */ |
| 41 | +class QHAdam |
| 42 | +{ |
| 43 | + public: |
| 44 | + /** |
| 45 | + * Construct the QHAdam optimizer with the given function and parameters. |
| 46 | + * QHAdam is sensitive to its paramters and hence a good hyper paramater |
| 47 | + * selection is necessary as its default may not fit every case. |
| 48 | + * |
| 49 | + * The maximum number of iterations refers to the maximum number of |
| 50 | + * points that are processed (i.e., one iteration equals one point; one |
| 51 | + * iteration does not equal one pass over the dataset). |
| 52 | + * |
| 53 | + * @param stepSize Step size for each iteration. |
| 54 | + * @param batchSize Number of points to process in a single step. |
| 55 | + * @param v1 The first quasi-hyperbolic term. |
| 56 | + * @param v1 The second quasi-hyperbolic term. |
| 57 | + * @param beta1 Exponential decay rate for the first moment estimates. |
| 58 | + * @param beta2 Exponential decay rate for the weighted infinity norm |
| 59 | + * estimates. |
| 60 | + * @param epsilon Value used to initialise the mean squared gradient |
| 61 | + * parameter. |
| 62 | + * @param maxIterations Maximum number of iterations allowed (0 means no |
| 63 | + * limit). |
| 64 | + * @param tolerance Maximum absolute tolerance to terminate algorithm. |
| 65 | + * @param shuffle If true, the function order is shuffled; otherwise, each |
| 66 | + * function is visited in linear order. |
| 67 | + * @param resetPolicy If true, parameters are reset before every Optimize |
| 68 | + * call; otherwise, their values are retained. |
| 69 | + */ |
| 70 | + QHAdam(const double stepSize = 0.001, |
| 71 | + const size_t batchSize = 32, |
| 72 | + const double v1 = 0.7, |
| 73 | + const double v2 = 1, |
| 74 | + const double beta1 = 0.9, |
| 75 | + const double beta2 = 0.999, |
| 76 | + const double epsilon = 1e-8, |
| 77 | + const size_t maxIterations = 100000, |
| 78 | + const double tolerance = 1e-5, |
| 79 | + const bool shuffle = true, |
| 80 | + const bool resetPolicy = true); |
| 81 | + |
| 82 | + /** |
| 83 | + * Optimize the given function using QHAdam. The given starting point will be |
| 84 | + * modified to store the finishing point of the algorithm, and the final |
| 85 | + * objective value is returned. |
| 86 | + * |
| 87 | + * @tparam DecomposableFunctionType Type of the function to optimize. |
| 88 | + * @param function Function to optimize. |
| 89 | + * @param iterate Starting point (will be modified). |
| 90 | + * @return Objective value of the final point. |
| 91 | + */ |
| 92 | + template<typename DecomposableFunctionType> |
| 93 | + double Optimize(DecomposableFunctionType& function, arma::mat& iterate) |
| 94 | + { |
| 95 | + return optimizer.Optimize(function, iterate); |
| 96 | + } |
| 97 | + |
| 98 | + //! Get the step size. |
| 99 | + double StepSize() const { return optimizer.StepSize(); } |
| 100 | + //! Modify the step size. |
| 101 | + double& StepSize() { return optimizer.StepSize(); } |
| 102 | + |
| 103 | + //! Get the batch size. |
| 104 | + size_t BatchSize() const { return optimizer.BatchSize(); } |
| 105 | + //! Modify the batch size. |
| 106 | + size_t& BatchSize() { return optimizer.BatchSize(); } |
| 107 | + |
| 108 | + //! Get the smoothing parameter. |
| 109 | + double Beta1() const { return optimizer.UpdatePolicy().Beta1(); } |
| 110 | + //! Modify the smoothing parameter. |
| 111 | + double& Beta1() { return optimizer.UpdatePolicy().Beta1(); } |
| 112 | + |
| 113 | + //! Get the second moment coefficient. |
| 114 | + double Beta2() const { return optimizer.UpdatePolicy().Beta2(); } |
| 115 | + //! Modify the second moment coefficient. |
| 116 | + double& Beta2() { return optimizer.UpdatePolicy().Beta2(); } |
| 117 | + |
| 118 | + //! Get the value used to initialise the mean squared gradient parameter. |
| 119 | + double Epsilon() const { return optimizer.UpdatePolicy().Epsilon(); } |
| 120 | + //! Modify the value used to initialise the mean squared gradient parameter. |
| 121 | + double& Epsilon() { return optimizer.UpdatePolicy().Epsilon(); } |
| 122 | + |
| 123 | + //! Get the maximum number of iterations (0 indicates no limit). |
| 124 | + size_t MaxIterations() const { return optimizer.MaxIterations(); } |
| 125 | + //! Modify the maximum number of iterations (0 indicates no limit). |
| 126 | + size_t& MaxIterations() { return optimizer.MaxIterations(); } |
| 127 | + |
| 128 | + //! Get the tolerance for termination. |
| 129 | + double Tolerance() const { return optimizer.Tolerance(); } |
| 130 | + //! Modify the tolerance for termination. |
| 131 | + double& Tolerance() { return optimizer.Tolerance(); } |
| 132 | + |
| 133 | + //! Get whether or not the individual functions are shuffled. |
| 134 | + bool Shuffle() const { return optimizer.Shuffle(); } |
| 135 | + //! Modify whether or not the individual functions are shuffled. |
| 136 | + bool& Shuffle() { return optimizer.Shuffle(); } |
| 137 | + |
| 138 | + //! Get whether or not the update policy parameters are reset before |
| 139 | + //! Optimize call. |
| 140 | + bool ResetPolicy() const { return optimizer.ResetPolicy(); } |
| 141 | + //! Modify whether or not the update policy parameters |
| 142 | + //! are reset before Optimize call. |
| 143 | + bool& ResetPolicy() { return optimizer.ResetPolicy(); } |
| 144 | + |
| 145 | + //! Get the first quasi hyperbolic parameter. |
| 146 | + double V1() const { return optimizer.UpdatePolicy().V1(); } |
| 147 | + //! Modify the first quasi hyperbolic parameter. |
| 148 | + double& V1() { return optimizer.UpdatePolicy().V1(); } |
| 149 | + |
| 150 | + //! Get the second quasi hyperbolic parameter. |
| 151 | + double V2() const { return optimizer.UpdatePolicy().V2(); } |
| 152 | + //! Modify the second quasi hyperbolic parameter. |
| 153 | + double& V2() { return optimizer.UpdatePolicy().V2(); } |
| 154 | + |
| 155 | + private: |
| 156 | + //! The Stochastic Gradient Descent object with QHAdam policy. |
| 157 | + SGD<QHAdamUpdate> optimizer; |
| 158 | +}; |
| 159 | + |
| 160 | +} // namespace ens |
| 161 | + |
| 162 | +// Include implementation. |
| 163 | +#include "qhadam_impl.hpp" |
| 164 | + |
| 165 | +#endif |
0 commit comments