|
| 1 | +#ifndef AI_TOOLBOX_ADAM_HEADER_FILE |
| 2 | +#define AI_TOOLBOX_ADAM_HEADER_FILE |
| 3 | + |
| 4 | +#include <AIToolbox/Types.hpp> |
| 5 | + |
| 6 | +namespace AIToolbox { |
| 7 | + /** |
| 8 | + * @brief This class implements the ADAM gradient descent algorithm. |
| 9 | + * |
| 10 | + * This class keeps things simple and fast. It takes two pointers to two |
| 11 | + * equally-sized vectors; one used to track the currently examined point, |
| 12 | + * and the other to provide Adam with the gradient. |
| 13 | + * |
| 14 | + * This class expects you to compute the gradient of the currently examined |
| 15 | + * point. At each step() call, the point vector is updated following the |
| 16 | + * gradient using the Adam algorithm. |
| 17 | + * |
| 18 | + * We take pointers rather than references so that the pointers can be |
| 19 | + * updated as needed, while the class instance kept around. This only works |
| 20 | + * if the new vectors have the same size as before, but it allows to avoid |
| 21 | + * reallocation of the internal helper vectors. |
| 22 | + */ |
| 23 | + class Adam { |
| 24 | + public: |
| 25 | + /** |
| 26 | + * @brief Basic constructor. |
| 27 | + * |
| 28 | + * We expect the pointers to not be null, and the vectors to be preallocated. |
| 29 | + * |
| 30 | + * The point vector should contain the point where to start the |
| 31 | + * gradient descent process. The gradient vector should contain |
| 32 | + * the gradient at that point. |
| 33 | + * |
| 34 | + * @param point A pointer to preallocated space where to write the point. |
| 35 | + * @param gradient A pointer to preallocated space containing the current gradient. |
| 36 | + * @param alpha Adam's step size/learning rate. |
| 37 | + * @param beta1 Adam's exponential decay rate for first moment estimates. |
| 38 | + * @param beta2 Adam's exponential decay rate for second moment estimates. |
| 39 | + * @param epsilon Additive parameter to prevent division by zero. |
| 40 | + */ |
| 41 | + Adam(AIToolbox::Vector * point, const AIToolbox::Vector * gradient, double alpha = 0.001, double beta1 = 0.9, double beta2 = 0.999, double epsilon = 1e-8); |
| 42 | + |
| 43 | + /** |
| 44 | + * @brief This function updates the point using the currently set gradient. |
| 45 | + * |
| 46 | + * This function overwrites the vector pointed by the `point` |
| 47 | + * pointer, by following the currently set gradient. |
| 48 | + * |
| 49 | + * It is expected that the gradient is correct and has been updated |
| 50 | + * by the user before calling this function. |
| 51 | + */ |
| 52 | + void step(); |
| 53 | + |
| 54 | + /** |
| 55 | + * @brief This function resets the gradient descent process. |
| 56 | + * |
| 57 | + * This function clears all internal values so that the gradient |
| 58 | + * descent process can be restarted from scratch. |
| 59 | + * |
| 60 | + * The point vector is not modified. |
| 61 | + */ |
| 62 | + void reset(); |
| 63 | + |
| 64 | + /** |
| 65 | + * @brief This function resets the gradient descent process. |
| 66 | + * |
| 67 | + * This function clears all internal values so that the gradient |
| 68 | + * descent process can be restarted from scratch. |
| 69 | + * |
| 70 | + * The point and gradient pointers are updated with the new inputs. |
| 71 | + */ |
| 72 | + void reset(AIToolbox::Vector * point, const AIToolbox::Vector * gradient); |
| 73 | + |
| 74 | + /** |
| 75 | + * @brief This function sets the current learning rate. |
| 76 | + */ |
| 77 | + void setAlpha(double alpha); |
| 78 | + |
| 79 | + /** |
| 80 | + * @brief This function sets the current exponential decay rate for first moment estimates. |
| 81 | + */ |
| 82 | + void setBeta1(double beta1); |
| 83 | + |
| 84 | + /** |
| 85 | + * @brief This function sets the current exponential decay rate for second moment estimates. |
| 86 | + */ |
| 87 | + void setBeta2(double beta2); |
| 88 | + |
| 89 | + /** |
| 90 | + * @brief This function sets the current additive division parameter. |
| 91 | + */ |
| 92 | + void setEpsilon(double epsilon); |
| 93 | + |
| 94 | + /** |
| 95 | + * @brief This function returns the current learning rate. |
| 96 | + */ |
| 97 | + double getAlpha() const; |
| 98 | + |
| 99 | + /** |
| 100 | + * @brief This function returns the current exponential decay rate for first moment estimates. |
| 101 | + */ |
| 102 | + double getBeta1() const; |
| 103 | + |
| 104 | + /** |
| 105 | + * @brief This function returns the current exponential decay rate for second moment estimates. |
| 106 | + */ |
| 107 | + double getBeta2() const; |
| 108 | + |
| 109 | + /** |
| 110 | + * @brief This function returns the current additive division parameter. |
| 111 | + */ |
| 112 | + double getEpsilon() const; |
| 113 | + |
| 114 | + private: |
| 115 | + AIToolbox::Vector * point_; |
| 116 | + const AIToolbox::Vector * gradient_; |
| 117 | + AIToolbox::Vector m_, v_; |
| 118 | + |
| 119 | + double beta1_, beta2_, alpha_, epsilon_; |
| 120 | + unsigned step_; |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +#endif |
0 commit comments