Caffe2 - C++ API
A deep learning, cross platform ML framework
adagrad.cpp
1 #include <torch/optim/adagrad.h>
2 
3 #include <torch/csrc/autograd/variable.h>
4 #include <torch/serialize/archive.h>
5 #include <torch/utils.h>
6 
7 #include <ATen/ATen.h>
8 
9 #include <functional>
10 
11 namespace torch {
12 namespace optim {
13 
14 AdagradOptions::AdagradOptions(double learning_rate)
15  : learning_rate_(learning_rate) {}
16 
19 void Adagrad::step() {
20  for (size_t i = 0; i < parameters_.size(); ++i) {
21  Tensor p = parameters_.at(i);
22  if (!p.grad().defined()) {
23  continue;
24  }
25 
26  if (options.weight_decay_ > 0) {
27  p.grad() = p.grad() + options.weight_decay_ * p;
28  }
29 
30  buffer_at(step_buffers, i) += 1.0;
31  const auto clr = options.learning_rate_ /
32  (1.0 + (buffer_at(step_buffers, i) - 1.0) * options.lr_decay_);
33 
34  auto& sum = buffer_at(sum_buffers, i);
35  sum.addcmul_(p.grad(), p.grad(), 1.0);
36  const auto std = buffer_at(sum_buffers, i).sqrt().add_(1e-10);
37 
38  NoGradGuard guard;
39  p.addcdiv_(p.grad(), std, -clr);
40  }
41 }
42 
43 void Adagrad::save(serialize::OutputArchive& archive) const {
44  serialize(*this, archive);
45 }
46 
47 void Adagrad::load(serialize::InputArchive& archive) {
48  serialize(*this, archive);
49 }
50 } // namespace optim
51 } // namespace torch
LinearOptions options
The options used to configure this module.
Definition: linear.h:40
Definition: jit_type.h:17
A recursive representation of tensors that can be deserialized from a file or stream.
Definition: input-archive.h:32