File size: 953 Bytes
26d5b81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef NEUROFLOW_ADAMW_HPP
#define NEUROFLOW_ADAMW_HPP

#include <cstddef>
#include <vector>
#include "tensor.hpp"

namespace neuroflow {

struct ParamState {
    Tensor m;
    Tensor v;
};

struct ParamGroup {
    std::vector<Tensor*> params;
    std::vector<Tensor*> grads;
    float lr;
    float weight_decay;
};

class AdamW {
public:
    float lr_;
    float beta1_;
    float beta2_;
    float eps_;
    float weight_decay_;
    size_t step_;

    std::vector<ParamGroup> param_groups_;
    std::vector<std::vector<ParamState>> states_;

    AdamW(float lr, float beta1 = 0.9f, float beta2 = 0.999f,
          float eps = 1e-8f, float weight_decay = 0.01f);

    void add_param_group(const ParamGroup& group);
    void step();
    void set_lr(float lr);
    float get_lr() const { return lr_; }
    size_t get_step() const { return step_; }
};

} // namespace neuroflow

#endif // NEUROFLOW_ADAMW_HPP