#include "adagrad_optimizer.h" namespace paddle { namespace optimizer { template AdagradOptimizer::AdagradOptimizer(const ::paddle::OptimizerConfig& config) : ParameterOptimizer(config) { epsilon = config.adagrad().epsilon(); decay = config.adagrad().decay(); } template void AdagradOptimizer::set_weight(const Tensor* p) { size_t size = p->width(); T* gptr = new T[size]; accum_gradient = Tensor(gptr, size); T* dptr = new T[size]; accum_delta = Tensor(dtpr, size); T* dptr_current = new T[size]; update_delta = Tensor(dptr_current, size); } template void AdagradOptimizer::update(const Tensor& 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; template class AdagradOptimizer; } // namespace optimizer } // namespace paddle