-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizers.py
139 lines (107 loc) · 5.55 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
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
import numpy as np
from sklearn.metrics import accuracy_score
import math
class AdamOptimizer:
def __init__(self, B1=0.9, B2=0.999, epsilon=1e-7, learning_rate=0.001):
self.B1 = B1
self.B2 = B2
self.epsilon = epsilon
self.learning_rate = learning_rate
self.Vd = dict()
self.Sd = dict()
self.L = None
self.t = 0
def init_params(self, layers):
self.L = len(layers)
for l in range(1, self.L):
self.Vd[f"W{l}"] = np.zeros((layers[l], layers[l-1]))
self.Vd[f"b{l}"] = np.zeros((layers[l], 1))
self.Sd[f"W{l}"] = np.zeros((layers[l], layers[l-1]))
self.Sd[f"b{l}"] = np.zeros((layers[l], 1))
def take_step(self, derivatives):
final_derivatives = dict()
for l in range(1, self.L):
self.t += 1
self.Vd[f"W{l}"] = self.B1 * self.Vd[f"W{l}"] + (1 - self.B1) * derivatives[f"dW{l}"]
self.Vd[f"b{l}"] = self.B1 * self.Vd[f"b{l}"] + (1 - self.B1) * derivatives[f"db{l}"]
self.Sd[f"W{l}"] = self.B2 * self.Sd[f"W{l}"] + (1 - self.B2) * derivatives[f"dW{l}"]**2
self.Sd[f"b{l}"] = self.B2 * self.Sd[f"b{l}"] + (1 - self.B2) * derivatives[f"db{l}"]**2
lr = self.learning_rate * np.sqrt(1 - np.power(self.B2, self.t)) / (1 - np.power(self.B1, self.t))
final_derivatives[f"dW{l}"] = lr * self.Vd[f"W{l}"] / (np.sqrt(self.Sd[f"W{l}"]) + self.epsilon)
final_derivatives[f"db{l}"] = lr * self.Vd[f"b{l}"] / (np.sqrt(self.Sd[f"b{l}"]) + self.epsilon)
# print(list(final_derivatives.items())[0])
return final_derivatives
class Momentum:
def __init__(self, B1=0.9, learning_rate=0.001):
self.learning_rate = learning_rate
self.B1 = B1
self.Vd = dict()
self.L = None
self.t = 0
def init_params(self, layers):
self.L = len(layers)
for l in range(1, self.L):
self.Vd[f"W{l}"] = np.zeros((layers[l], layers[l-1]))
self.Vd[f"b{l}"] = np.zeros((layers[l], 1))
def take_step(self, derivatives):
final_derivatives = dict()
for l in range(1, self.L):
self.t += 1
self.Vd[f"W{l}"] = (self.B1 * self.Vd[f"W{l}"] + self.learning_rate * derivatives[f"dW{l}"]) #/ (1 - np.power(self.B1, self.t))
self.Vd[f"b{l}"] = (self.B1 * self.Vd[f"b{l}"] + self.learning_rate * derivatives[f"db{l}"]) #/ (1 - np.power(self.B1, self.t))
final_derivatives[f"dW{l}"] = self.Vd[f"W{l}"]
final_derivatives[f"db{l}"] = self.Vd[f"b{l}"]
# print(list(final_derivatives.items())[0])
return final_derivatives
class RMSProp:
def __init__(self, B2=0.9, epsilon=1e-7, learning_rate=0.001):
self.B2 = B2
self.epsilon = epsilon
self.learning_rate = learning_rate
self.Sd = dict()
self.L = None
self.t = 0
def init_params(self, layers):
self.L = len(layers)
for l in range(1, self.L):
self.Sd[f"W{l}"] = np.zeros((layers[l], layers[l-1]))
self.Sd[f"b{l}"] = np.zeros((layers[l], 1))
def take_step(self, derivatives):
final_derivatives = dict()
for l in range(1, self.L):
self.t += 1
self.Sd[f"W{l}"] = (self.B2 * self.Sd[f"W{l}"] + (1 - self.B2) * derivatives[f"dW{l}"]**2) / (1 - np.power(self.B2, self.t))
self.Sd[f"b{l}"] = (self.B2 * self.Sd[f"b{l}"] + (1 - self.B2) * derivatives[f"db{l}"]**2) / (1 - np.power(self.B2, self.t))
final_derivatives[f"dW{l}"] = self.learning_rate * derivatives[f"dW{l}"] / (np.sqrt(self.Sd[f"W{l}"]) + self.epsilon)
final_derivatives[f"db{l}"] = self.learning_rate * derivatives[f"db{l}"] / (np.sqrt(self.Sd[f"b{l}"]) + self.epsilon)
# print(list(final_derivatives.items())[0])
return final_derivatives
class AdaDelta:
def __init__(self, B2=0.9, epsilon=1e-7):
self.B2 = B2
self.epsilon = epsilon
self.Vd = dict()
self.Wd = dict()
self.L = None
self.t = 0
def init_params(self, layers):
self.L = len(layers)
for l in range(1, self.L):
self.Vd[f"W{l}"] = np.zeros((layers[l], layers[l-1]))
self.Vd[f"b{l}"] = np.zeros((layers[l], 1))
self.Wd[f"W{l}"] = np.zeros((layers[l], layers[l-1]))
self.Wd[f"b{l}"] = np.zeros((layers[l], 1))
def take_step(self, derivatives):
final_derivatives = dict()
for l in range(1, self.L):
self.t += 1
self.Vd[f"W{l}"] = self.B2 * self.Vd[f"W{l}"] + (1 - self.B2) * np.square(derivatives[f"dW{l}"])
self.Vd[f"b{l}"] = self.B2 * self.Vd[f"b{l}"] + (1 - self.B2) * np.square(derivatives[f"db{l}"])
dW = (np.sqrt(self.Wd[f"W{l}"] + self.epsilon)) / (np.sqrt(self.Vd[f"W{l}"] + self.epsilon))
db = (np.sqrt(self.Wd[f"b{l}"] + self.epsilon)) / (np.sqrt(self.Vd[f"b{l}"] + self.epsilon))
self.Wd[f"W{l}"] = self.B2 * self.Wd[f"W{l}"] + (1 - self.B2) * np.square(dW)
self.Wd[f"b{l}"] = self.B2 * self.Wd[f"b{l}"] + (1 - self.B2) * np.square(db)
final_derivatives[f"dW{l}"] = dW
final_derivatives[f"db{l}"] = db
# print(list(final_derivatives.items())[0])
return final_derivatives