ConvNet  1.0
A GPU-based C++ implementation of Convolutional Neural Nets
 All Classes Namespaces Functions Variables
optimizer.h
1 #ifndef OPTIMIZER_H_
2 #define OPTIMIZER_H_
3 #include "util.h"
4 #include "matrix.h"
5 
8 class Optimizer {
9  public:
10  Optimizer(const config::Optimizer& optimizer_config);
11  ~Optimizer();
12 
13  // Allocate any memory needed.
14  virtual void AllocateMemory(const int rows, const int cols);
15  virtual bool IsAllocated() { return false; }
16 
17  // Do the optimizaion. This will update parameter.
18  virtual void Optimize(Matrix& gradient, Matrix& parameter) = 0;
19  virtual void ReduceLearningRate(float factor);
20 
21  // Load and Save gradient history so that optimization can be restarted if it
22  // gets interrupted for some reason.
23  virtual void LoadParameters(hid_t file, const string& prefix);
24  virtual void SaveParameters(hid_t file, const string& prefix);
25 
26  static Optimizer* ChooseOptimizer(const config::Optimizer& config);
27 
28  protected:
29  float GetDecayedEpsilon() const;
30  void ApplyConstraints(Matrix& parameter);
31 
32  const config::Optimizer::Decay epsilon_decay_type_;
33  float epsilon_, minimum_epsilon_;
34  const int epsilon_decay_timescale_, start_optimization_after_;
35  const float l2_decay_, weight_norm_limit_, weight_norm_constraint_;
36  int step_;
37 };
38 
42 class SGDOptimizer : public Optimizer {
43  public:
44  SGDOptimizer(const config::Optimizer& optimizer_config);
45  virtual void AllocateMemory(const int rows, const int cols);
46  virtual void Optimize(Matrix& gradient, Matrix& parameter);
47  virtual void LoadParameters(hid_t file, const string& prefix);
48  virtual void SaveParameters(hid_t file, const string& prefix);
49  virtual bool IsAllocated() { return gradient_history_.GetNumEls() > 0; }
50 
51  protected:
52  float GetMomentum() const;
53 
54  Matrix gradient_history_;
55  const float gradient_clip_;
56 
57  // Hyperparams.
58  const float initial_momentum_, final_momentum_;
59  const int momentum_transition_timescale_;
60 };
61 
65 class LBFGSOptimizer : public Optimizer {
66  public:
67  LBFGSOptimizer(const config::Optimizer& optimizer_config);
68  virtual void AllocateMemory(const int rows, const int cols);
69  virtual void Optimize(Matrix& gradient, Matrix& parameter);
70  virtual void LoadParameters(hid_t file, const string& prefix);
71  virtual void SaveParameters(hid_t file, const string& prefix);
72  virtual bool IsAllocated() { return q_.GetNumEls() > 0; }
73 
74  protected:
75  Matrix q_, last_q_, last_w_;
76  const int m_;
77  vector<float> rho_, alpha_, beta_;
78  vector<Matrix> s_, y_;
79  int start_;
80 
81 };
82 #endif
Base class for all optimizers.
Definition: optimizer.h:8
A GPU matrix class.
Definition: matrix.h:11
Stochastic gradient descent.
Definition: optimizer.h:42
Implmenets LBFGS optimization.
Definition: optimizer.h:65