lr_policy.h 944 字节
Newer Older
D
dzhwinter 已提交
1
#pragma once
2

D
dzhwinter 已提交
3
#include <algorithm>
4
#include "OptimizerConfig.pb.h"
5 6 7 8

namespace paddle {
namespace optimizer {

D
dzhwinter 已提交
9
class LrPolicy {
10
public:
D
dzhwinter 已提交
11 12
  virtual ~LrPolicy() {}
  virtual double LearningRate(const uint64_t num_sample_passed) = 0;
13 14 15
};

// constant learning rate policy
D
dzhwinter 已提交
16
class ConstLr final : public LrPolicy {
17
public:
D
dzhwinter 已提交
18 19
  ConstLr(double lr) : learning_rate(lr){};
  double LearningRate(const uint64_t num_sample_passed) {
20 21
    return learning_rate;
  }
D
dzhwinter 已提交
22 23 24

protected:
  double learning_rate;
25 26
};

D
dzhwinter 已提交
27
class LinearLr final : public LrPolicy {
D
dzhwinter 已提交
28 29
public:
  LinearLr(double lr, double lr_decay_a, double lr_decay_b)
D
dzhwinter 已提交
30 31
      : learning_rate(lr), lr_decay_a(lr_decay_a), lr_decay_b(lr_decay_b) {}
  double LearningRate(const uint64_t num_sample_passed) {
D
dzhwinter 已提交
32 33 34 35
    return std::max(learning_rate - lr_decay_a * num_sample_passed, lr_decay_b);
  }

private:
D
dzhwinter 已提交
36
  double learning_rate;
D
dzhwinter 已提交
37 38 39 40
  double lr_decay_a;
  double lr_decay_b;
};

41 42
}  // namespace optimizer
}  // namespace paddle