MiniDNN
RMSProp.h
1 #ifndef OPTIMIZER_RMSPROP_H_
2 #define OPTIMIZER_RMSPROP_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 RMSProp: 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  Scalar m_decay;
31 
32  RMSProp() :
33  m_lrate(Scalar(0.001)), m_eps(Scalar(1e-6)), m_decay(Scalar(0.9))
34  {}
35 
36  void reset() { m_history.clear(); }
37 
38  void update(ConstAlignedMapVec& dvec, AlignedMapVec& vec)
39  {
40  // Get the accumulated squared gradient associated with this gradient
41  Array& grad_square = m_history[dvec.data()];
42  // If length is zero, initialize it
43  if(grad_square.size() == 0)
44  {
45  grad_square.resize(dvec.size());
46  grad_square.setZero();
47  }
48  // Update accumulated squared gradient
49  grad_square = m_decay * grad_square + (Scalar(1) - m_decay) * dvec.array().square();
50  // Update parameters
51  vec.array() -= m_lrate * dvec.array() / (grad_square + m_eps).sqrt();
52  }
53 };
54 
55 
56 } // namespace MiniDNN
57 
58 
59 #endif /* OPTIMIZER_RMSPROP_H_ */
void reset()
Definition: RMSProp.h:36
void update(ConstAlignedMapVec &dvec, AlignedMapVec &vec)
Definition: RMSProp.h:38