MiniDNN
AdaGrad.h
1 #ifndef OPTIMIZER_ADAGRAD_H_
2 #define OPTIMIZER_ADAGRAD_H_
3 
4 #include <Eigen/Core>
5 #include "../Config.h"
6 #include "../Optimizer.h"
7 #include "../Utils/sparsepp.h"
8 
9 namespace MiniDNN {
10 
11 
17 class AdaGrad: public Optimizer
18 {
19 private:
20  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> Vector;
21  typedef Eigen::Array<Scalar, Eigen::Dynamic, 1> Array;
22  typedef Vector::ConstAlignedMapType ConstAlignedMapVec;
23  typedef Vector::AlignedMapType AlignedMapVec;
24 
25  spp::sparse_hash_map<const Scalar*, Array> m_history;
26 
27 public:
28  Scalar m_lrate;
29  Scalar m_eps;
30 
31  AdaGrad() :
32  m_lrate(Scalar(0.01)), m_eps(Scalar(1e-7))
33  {}
34 
35  void reset() { m_history.clear(); }
36 
37  void update(ConstAlignedMapVec& dvec, AlignedMapVec& vec)
38  {
39  // Get the accumulated squared gradient associated with this gradient
40  Array& grad_square = m_history[dvec.data()];
41  // If length is zero, initialize it
42  if(grad_square.size() == 0)
43  {
44  grad_square.resize(dvec.size());
45  grad_square.setZero();
46  }
47  // Update accumulated squared gradient
48  grad_square += dvec.array().square();
49  // Update parameters
50  vec.array() -= m_lrate * dvec.array() / (grad_square.sqrt() + m_eps);
51  }
52 };
53 
54 
55 } // namespace MiniDNN
56 
57 
58 #endif /* OPTIMIZER_ADAGRAD_H_ */
void update(ConstAlignedMapVec &dvec, AlignedMapVec &vec)
Definition: AdaGrad.h:37
void reset()
Definition: AdaGrad.h:35