parameter_optimizer.h 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#ifndef PADDLE_PARAMETER_OPTIMIZER_H_
#define PADDLE_PARAMETER_OPTIMIZER_H_

#include <glog/logging.h>
#include <functional>
#include <string>
#include "OptimizerConfig.pb.h"
#include "Tensor.h"
#include "lr_policy.h"

namespace paddle {
namespace optimizer {

14 15 16 17 18 19
class ParameterOptimizerBase {
private:
  ParameterOptimizerBase(const ParameterOptimizerBase &) = delete;
  ParameterOptimizerBase &operator=(const ParameterOptimizerBase &) = delete;
};

20 21 22 23 24 25 26
template <class T>
class ParameterOptimizer {
public:
  /**
   * @brief  update hook for algorithm need to traverse parameter more than
   * once.
   */
27
  // use config for pack trainig state
28 29
  ParameterOptimizer(const OptimizerConfig &config) : config_(config){};

30 31 32
  ParameterOptimizer(BaseLr *lr) : lr_policy(lr), num_sample_passed(0) {}
  virtual ~ParameterOptimizer() { delete parameter_; };

33
  static ParameterOptimizer *create(const ::std::string &config_proto);
34
  virtual void update(const Tensor<T> &gradient) = 0;
35 36 37
  virtual T *get_weight() const;
  virtual void set_weight(const Tensor<T> *parameter);

38
public:
39 40 41 42 43 44 45 46 47 48 49 50 51
  bool config_valid(::std::string &config) const;
  OptimizerConfig config_;
  Tensor<T> *parameter_;

  // learning rate policy
  BaseLr *lr_policy;
  uint64_t num_sample_passed;
};

}  // namespace optimizer
}  // namespace paddle

#endif