-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimization.py
152 lines (144 loc) · 6.89 KB
/
optimization.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
152
from backpropagation import *
def gradient_descent(train_X, train_Y, epochs, l_rate, layers, L, af, af_choices, batch_size):
s = batch_size
m = s
parameters = initialize_parameters(layers, L)
costs = []
accs = []
for e in range(epochs):
for i in range(0, train_X.shape[0], s):
X = train_X[i:s + i, :]
Y = train_Y[i:s + i, :]
A, store = forward_prop(X, parameters, L, af, af_choices)
derivatives = backward_prop(X, Y, store, m, L, af, af_choices)
for l in range(1, L + 1):
parameters[f'W{str(l)}'] -= l_rate * derivatives[f'dW{str(l)}']
parameters[f'b{str(l)}'] -= l_rate * derivatives[f'db{str(l)}']
if e % 1 == 0:
AA, _ = forward_prop(train_X, parameters, L, af, af_choices)
cost = categorical_crossentropy(AA, train_Y)
acc = pred(train_X, train_Y, parameters, L, af, af_choices)
costs.append(cost)
accs.append(acc)
print(f'epoch: {e} - cost= {cost} - Train Acc= {acc}')
return parameters, costs, accs
def adagrad(train_X, train_Y, epochs, l_rate, layers, L, af, af_choices, batch_size):
s = batch_size
m = s
parameters = initialize_parameters(layers, L)
costs = []
accs = []
eps = 1e-7
r_dW = dict()
r_db = dict()
X = train_X[0:s, :]
Y = train_Y[0:s, :]
A, store = forward_prop(X, parameters, L, af, af_choices)
derivatives = backward_prop(X, Y, store, m, L, af, af_choices)
for l in range(1, L + 1):
r_dW[f'dW{str(l)}'] = np.zeros(derivatives[f'dW{str(l)}'].shape)
r_db[f'db{str(l)}'] = np.zeros(derivatives[f'db{str(l)}'].shape)
for e in range(epochs):
for i in range(0, train_X.shape[0], s):
X = train_X[i:s + i, :]
Y = train_Y[i:s + i, :]
A, store = forward_prop(X, parameters, L, af, af_choices)
derivatives = backward_prop(X, Y, store, m, L, af, af_choices)
for l in range(1, L + 1):
r_dW[f'dW{str(l)}'] += derivatives[f'dW{str(l)}'] ** 2
r_db[f'db{str(l)}'] += derivatives[f'db{str(l)}'] ** 2
parameters[f'W{str(l)}'] -= (l_rate / (eps + np.sqrt(r_dW[f'dW{str(l)}']))) * derivatives[f'dW{str(l)}']
parameters[f'b{str(l)}'] -= (l_rate / (eps + np.sqrt(r_db[f'db{str(l)}']))) * derivatives[f'db{str(l)}']
if e % 1 == 0:
AA, _ = forward_prop(train_X, parameters, L, af, af_choices)
cost = categorical_crossentropy(AA, train_Y)
acc = pred(train_X, train_Y, parameters, L, af, af_choices)
accs.append(acc)
costs.append(cost)
print(f'epoch: {e} - cost= {cost} - Train Acc= {acc}')
return parameters, costs, accs
def rmsprop(train_X, train_Y, epochs, l_rate, layers, L, af, af_choices, batch_size):
s = batch_size
m = s
parameters = initialize_parameters(layers, L)
costs = []
accs = []
eps = 1e-8
rho = 0.9
r_dW = dict()
r_db = dict()
X = train_X[0:s, :]
Y = train_Y[0:s, :]
A, store = forward_prop(X, parameters, L, af, af_choices)
derivatives = backward_prop(X, Y, store, m, L, af, af_choices)
for l in range(1, L + 1):
r_dW[f'dW{str(l)}'] = np.zeros(derivatives[f'dW{str(l)}'].shape)
r_db[f'db{str(l)}'] = np.zeros(derivatives[f'db{str(l)}'].shape)
for e in range(epochs):
for i in range(0, train_X.shape[0], s):
X = train_X[i:s + i, :]
Y = train_Y[i:s + i, :]
A, store = forward_prop(X, parameters, L, af, af_choices)
derivatives = backward_prop(X, Y, store, m, L, af, af_choices)
for l in range(1, L + 1):
r_dW[f'dW{str(l)}'] = rho * r_dW[f'dW{str(l)}'] + (1 - rho) * derivatives[f'dW{str(l)}'] ** 2
r_db[f'db{str(l)}'] = rho * r_db[f'db{str(l)}'] + (1 - rho) * derivatives[f'db{str(l)}'] ** 2
parameters[f'W{str(l)}'] -= (l_rate / np.sqrt(eps + r_dW[f'dW{str(l)}'])) * derivatives[f'dW{str(l)}']
parameters[f'b{str(l)}'] -= (l_rate / np.sqrt(eps + r_db[f'db{str(l)}'])) * derivatives[f'db{str(l)}']
if e % 1 == 0:
AA, _ = forward_prop(train_X, parameters, L, af, af_choices)
cost = categorical_crossentropy(AA, train_Y)
acc = pred(train_X, train_Y, parameters, L, af, af_choices)
accs.append(acc)
costs.append(cost)
print(f'epoch: {e} - cost= {cost} - Train Acc= {acc}')
return parameters, costs, accs
def adam(train_X, train_Y, epochs, l_rate, layers, L, af, af_choices, batch_size):
s = batch_size
m = s
parameters = initialize_parameters(layers, L)
costs = []
accs = []
eps = 1e-8
rho1 = 0.9
rho2 = 0.999
s_dW = dict()
s_db = dict()
r_dW = dict()
r_db = dict()
X = train_X[0:s, :]
Y = train_Y[0:s, :]
A, store = forward_prop(X, parameters, L, af, af_choices)
derivatives = backward_prop(X, Y, store, m, L, af, af_choices)
for l in range(1, L + 1):
s_dW[f'dW{str(l)}'] = np.zeros(derivatives[f'dW{str(l)}'].shape)
s_db[f'db{str(l)}'] = np.zeros(derivatives[f'db{str(l)}'].shape)
r_dW[f'dW{str(l)}'] = np.zeros(derivatives[f'dW{str(l)}'].shape)
r_db[f'db{str(l)}'] = np.zeros(derivatives[f'db{str(l)}'].shape)
t = 0
for e in range(epochs):
for i in range(0, train_X.shape[0], s):
X = train_X[i:s + i, :]
Y = train_Y[i:s + i, :]
A, store = forward_prop(X, parameters, L, af, af_choices)
derivatives = backward_prop(X, Y, store, m, L, af, af_choices)
t += 1
for l in range(1, L + 1):
s_dW[f'dW{str(l)}'] = rho1 * s_dW[f'dW{str(l)}'] + (1 - rho1) * derivatives[f'dW{str(l)}']
s_db[f'db{str(l)}'] = rho1 * s_db[f'db{str(l)}'] + (1 - rho1) * derivatives[f'db{str(l)}']
r_dW[f'dW{str(l)}'] = rho2 * r_dW[f'dW{str(l)}'] + (1 - rho2) * (derivatives[f'dW{str(l)}'] ** 2)
r_db[f'db{str(l)}'] = rho2 * r_db[f'db{str(l)}'] + (1 - rho2) * (derivatives[f'db{str(l)}'] ** 2)
s_dW_hat = s_dW[f'dW{str(l)}'] / (1 - (rho1 ** t))
s_db_hat = s_db[f'db{str(l)}'] / (1 - (rho1 ** t))
r_dW_hat = r_dW[f'dW{str(l)}'] / (1 - (rho2 ** t))
r_db_hat = r_db[f'db{str(l)}'] / (1 - (rho2 ** t))
parameters[f'W{str(l)}'] -= (l_rate * s_dW_hat) / (eps + np.sqrt(r_dW_hat))
parameters[f'b{str(l)}'] -= (l_rate * s_db_hat) / (eps + np.sqrt(r_db_hat))
if e % 1 == 0:
AA, _ = forward_prop(train_X, parameters, L, af, af_choices)
cost = categorical_crossentropy(AA, train_Y)
acc = pred(train_X, train_Y, parameters, L, af, af_choices)
accs.append(acc)
costs.append(cost)
print(f'epoch: {e} - cost= {cost} - Train Acc= {acc}')
return parameters, costs, accs