sgd_optimizer.h 957 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#ifndef PADDLE_SGD_OPTIMIZER_H_
#define PADDLE_SGD_OPTIMIZER_H_

#include "parameter_optimizer.h"

namespace paddle {
namespace optimizer {

template <class T>
class SGDOptimizer : public ParameterOptimizer<T> {
public:
12 13 14 15 16 17 18 19 20 21 22 23
  using ParameterOptimizer<T>::parameter_;
  using ParameterOptimizer<T>::num_sample_passed;
  using ParameterOptimizer<T>::lr_policy;

  SGDOptimizer(double m,
               double d,
               bool n,
               double learning_rate,
               uint64_t num_sample_passed,
               BaseLr* lr)
      : ParameterOptimizer<T>(lr), momentum(m), decay(d), nesterov(n) {}
  virtual ~SGDOptimizer() {
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    // clear memory by Tensor library
    delete momentums_;
  }
  void update(const Tensor<T>& gradient);

  void set_weight(const Tensor<T>* p);
  T* get_weight() const;

private:
  Tensor<T>* momentums_;
  double momentum;
  double decay;
  bool nesterov;
};

}  // namespace optimizer
}  // namespace paddle

#endif