-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOptimizers.py
28 lines (22 loc) · 901 Bytes
/
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
import numpy as np
class IOptimizer(object):
def Optimize(self, theta, dtheta, cache):
raise NotImplementedError('Calling an abstract method')
class AdamOptimizer(IOptimizer):
def __init__(self, learning_rate, beta1, beta2):
self.learning_rate = learning_rate
self.beta1 = beta1
self.beta2 = beta2
def Optimize(self, theta, dtheta, cache):
m = cache.get('m', np.zeros_like(theta))
v = cache.get('v', np.zeros_like(theta))
t = cache.get('t', 0)
t += 1
m = self.beta1 * m + (1 - self.beta1) * dtheta
v = self.beta2 * v + (1 - self.beta2) * dtheta**2
corrected_m = m/(1 - self.beta1**t)
corrected_v = v/(1 - self.beta2**t)
cache['t'] = t
cache['m'] = m
cache['v'] = v
return theta + (-self.learning_rate * corrected_m / (np.sqrt(corrected_v) + 1e-8)), cache