adadelta_optimizer.cc 1.1 KB
Newer Older
1 2 3 4 5 6
#include "adadelta_optimizer.h"
#include <algorithm>

namespace paddle {
namespace optimizer {

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

D
dzhwinter 已提交
17
void AdadeltaOptimizer::update(const Tensor& gradient) {
18
  num_sample_passed += 1;
19 20
  double learning_rate = lr_policy->get_learning_rate(num_sample_passed);
  for (size_t i = 0; i < parameter_->size(); ++i) {
21 22 23 24 25 26 27 28 29
    accum_gradient[i] =
        rho * accum_gradient[i] + (1.0 - rho) * gradient[i] * gradient[i];

    update_delta[i] = std::sqrt(accum_delta[i] + epsilon) /
                      std::sqrt(accum_gradient[i] + epsilon) * gradient[i];

    accum_delta[i] =
        rho * accum_delta[i] + (1.0 - rho) * update_delta[i] * update_delta[i];

30 31
    parameter_[i] -=
        learning_rate * update_delta[i] + learning_rate * decay * parameter_[i];
32 33 34 35
  }
}
}  // namespace optimizer
}  // namespace paddle