adadelta_optimizer.cc 2.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2

L
Luo Tao 已提交
3 4 5
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
D
dzhwinter 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
D
dzhwinter 已提交
14

15 16
#include "adadelta_optimizer.h"
#include <algorithm>
17
#include <cmath>
18 19 20 21

namespace paddle {
namespace optimizer {

D
dzhwinter 已提交
22 23 24
void AdadeltaOptimizer::Update(const Tensor* gradient) {
  num_sample_passed_ += 1;
  double learning_rate = lr_policy_->LearningRate(num_sample_passed_);
25 26
  Tensor& param = *parameter_;
  const Tensor& grad = *gradient;
D
dzhwinter 已提交
27 28 29
  Tensor& accum_g = *accum_gradient_;
  Tensor& accum_d = *accum_delta_;
  Tensor& update_d = *update_delta_;
30
  for (size_t i = 0; i < param.size(); ++i) {
D
dzhwinter 已提交
31
    accum_g[i] = rho_ * accum_g[i] + (1.0 - rho_) * grad[i] * grad[i];
32

D
dzhwinter 已提交
33 34
    update_d[i] = std::sqrt(accum_d[i] + epsilon_) /
                  std::sqrt(accum_g[i] + epsilon_) * grad[i];
35

D
dzhwinter 已提交
36
    accum_d[i] = rho_ * accum_d[i] + (1.0 - rho_) * update_d[i] * update_d[i];
37

D
dzhwinter 已提交
38
    param[i] -= learning_rate * update_d[i] + learning_rate * decay_ * param[i];
39 40
  }
}
D
dzhwinter 已提交
41

42
std::string AdadeltaOptimizer::SerializeState() {
D
dzhwinter 已提交
43
  AdadeltaOptimizerState state;
D
dzhwinter 已提交
44
  state.set_num_sample_passed(num_sample_passed_);
45
  std::string lr_str = this->lr_policy_->SerializeState();
D
dongzhihong 已提交
46
  state.mutable_lr_state()->ParseFromString(lr_str);
D
dzhwinter 已提交
47 48 49 50 51

  TensorToProto(*parameter_, state.mutable_parameter());
  TensorToProto(*accum_gradient_, state.mutable_accum_gradient());
  TensorToProto(*accum_delta_, state.mutable_accum_delta());
  TensorToProto(*update_delta_, state.mutable_update_delta());
52
  return state.SerializeAsString();
D
dzhwinter 已提交
53 54
}

D
dzhwinter 已提交
55 56
void AdadeltaOptimizer::DeserializeState(const std::string& str) {
  AdadeltaOptimizerState state;
D
dzhwinter 已提交
57
  state.ParseFromString(str);
D
dongzhihong 已提交
58 59
  auto lr_state = state.lr_state();
  this->lr_policy_->DeserializeState(lr_state.SerializeAsString());
D
dzhwinter 已提交
60 61 62 63 64 65
  num_sample_passed_ = state.num_sample_passed();

  ProtoToTensor(state.parameter(), parameter_);
  ProtoToTensor(state.accum_gradient(), accum_gradient_);
  ProtoToTensor(state.accum_delta(), accum_delta_);
  ProtoToTensor(state.update_delta(), update_delta_);
D
dzhwinter 已提交
66
}
D
dzhwinter 已提交
67 68

}  // namespace optimizer
D
dzhwinter 已提交
69
}  // namespace paddle