lr_policy.h 2.4 KB
Newer Older
1
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
D
dzhwinter 已提交
14
#pragma once
15

D
dzhwinter 已提交
16
#include <algorithm>
17
#include "OptimizerConfig.pb.h"
18 19 20 21

namespace paddle {
namespace optimizer {

D
dzhwinter 已提交
22
class LrPolicy {
W
Wu Yi 已提交
23
 public:
D
dzhwinter 已提交
24 25
  virtual ~LrPolicy() {}
  virtual double LearningRate(const uint64_t num_sample_passed) = 0;
26
  virtual std::string SerializeState() = 0;
D
dzhwinter 已提交
27
  virtual void DeserializeState(const std::string &state) = 0;
28 29 30
};

// constant learning rate policy
D
dzhwinter 已提交
31
class ConstLr final : public LrPolicy {
W
Wu Yi 已提交
32
 public:
D
dongzhihong 已提交
33
  ConstLr(double lr) : learning_rate_(lr){};
D
dzhwinter 已提交
34
  double LearningRate(const uint64_t num_sample_passed) {
D
dongzhihong 已提交
35 36
    return learning_rate_;
  }
37
  std::string SerializeState() {
D
dongzhihong 已提交
38 39
    LrPolicyState state;
    state.set_learning_rate(learning_rate_);
40
    return state.SerializeAsString();
D
dongzhihong 已提交
41
  }
D
dongzhihong 已提交
42
  void DeserializeState(const std::string &str) {
D
dongzhihong 已提交
43 44 45
    LrPolicyState state;
    state.ParseFromString(str);
    learning_rate_ = state.learning_rate();
46
  }
D
dzhwinter 已提交
47

W
Wu Yi 已提交
48
 private:
D
dongzhihong 已提交
49
  double learning_rate_;
50 51
};

D
dzhwinter 已提交
52
class LinearLr final : public LrPolicy {
W
Wu Yi 已提交
53
 public:
D
dzhwinter 已提交
54
  LinearLr(double lr, double lr_decay_a, double lr_decay_b)
D
dongzhihong 已提交
55
      : learning_rate_(lr), lr_decay_a_(lr_decay_a), lr_decay_b_(lr_decay_b) {}
D
dzhwinter 已提交
56
  double LearningRate(const uint64_t num_sample_passed) {
D
dongzhihong 已提交
57 58
    return std::max(learning_rate_ - lr_decay_a_ * num_sample_passed,
                    lr_decay_b_);
D
dzhwinter 已提交
59
  }
60
  std::string SerializeState() {
D
dongzhihong 已提交
61 62 63 64
    LrPolicyState state;
    state.set_learning_rate(learning_rate_);
    state.set_lr_decay_a(lr_decay_a_);
    state.set_lr_decay_b(lr_decay_b_);
65
    return state.SerializeAsString();
D
dzhwinter 已提交
66
  }
D
dongzhihong 已提交
67 68 69 70 71 72
  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 已提交
73
  }
D
dzhwinter 已提交
74

W
Wu Yi 已提交
75
 private:
D
dongzhihong 已提交
76 77 78
  double learning_rate_;
  double lr_decay_a_;
  double lr_decay_b_;
D
dzhwinter 已提交
79 80
};

81 82
}  // namespace optimizer
}  // namespace paddle