adagrad_optimizer.cc 821 字节
Newer Older
1 2 3 4 5
#include "adagrad_optimizer.h"

namespace paddle {
namespace optimizer {

D
dzhwinter 已提交
6
void AdagradOptimizer::set_weight(Tensor* p) {
7
  size_t size = p->width();
D
dzhwinter 已提交
8 9 10 11 12 13
  real* gptr = new real[size];
  accum_gradient = Tensor(gptr, size);
  real* dptr = new real[size];
  accum_delta = Tensor(dtpr, size);
  real* dptr_current = new real[size];
  update_delta = Tensor(dptr_current, size);
14 15
}

D
dzhwinter 已提交
16
void AdagradOptimizer::update(const Tensor& gradient) {
17 18 19 20 21 22 23 24 25 26 27 28
  num_sample_passed += 1;
  double learning_rate = lr_policy->get_learning_rate();
  for (size_t i = 0; i < parameter_.size(); ++i) {
    accum_gradient[i] += gradient[i] * gradient[i];
    parameter_[i] +=
        learning_rate * (gradient[i] / std::sqrt(accum_gradient[i] + epsilon) +
                         decay * parameter_[i]);
  }
}

}  // namespace optimizer
}  // namespace paddle