1 #include <torch/optim/rmsprop.h> 3 #include <torch/csrc/autograd/variable.h> 4 #include <torch/serialize/archive.h> 5 #include <torch/utils.h> 14 RMSpropOptions::RMSpropOptions(
double learning_rate)
15 : learning_rate_(learning_rate) {}
22 if (!p.grad().defined()) {
26 if (options.weight_decay_ > 0) {
27 p.grad() = p.grad() + options.weight_decay_ * p;
30 auto square_average =
buffer_at(square_average_buffers, i);
31 square_average.mul_(options.alpha_)
32 .addcmul_(p.grad(), p.grad(), 1.0 - options.alpha_);
35 if (options.centered_ > 0) {
36 auto& grad_average =
buffer_at(grad_average_buffers, i);
37 grad_average.mul_(options.alpha_).add_(p.grad(), 1.0 - options.alpha_);
38 average = square_average.addcmul(grad_average, grad_average, -1.0)
42 average = square_average.sqrt().add_(options.eps_);
46 if (options.momentum_ > 0) {
47 auto& momentum =
buffer_at(momentum_buffers, i);
48 momentum.mul_(options.momentum_).addcdiv_(p.grad(), average);
49 p.add_(momentum, -options.learning_rate_);
51 p.addcdiv_(p.grad(), average, -options.learning_rate_);
57 serialize(*
this, archive);
61 serialize(*
this, archive);
std::vector< Tensor > parameters_
The parameters this optimizer optimizes.
void load(serialize::InputArchive &archive) override
Deserializes the optimizer state from the given archive.
void step() override
Adapted from https://github.com/pytorch/pytorch/blob/master/torch/optim/rmsprop.py.
void save(serialize::OutputArchive &archive) const override
Serializes the optimizer state into the given archive.
T & buffer_at(std::vector< T > &buffers, size_t index)
Accesses a buffer at the given index.