NNFS
Neural network library from scratch
Loading...
Searching...
No Matches
Optimizer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Dense>
4#include "../Utilities/clue.hpp"
5#include "../Layer/Dense.hpp"
6
7namespace NNFS
8{
15 {
16 public:
23 Optimizer(double lr, double decay) : _lr(lr), _current_lr(lr), _iterations(0), _decay(decay) {}
24
28 virtual ~Optimizer() = default;
29
35 virtual void update_params(std::shared_ptr<Dense> &layer) = 0;
36
41 {
42 if (_decay > 0)
43 {
44 _current_lr = _lr * (1. / (1. + _decay * _iterations));
45 }
46 }
47
52 {
53 _iterations += 1;
54 }
55
61 double &current_lr()
62 {
63 return _current_lr;
64 }
65
72 {
73 return _iterations;
74 }
75
76 protected:
77 const double _lr; // Learning rate (constant)
78 double _current_lr; // Current learning rate
79 int _iterations; // Iteration count
80 double _decay; // Learning rate decay
81 };
82} // namespace NNFS
Base class for all optimizers.
Definition Optimizer.hpp:15
virtual ~Optimizer()=default
Basic destructor.
virtual void update_params(std::shared_ptr< Dense > &layer)=0
Update the parameters of the layer.
const double _lr
Definition Optimizer.hpp:77
void post_update_params()
Post-update parameters (e.g. increase iteration count)
Definition Optimizer.hpp:51
Optimizer(double lr, double decay)
Construct a new Optimizer object.
Definition Optimizer.hpp:23
double _decay
Definition Optimizer.hpp:80
double & current_lr()
Get the current learning rate.
Definition Optimizer.hpp:61
int & iterations()
Get current iteration count.
Definition Optimizer.hpp:71
void pre_update_params()
Pre-update parameters (e.g. learning rate decay)
Definition Optimizer.hpp:40
int _iterations
Definition Optimizer.hpp:79
double _current_lr
Definition Optimizer.hpp:78
Definition Activation.hpp:6