-
Notifications
You must be signed in to change notification settings - Fork 57
/
apollo.py
115 lines (93 loc) · 4.6 KB
/
apollo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# from https://raw.githubusercontent.com/XuezheMax/apollo/master/optim/apollo.py
import torch
from torch.optim.optimizer import Optimizer
class Apollo(Optimizer):
r"""Implements Atom algorithm.
Arguments:
params (iterable): iterable of parameters to optimize or dicts defining
parameter groups
lr (float): learning rate
beta (float, optional): coefficient used for computing
running averages of gradient (default: 0.9)
eps (float, optional): term added to the denominator to improve
numerical stability (default: 1e-4)
warmup (int, optional): number of warmup steps (default: 0)
init_lr (float, optional): initial learning rate for warmup (default: 0.01)
weight_decay (float, optional): weight decay coefficient (default: 0)
"""
def __init__(self, params, lr, beta=0.9, eps=1e-4, warmup=100, init_lr=0.01, weight_decay=0):
if not 0.0 < lr:
raise ValueError("Invalid learning rate value: {}".format(lr))
if not 0.0 <= eps:
raise ValueError("Invalid epsilon value: {}".format(eps))
if not 0.0 <= beta < 1.0:
raise ValueError("Invalid beta parameter at index 0: {}".format(beta))
if not 0.0 <= weight_decay:
raise ValueError("Invalid weight_decay value: {}".format(weight_decay))
if not 0.0 <= warmup:
raise ValueError("Invalid warmup updates: {}".format(warmup))
if not 0.0 <= init_lr <= 1.0:
raise ValueError("Invalid initial learning rate: {}".format(init_lr))
defaults = dict(lr=lr, beta=beta, eps=eps, warmup=warmup,
init_lr=init_lr, base_lr=lr, weight_decay=weight_decay)
super(Apollo, self).__init__(params, defaults)
def __setstate__(self, state):
super(Apollo, self).__setstate__(state)
@torch.no_grad()
def step(self, closure=None):
"""Performs a single optimization step.
Arguments:
closure (callable, optional): A closure that reevaluates the model
and returns the loss.
"""
loss = None
if closure is not None:
with torch.enable_grad():
loss = closure()
for group in self.param_groups:
for p in group['params']:
if p.grad is None:
continue
state = self.state[p]
# State initialization
if len(state) == 0:
state['step'] = 0
# Exponential moving average of gradient values
state['exp_avg_grad'] = torch.zeros_like(p, memory_format=torch.preserve_format)
# Exponential moving average of squared gradient values
state['approx_hessian'] = torch.zeros_like(p, memory_format=torch.preserve_format)
# Previous update direction
state['update'] = torch.zeros_like(p, memory_format=torch.preserve_format)
# Calculate current lr
if state['step'] < group['warmup']:
curr_lr = (group['base_lr'] - group['init_lr']) * state['step'] / group['warmup'] + group['init_lr']
else:
curr_lr = group['lr']
# Perform optimization step
grad = p.grad
if grad.is_sparse:
raise RuntimeError('Atom does not support sparse gradients.')
# Perform step weight decay
if group['weight_decay'] != 0:
grad = grad.add(p, alpha=group['weight_decay'])
beta = group['beta']
exp_avg_grad = state['exp_avg_grad']
B = state['approx_hessian']
d_p = state['update']
state['step'] += 1
bias_correction = 1 - beta ** state['step']
alpha = (1 - beta) / bias_correction
# Update the running average grad
delta_grad = grad - exp_avg_grad
exp_avg_grad.add_(delta_grad, alpha=alpha)
denom = d_p.norm(p=4).add(group['eps'])
d_p.div_(denom)
v_sq = d_p.mul(d_p)
delta = delta_grad.div_(denom).mul_(d_p).sum().mul(-alpha) - B.mul(v_sq).sum()
# Update B
B.addcmul_(v_sq, delta)
# calc direction of parameter updates
denom = B.abs().clamp_(min=1)
d_p.copy_(exp_avg_grad.div(denom))
p.add_(d_p, alpha=-curr_lr)
return loss