-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimizers.py
65 lines (51 loc) · 2.29 KB
/
optimizers.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
import numpy as np
class Optimizer(object):
def __init__(self, clip=None):
self.weight_list = {}
self.weight_params = {}
self.clip = clip
def update_dW(self, weight, dJdW):
weight.dW += dJdW + weight.L1 * np.sign(weight.get()) + weight.L2 * weight.get()
self.weight_list[weight.W.ctypes.data] = weight
def update_W(self, weight):
pass
def get_or_create_param(self, weight, param_id, param_init_val = 0.0):
if weight.W.ctypes.data not in self.weight_params:
self.weight_params[weight.W.ctypes.data] = {}
return self.weight_params[weight.W.ctypes.data].get(param_id, param_init_val)
def set_param(self, weight, param_id, param_val):
self.weight_params[weight.W.ctypes.data][param_id] = param_val
def update_model(self):
for weight in self.weight_list:
if self.clip is not None:
np.clip(self.weight_list[weight].dW, -self.clip, self.clip, out=self.weight_list[weight].dW)
self.update_W(self.weight_list[weight])
class GradientDescent(Optimizer):
def __init__(self, learning_rate, **kwargs):
super(GradientDescent, self).__init__(**kwargs)
self.learning_rate = learning_rate
def update_W(self, weight):
weight.W -= self.learning_rate * weight.dW
weight.dW.fill(0.0)
class GradientDescentMomentum(Optimizer):
def __init__(self, learning_rate, momentum, **kwargs):
super(GradientDescentMomentum, self).__init__(**kwargs)
self.learning_rate = learning_rate
self.momentum = momentum
def update_W(self, weight):
velocity = (self.momentum*self.get_or_create_param(weight,'velocity')) - (self.learning_rate*weight.dW)
self.set_param(weight, 'velocity', velocity)
weight.W += velocity
weight.dW.fill(0.0)
class AdaGrad(Optimizer):
def __init__(self, learning_rate, **kwargs):
super(AdaGrad, self).__init__(**kwargs)
self.learning_rate = learning_rate
self.delta = 1e-8
def update_W(self, weight):
r = self.get_or_create_param(weight, 'r') + np.multiply(weight.dW, weight.dW)
self.set_param(weight, 'r', r)
weight.W += -(self.learning_rate * weight.dW) / np.sqrt(r + self.delta)
weight.dW.fill(0.0)
class RmsProp():
pass