-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathadasopt_pytorch.py
151 lines (136 loc) · 6.63 KB
/
adasopt_pytorch.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# MIT License
#
# Copyright (c) 2020 YanaiEliyahu
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Implementation of Adas."""
import math
import torch
from torch.optim import Optimizer
class Adas(Optimizer):
r"""
Introduction:
For the mathematical part see https://github.com/YanaiEliyahu/AdasOptimizer,
the `Theory` section contains the major innovation,
and then `How ADAS works` contains more low level details that are still somewhat related to the theory.
Arguments:
params (iterable): iterable of parameters to optimize or dicts defining parameter groups
lr: float > 0. Initial learning rate that is per feature/input (e.g. dense layer with N inputs and M outputs, will have N learning rates).
lr2: float >= 0. lr's Initial learning rate. (just ~1-2 per layer, additonal one because of bias)
lr3: float >= 0. lr2's fixed learning rate. (global)
beta_1: 0 < float < 1. Preferably close to 1. Second moments decay factor to update lr and lr2 weights.
beta_2: 0 < float < 1. Preferably close to 1. 1/(1 - beta_2) steps back in time that `lr`s will be optimized for, larger dataset might require more nines.
beta_3: 0 < float < 1. Preferably close to 1. Same as beta_2, but for `lr2`s.
epsilon: float >= 0. Fuzz factor. If `None`, defaults to `K.epsilon()`.
"""
def __init__(self, params,
lr = 0.001, lr2 = .005, lr3 = .0005,
beta_1 = 0.999, beta_2 = 0.999, beta_3 = 0.9999,
epsilon = 1e-8, **kwargs):
if not 0.0 <= lr:
raise ValueError("Invalid lr: {}".format(lr))
if not 0.0 <= lr2:
raise ValueError("Invalid lr2: {}".format(lr))
if not 0.0 <= lr3:
raise ValueError("Invalid lr3: {}".format(lr))
if not 0.0 <= epsilon:
raise ValueError("Invalid epsilon value: {}".format(eps))
if not 0.0 <= beta_1 < 1.0:
raise ValueError("Invalid beta_1 parameter: {}".format(betas[0]))
if not 0.0 <= beta_2 < 1.0:
raise ValueError("Invalid beta_2 parameter: {}".format(betas[1]))
if not 0.0 <= beta_3 < 1.0:
raise ValueError("Invalid beta_3 parameter: {}".format(betas[2]))
defaults = dict(lr=lr, lr2=lr2, lr3=lr3, beta_1=beta_1, beta_2=beta_2, beta_3=beta_3, epsilon=epsilon)
self._varn = None
self._is_create_slots = None
self._curr_var = None
self._lr = lr
self._lr2 = lr2
self._lr3 = lr3
self._beta_1 = beta_1
self._beta_2 = beta_2
self._beta_3 = beta_3
self._epsilon = epsilon
super(Adas, self).__init__(params, defaults)
def __setstate__(self, state):
super(Adas, self).__setstate__(state)
@torch.no_grad()
def _add(self,x,y):
x.add_(y)
return x
@torch.no_grad()
# TODO: fix variables' names being too convoluted in _derivatives_normalizer and _get_updates_universal_impl
def _derivatives_normalizer(self,derivative,beta):
steps = self._make_variable(0,(),derivative.dtype)
self._add(steps,1)
factor = (1. - (self._beta_1 ** steps)).sqrt()
m = self._make_variable(0,derivative.shape,derivative.dtype)
moments = self._make_variable(0,derivative.shape,derivative.dtype)
m.mul_(self._beta_1).add_((1 - self._beta_1) * derivative * derivative)
np_t = derivative * factor / (m.sqrt() + self._epsilon)
#the third returned value should be called when the moments is finally unused, so it's updated
return (moments,np_t,lambda: moments.mul_(beta).add_((1 - beta) * np_t))
def _make_variable(self,value,shape,dtype):
self._varn += 1
name = 'unnamed_variable' + str(self._varn)
if self._is_create_slots:
self.state[self._curr_var][name] = torch.full(size=shape,fill_value=value,dtype=dtype,device=self._curr_var.device)
return self.state[self._curr_var][name]
@torch.no_grad()
def _get_updates_universal_impl(self, grad, param):
lr = self._make_variable(value = self._lr,shape=param.shape[1:], dtype=param.dtype)
moment, deriv, f = self._derivatives_normalizer(grad,self._beta_3)
param.add_( - torch.unsqueeze(lr,0) * deriv)
lr_deriv = torch.sum(moment * grad,0)
f()
master_lr = self._make_variable(self._lr2,(),dtype=torch.float32)
m2,d2, f = self._derivatives_normalizer(lr_deriv,self._beta_2)
self._add(lr,master_lr * lr * d2)
master_lr_deriv2 = torch.sum(m2 * lr_deriv)
f()
m3,d3,f = self._derivatives_normalizer(master_lr_deriv2,0.)
self._add(master_lr,self._lr3 * master_lr * d3)
f()
@torch.no_grad()
def _get_updates_universal(self, param, grad, is_create_slots):
self._curr_var = param
self._is_create_slots = is_create_slots
self._varn = 0
return self._get_updates_universal_impl(grad,self._curr_var.data)
@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
grad = p.grad.data
if grad.is_sparse:
raise RuntimeError('Adas does not support sparse gradients')
self._get_updates_universal(p,grad,len(self.state[p]) == 0)
return loss