forked from snnclsr/neural_nets_from_scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizers.py
64 lines (49 loc) · 1.67 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
import numpy as np
class Optimizer:
def __init__(self, lr=0.01):
pass
def step(self):
pass
class SGD(Optimizer):
def __init__(self, lr=0.01, momentum=0.0):
self.lr = lr
self.momentum = momentum
self.v = None
def step(self, param, dparam):
if self.v is None:
self.v = np.zeros(param.shape)
self.v = self.momentum * self.v - (self.lr) * dparam
param = param + self.v
return param
class RMSProp(Optimizer):
def __init__(self, lr=0.01, decay_rate=0.99, eps=1e-6):
self.lr = lr
self.decay_rate = decay_rate
self.eps = eps
self.v = None
def step(self, param, dparam):
if self.v is None:
self.v = np.zeros(dparam.shape)
self.v = self.decay_rate * self.v + (1 - self.decay_rate) * dparam ** 2
param = param - ((self.lr * dparam) / (np.sqrt(self.v) + self.eps))
return param
class Adam(Optimizer):
def __init__(self, lr=1e-3, beta1=0.9, beta2=0.999, eps=1e-8):
self.lr = lr
self.beta1 = beta1
self.beta2 = beta2
self.eps = eps
self.t = 0
self.m = None
self.v = None
def step(self, param, dparam):
self.t += 1
if self.m is None:
self.m = np.zeros(param.shape)
self.v = np.zeros(param.shape)
self.m = self.beta1 * self.m + (1 - self.beta1) * dparam
self.v = self.beta2 * self.v + (1 - self.beta2) * (dparam ** 2)
mt = self.m / (1 - self.beta1 ** self.t)
vt = self.v / (1 - self.beta2 ** self.t)
param = param - ((self.lr * mt) / (np.sqrt(vt) + self.eps))
return param