Caffe2 - Python API
A deep learning, cross platform ML framework
adadelta.py
1 import torch
2 
3 from .optimizer import Optimizer
4 
5 
6 class Adadelta(Optimizer):
7  """Implements Adadelta algorithm.
8 
9  It has been proposed in `ADADELTA: An Adaptive Learning Rate Method`__.
10 
11  Arguments:
12  params (iterable): iterable of parameters to optimize or dicts defining
13  parameter groups
14  rho (float, optional): coefficient used for computing a running average
15  of squared gradients (default: 0.9)
16  eps (float, optional): term added to the denominator to improve
17  numerical stability (default: 1e-6)
18  lr (float, optional): coefficient that scale delta before it is applied
19  to the parameters (default: 1.0)
20  weight_decay (float, optional): weight decay (L2 penalty) (default: 0)
21 
22  __ https://arxiv.org/abs/1212.5701
23  """
24 
25  def __init__(self, params, lr=1.0, rho=0.9, eps=1e-6, weight_decay=0):
26  if not 0.0 <= lr:
27  raise ValueError("Invalid learning rate: {}".format(lr))
28  if not 0.0 <= rho <= 1.0:
29  raise ValueError("Invalid rho value: {}".format(rho))
30  if not 0.0 <= eps:
31  raise ValueError("Invalid epsilon value: {}".format(eps))
32  if not 0.0 <= weight_decay:
33  raise ValueError("Invalid weight_decay value: {}".format(weight_decay))
34 
35  defaults = dict(lr=lr, rho=rho, eps=eps, weight_decay=weight_decay)
36  super(Adadelta, self).__init__(params, defaults)
37 
38  def step(self, closure=None):
39  """Performs a single optimization step.
40 
41  Arguments:
42  closure (callable, optional): A closure that reevaluates the model
43  and returns the loss.
44  """
45  loss = None
46  if closure is not None:
47  loss = closure()
48 
49  for group in self.param_groups:
50  for p in group['params']:
51  if p.grad is None:
52  continue
53  grad = p.grad.data
54  if grad.is_sparse:
55  raise RuntimeError('Adadelta does not support sparse gradients')
56  state = self.state[p]
57 
58  # State initialization
59  if len(state) == 0:
60  state['step'] = 0
61  state['square_avg'] = torch.zeros_like(p.data)
62  state['acc_delta'] = torch.zeros_like(p.data)
63 
64  square_avg, acc_delta = state['square_avg'], state['acc_delta']
65  rho, eps = group['rho'], group['eps']
66 
67  state['step'] += 1
68 
69  if group['weight_decay'] != 0:
70  grad = grad.add(group['weight_decay'], p.data)
71 
72  square_avg.mul_(rho).addcmul_(1 - rho, grad, grad)
73  std = square_avg.add(eps).sqrt_()
74  delta = acc_delta.add(eps).sqrt_().div_(std).mul_(grad)
75  p.data.add_(-group['lr'], delta)
76  acc_delta.mul_(rho).addcmul_(1 - rho, delta, delta)
77 
78  return loss
def step(self, closure=None)
Definition: adadelta.py:38