lr_policy.h 1.9 KB
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;
D
dzhwinter 已提交
13 14
  virtual const char *SerializeState(int *state_len) = 0;
  virtual void DeserializeState(const std::string &state) = 0;
15 16 17
};

// constant learning rate policy
D
dzhwinter 已提交
18
class ConstLr final : public LrPolicy {
19
public:
D
dongzhihong 已提交
20
  ConstLr(double lr) : learning_rate_(lr){};
D
dzhwinter 已提交
21
  double LearningRate(const uint64_t num_sample_passed) {
D
dongzhihong 已提交
22 23 24 25 26 27 28 29 30
    return learning_rate_;
  }
  const char *SerializeState(int *state_len) {
    LrPolicyState state;
    state.set_learning_rate(learning_rate_);
    auto str = state.SerializeAsString();
    *state_len = str.size();
    return str.c_str();
  }
D
dongzhihong 已提交
31
  void DeserializeState(const std::string &str) {
D
dongzhihong 已提交
32 33 34
    LrPolicyState state;
    state.ParseFromString(str);
    learning_rate_ = state.learning_rate();
35
  }
D
dzhwinter 已提交
36

37
private:
D
dongzhihong 已提交
38
  double learning_rate_;
39 40
};

D
dzhwinter 已提交
41
class LinearLr final : public LrPolicy {
D
dzhwinter 已提交
42 43
public:
  LinearLr(double lr, double lr_decay_a, double lr_decay_b)
D
dongzhihong 已提交
44
      : learning_rate_(lr), lr_decay_a_(lr_decay_a), lr_decay_b_(lr_decay_b) {}
D
dzhwinter 已提交
45
  double LearningRate(const uint64_t num_sample_passed) {
D
dongzhihong 已提交
46 47
    return std::max(learning_rate_ - lr_decay_a_ * num_sample_passed,
                    lr_decay_b_);
D
dzhwinter 已提交
48
  }
D
dzhwinter 已提交
49
  const char *SerializeState(int *state_len) {
D
dongzhihong 已提交
50 51 52 53 54 55 56
    LrPolicyState state;
    state.set_learning_rate(learning_rate_);
    state.set_lr_decay_a(lr_decay_a_);
    state.set_lr_decay_b(lr_decay_b_);
    auto str = state.SerializeAsString();
    *state_len = str.size();
    return str.c_str();
D
dzhwinter 已提交
57
  }
D
dongzhihong 已提交
58 59 60 61 62 63
  void DeserializeState(const std::string &str) {
    LrPolicyState state;
    state.ParseFromString(str);
    learning_rate_ = state.learning_rate();
    lr_decay_a_ = state.lr_decay_a();
    lr_decay_b_ = state.lr_decay_b();
D
dzhwinter 已提交
64
  }
D
dzhwinter 已提交
65 66

private:
D
dongzhihong 已提交
67 68 69
  double learning_rate_;
  double lr_decay_a_;
  double lr_decay_b_;
D
dzhwinter 已提交
70 71
};

72 73
}  // namespace optimizer
}  // namespace paddle