adagrad_optimizer.cc 968 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "adagrad_optimizer.h"

namespace paddle {
namespace optimizer {
template <class T>

template <class T>
void AdagradOptimizer<T>::set_weight(const Tensor<T>* p) {
  size_t size = p->width();
  T* gptr = new T[size];
  accum_gradient = Tensor<T>(gptr, size);
  T* dptr = new T[size];
  accum_delta = Tensor<T>(dtpr, size);
  T* dptr_current = new T[size];
  update_delta = Tensor<T>(dptr_current, size);
}

template <class T>
void AdagradOptimizer<T>::update(const Tensor<T>& gradient) {
  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]);
  }
}

template class AdagradOptimizer<float>;
template class AdagradOptimizer<double>;
}  // namespace optimizer
}  // namespace paddle