NNFS
Neural network library from scratch
Loading...
Searching...
No Matches
Adagrad.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Dense>
4#include "Optimizer.hpp"
5
6namespace NNFS
7{
13 class Adagrad : public Optimizer
14 {
15 public:
23 Adagrad(double lr, double decay = 0.0, double epsilon = 1e-7) : Optimizer(lr, decay),
24 _epsilon(epsilon) {}
25
32 void update_params(std::shared_ptr<Dense> &layer)
33 {
34 Eigen::MatrixXd weights = layer->weights();
35 Eigen::MatrixXd biases = layer->biases();
36 Eigen::MatrixXd dweights = layer->dweights();
37 Eigen::MatrixXd dbiases = layer->dbiases();
38
39 Eigen::MatrixXd weights_cache = layer->weights_optimizer();
40 Eigen::MatrixXd biases_cache = layer->biases_optimizer();
41
42 weights_cache += dweights.cwisePow(2);
43 biases_cache += dbiases.cwisePow(2);
44
45 weights += (-_current_lr * dweights.array() / (weights_cache.array().sqrt() + _epsilon)).matrix();
46 biases += (-_current_lr * dbiases.array() / (biases_cache.array().sqrt() + _epsilon)).matrix();
47
48 layer->weights_optimizer(weights_cache);
49 layer->biases_optimizer(biases_cache);
50
51 layer->weights(weights);
52 layer->biases(biases);
53 }
54
55 private:
56 double _epsilon; // Epsilon - to avoid division by zero
57 };
58} // namespace NNFS
Adagrad optimizer (Adaptive Gradient)
Definition Adagrad.hpp:14
Adagrad(double lr, double decay=0.0, double epsilon=1e-7)
Construct a new Adagrad object.
Definition Adagrad.hpp:23
void update_params(std::shared_ptr< Dense > &layer)
Update the parameters of the layer.
Definition Adagrad.hpp:32
Base class for all optimizers.
Definition Optimizer.hpp:15
double _current_lr
Definition Optimizer.hpp:78
Definition Activation.hpp:6