-
Notifications
You must be signed in to change notification settings - Fork 1
/
optimizers.py
388 lines (293 loc) · 18.7 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import numpy as np
from utils import progress_bar
def Gradient_Descent(X, y, model, epochs, batch_size, alpha_0=1e-2, learning_decay_rate_type=None, discrete_reduction=0.001, decay_rate=1, k=1, metrics=[], show=False):
"""
## GRADIENT DESCENT
Gradient Descent optimization algorithm, it can be iterpreted as a Batch, Mini-Batch or Stochastic Gradient Descent depending on the value set in the batch-size variable.\n
### Arguments:
- X: {Array-like} with the input values of the examples.
- Y: {Array-like} with the output values of the examples.
- model: {Model} Object model of the neural network which parameters wants to be optimized.
- epochs: {int} value that defines iterations desired to run the algorithm through all the batches.
- batch_size: {int} value that defines batch size in which the examples will be divide to pass through the algorithm.
- alpha_0: {float} value that defines the initial value of the learning rate.
- learning_decay_rate_type: {str} value that chooses the type of decay rate desired for the algorithm, this can be:
1. 'inverse_radical'
2. 'exponential'
3. 'inverse'
4. 'discrete'
5. if pass any diferent value default alpha-0 set as the learning rate
- discrete_reduction: {float} If learning-decay-rate-type is chose to be 'discrete', float value to reduce the value of the learning rate at the beginning of each epoch.
- decay_rate: {float} If learning-decay-rate-type is chose to be 'inverse', float value that defines the decay rate that redices the learning rate each epoch.
- k: {float} If learning-decay-rate-type is chose to be 'inverse_radical', float value that defines inversely how fast radicaly the learning decays in each epoch.
- metrics: {str[]} List of strings that define the metrics that wants to be tracked through the optimization algorithm, for now it can only be 'loss' or 'acc'
- show: {bool} Defines if the metrics and loading bar for the batches are shown in screen
### Returns:
- model: {Model} Modified model after all the epochs have been runned.
- J-history: {float[]} The recording values of all the losses evaluated in each epoch after running through all the batches.
- acc-history: {float[]} The recording values of all the accuracy evaluated in each epoch after running through all the batches.
* Note: This last 2 are return as a dictionary
* Note: variable names do not use - but underscore, the change is because underscore means something in pydocs jeje
"""
if X.shape[1] != y.shape[1]:
raise Exception('Different example size for X and y')
J_history, acc_history = [], []
for i in range(epochs):
print('Epoch {}/{}'.format(i+1, epochs))
if learning_decay_rate_type == 'inverse_radical':
alpha = (k / np.sqrt(i)) * alpha_0
elif learning_decay_rate_type == 'exponential':
alpha = (0.95 ** i) * alpha_0
elif learning_decay_rate_type == 'inverse':
alpha = (1 / (1 + decay_rate * i)) * alpha_0
elif learning_decay_rate_type == 'discrete':
alpha = alpha_0 - discrete_reduction
else:
alpha = alpha_0
for t in range(X.shape[1] // batch_size):
start = t * batch_size
end = (t + 1) * batch_size
X_batch, y_batch = X[:, start : end], y[:, start : end]
J, grads = model.cost_function(X_batch, y_batch)
dW, dB = grads
W, B = model.get_parameters()
# print('W', W)
# print('B', B)
W, B = W - alpha * dW, B - alpha * dB
model.set_parameters(W, B)
if show:
progress_bar(t+1, X.shape[1] // batch_size, length=30)
if 'loss' in metrics and show == True:
J, _ = model.cost_function(X, y)
print('Training Loss:', J, end='\t')
J_history.append(J)
if ('accuracy' in metrics or 'acc' in metrics) and show == True:
# if model.loss == 'bce' or model.loss == 'ce':
# acc = np.mean(model.forward_propagation(X)[0][-1] == y)
# else:
# pass
acc = np.mean(model.forward_propagation(X)[0][-1] == y)
print('Training accuracy:', acc, end='\t')
acc_history.append(acc)
print()
return model, {'loss': J_history, 'acc': acc_history}
def Adam(X, y, model, epochs, batch_size, alpha_0=1e-2, learning_decay_rate_type=None, discrete_reduction=0.001, decay_rate=1, k=1, beta1=0.9, beta2=0.999, momentum=0, epsilon=1e-8, metrics=[], show=False):
"""
## ADAM
Adam optimization algorithm, is an algorithm for first-order gradient.based optimization of stochastic objective functions, based on adaptive estimates of lower-order moments.
For further reference check the paper => [Adam: A Method for Stochastic Optimization](https://arxiv.org/pdf/1412.6980.pdf)
### Arguments:
- X: {Array-like} with the input values of the examples.
- Y: {Array-like} with the output values of the examples.
- model: {Model} Object model of the neural network which parameters wants to be optimized.
- epochs: {int} value that defines iterations desired to run the algorithm through all the batches.
- batch-size: {int} value that defines batch size in which the examples will be divide to pass through the algorithm.
- alpha-0: {float} value that defines the initial value of the learning rate.
- learning-decay-rate-type: {str} value that chooses the type of decay rate desired for the algorithm, this can be:
1. 'inverse-radical'
2. 'exponential'
3. 'inverse'
4. 'discrete'
5. if pass any diferent value default alpha-0 set as the learning rate
- discrete-reduction: {float} If learning-decay-rate-type is chose to be 'discrete', float value to reduce the value of the learning rate at the beginning of each epoch.
- decay-rate: {float} If learning-decay-rate-type is chose to be 'inverse', float value that defines the decay rate that redices the learning rate each epoch.
- k: {float} If learning-decay-rate-type is chose to be 'inverse-radical', float value that defines inversely how fast radicaly the learning decays in each epoch.
- beta1: {float} value that controls the exponentially weighted average of the momentum
- beta2: {float} value that controls the exponentially weighted average of the RMSProp
- momentum: {float} value that defines the starting vector for momentum and RMSprop
- epsilon: {float} Value to control, when applying RMSProp, no to divide by 0
- metrics: {string[]} List of strings that define the metrics that wants to be tracked through the optimization algorithm, for now it can only be 'loss' or 'acc'
- show: {bool} values that defines if the metrics and loading bar for the batches are shown in screen
### Returns:
- model: {Model} Modified model after all the epochs have been runned.
- J-history: {float[]} The recording values of all the losses evaluated in each epoch after running through all the batches.
- acc-history: {float[]} The recording values of all the accuracy evaluated in each epoch after running through all the batches.
* Note: This last 2 are return as a dictionary
* Note: variable names do not use - but underscore, the change is because underscore means something in pydocs jeje
"""
if X.shape[1] != y.shape[1]:
raise Exception('Different example size for X and y')
J_history, acc_history = [], []
for i in range(epochs):
print('Epoch {}/{}'.format(i+1, epochs))
if learning_decay_rate_type == 'inverse_radical':
alpha = (k / np.sqrt(i)) * alpha_0
elif learning_decay_rate_type == 'exponential':
alpha = (0.95 ** i) * alpha_0
elif learning_decay_rate_type == 'inverse':
alpha = (1 / (1 + decay_rate * i)) * alpha_0
elif learning_decay_rate_type == 'discrete':
alpha = alpha_0 - discrete_reduction
else:
alpha = alpha_0
V_dW, V_db = np.ones((np.sum([w.size for i, w in enumerate(model.W) if i != 0],))) * momentum, np.ones((np.sum([b.size for j, b in enumerate(model.b) if j != 0],))) * momentum
S_dW, S_db = np.ones((np.sum([w.size for i, w in enumerate(model.W) if i != 0],))) * momentum, np.ones((np.sum([b.size for j, b in enumerate(model.b) if j != 0],))) * momentum
for t in range(X.shape[1] // batch_size):
X_batch, y_batch = X[:, t * batch_size:(t + 1) * batch_size], y[:, t * batch_size : (t + 1) * batch_size]
J, grads = model.cost_function(X_batch, y_batch)
dW, dB = grads
V_dW, V_db = beta1 * V_dW + (1 - beta1) * dW, beta1 * V_db + (1 - beta1) * dB
S_dW, S_db = beta2 * S_dW + (1 - beta2) * (dW ** 2), beta2 * S_db + (1 - beta2) * (dB ** 2)
V_dW_corrected, V_db_corrected = V_dW / (1 - (beta1 ** (t + 1))), V_db / (1 - (beta1 ** (t + 1)))
S_dW_corrected, S_db_corrected = S_dW / (1 - (beta2 ** (t + 1))), S_db / (1 - (beta2 ** (t + 1)))
W, B = model.get_parameters()
W, B = W - alpha * (V_dW_corrected/(np.sqrt(S_dW_corrected) + epsilon)), B - alpha * (V_db_corrected/(np.sqrt(S_db_corrected) + epsilon))
# W, B = W - alpha * (V_dW/(np.sqrt(S_dW) + epsilon)), B - alpha * (V_db/(np.sqrt(S_db) + epsilon))
model.set_parameters(W, B)
if show == True:
progress_bar(t+1, X.shape[1] // batch_size, length=30)
if 'loss' in metrics and show == True:
J, _ = model.cost_function(X, y)
print('Training Loss:', J, end='\t')
J_history.append(J)
if ('accuracy' in metrics or 'acc' in metrics) and show == True:
acc = np.mean(model.forward_propagation(X)[0][-1] == y)
# if model.loss == 'bce' or model.loss == 'ce':
# acc = np.mean(model.forward_propagation(X)[0][-1] == y)
# else:
# pass
print('Training accuracy:', acc, end='\t')
acc_history.append(acc)
print()
return model, {'loss': J_history, 'acc': acc_history}
def Momentum(X, y, model, epochs, batch_size, alpha_0=1e-2, learning_decay_rate_type=None, discrete_reduction=0.001, decay_rate=1, k=1, beta=0.999, momentum=0, metrics=[], show=False):
"""
## MOMENTUM
TODO: DEFINE MOMENTUM OPTIMIZATION FUNC
### Arguments:
- X: {Array-like} with the input values of the examples.
- Y: {Array-like} with the output values of the examples.
- model: {Model} Object model of the neural network which parameters wants to be optimized.
- epochs: {int} value that defines iterations desired to run the algorithm through all the batches.
- batch-size: {int} value that defines batch size in which the examples will be divide to pass through the algorithm.
- alpha-0: {float} value that defines the initial value of the learning rate.
- learning-decay-rate-type: {string} value that chooses the type of decay rate desired for the algorithm, this can be:
1. 'inverse-radical'
2. 'exponential'
3. 'inverse'
4. 'discrete'
5. if pass any diferent value default alpha-0 set as the learning rate
- discrete-reduction: {float} If learning-decay-rate-type is chose to be 'discrete', float value to reduce the value of the learning rate at the beginning of each epoch.
- decay-rate: {float} If learning-decay-rate-type is chose to be 'inverse', float value that defines the decay rate that redices the learning rate each epoch.
- k: {float} If learning-decay-rate-type is chose to be 'inverse-radical', float value that defines inversely how fast radicaly the learning decays in each epoch.
- beta: {float} value that controls the exponentially weighted average of the momentum
- momentum: {float} value that defines the starting vector for momentum
- metrics: {string[]} List of strings that define the metrics that wants to be tracked through the optimization algorithm, for now it can only be 'loss' or 'acc'
- show: {bool} value that defines if the metrics and loading bar for the batches are shown in screen
### Returns:
- model: {Model} Modified model after all the epochs have been runned.
- J-history: {float[]} The recording values of all the losses evaluated in each epoch after running through all the batches.
- acc-history: {float[]} The recording values of all the accuracy evaluated in each epoch after running through all the batches.
* Note: This last 2 are return as a dictionary
* Note: variable names do not use - but underscore, the change is because underscore means something in pydocs jeje
"""
if X.shape[1] != y.shape[1]:
raise Exception('Different example size for X and y')
J_history, acc_history = [], []
for i in range(epochs):
print('Epoch {}/{}'.format(i+1, epochs))
if learning_decay_rate_type == 'inverse_radical':
alpha = (k / np.sqrt(i)) * alpha_0
elif learning_decay_rate_type == 'exponential':
alpha = (0.95 ** i) * alpha_0
elif learning_decay_rate_type == 'inverse':
alpha = (1 / (1 + decay_rate * i)) * alpha_0
elif learning_decay_rate_type == 'discrete':
alpha = alpha_0 - discrete_reduction
else:
alpha = alpha_0
V_dW, V_db = np.ones((np.sum([w.size for i, w in enumerate(model.W) if i != 0],))) * momentum, np.ones((np.sum([b.size for j, b in enumerate(model.b) if j != 0],))) * momentum
for t in range(X.shape[1] // batch_size):
X_batch, y_batch = X[:, t*batch_size:(t+1)*batch_size], y[:, t*batch_size:(t+1)*batch_size]
J, grads = model.cost_function(X_batch, y_batch)
dW, dB = grads
V_dW, V_db = beta * V_dW + (1 - beta) * dW, beta * V_db + (1 - beta) * dB
W, B = model.get_parameters()
W, B = W - alpha * V_dW, B - alpha * V_db
model.set_parameters(W, B)
if show == True:
progress_bar(t+1, X.shape[1] // batch_size, length=30)
if 'loss' in metrics and show == True:
J, _ = model.cost_function(X, y)
print('Training Loss:', J, end='\t')
J_history.append(J)
if ('accuracy' in metrics or 'acc' in metrics) and show == True:
if model.loss == 'bce' or model.loss == 'ce':
acc = np.mean(model.forward_propagation(X)[0][-1] == y)
else:
pass
print('Training accuracy:', acc, end='\t')
acc_history.append(acc)
print()
return model, {'loss': J_history, 'acc': acc_history}
def RMS_Prop(X, y, model, epochs, batch_size, alpha_0=1e-2, learning_decay_rate_type=None, discrete_reduction=0.001, decay_rate=1, k=1, beta=0.9, prop=0, epsilon=1e-8, metrics=[], show=False):
"""
## RMS PROP
TODO: DEFINE RMS PROP
### Arguments:
- X: {Array-like} with the input values of the examples.
- Y: {Array-like} with the output values of the examples.
- model: {Model} Object model of the neural network which parameters wants to be optimized.
- epochs: {int} value that defines iterations desired to run the algorithm through all the batches.
- batch-size: {int} value that defines batch size in which the examples will be divide to pass through the algorithm.
- alpha-0: {float} value that defines the initial value of the learning rate.
- learning-decay-rate-type: {str} value that chooses the type of decay rate desired for the algorithm, this can be:
1. 'inverse-radical'
2. 'exponential'
3. 'inverse'
4. 'discrete'
5. if pass any diferent value default alpha-0 set as the learning rate
- discrete-reduction: {float} If learning-decay-rate-type is chose to be 'discrete', float value to reduce the value of the learning rate at the beginning of each epoch.
- decay-rate: {float} If learning-decay-rate-type is chose to be 'inverse', float value that defines the decay rate that redices the learning rate each epoch.
- k: {float} If learning-decay-rate-type is chose to be 'inverse-radical', float value that defines inversely how fast radicaly the learning decays in each epoch.
- beta: {float} value that controls the exponentially weighted average of the RMSProp
- momentum: {float} value that defines the starting vector for RMSProp
- epsilon: {float} Value to control, when applying RMSProp, no to divide by 0
- metrics: {str[]} List of strings that define the metrics that wants to be tracked through the optimization algorithm, for now it can only be 'loss' or 'acc'
- show: {bool} value that defines if the metrics and loading bar for the batches are shown in screen
### Returns:
- model: {Model} Modified model after all the epochs have been runned.
- J-history: {float[]} The recording values of all the losses evaluated in each epoch after running through all the batches.
- acc-history: {float[]} The recording values of all the accuracy evaluated in each epoch after running through all the batches.
* Note: This last 2 are return as a dictionary
* Note: variable names do not use - but underscore, the change is because underscore means something in pydocs jeje
"""
if X.shape[1] != y.shape[1]:
raise Exception('Different example size for X and y')
J_history, acc_history = [], []
for i in range(epochs):
print('Epoch {}/{}'.format(i+1, epochs))
if learning_decay_rate_type == 'inverse_radical':
alpha = (k / np.sqrt(i)) * alpha_0
elif learning_decay_rate_type == 'exponential':
alpha = (0.95 ** i) * alpha_0
elif learning_decay_rate_type == 'inverse':
alpha = (1 / (1 + decay_rate * i)) * alpha_0
elif learning_decay_rate_type == 'discrete':
alpha = alpha_0 - discrete_reduction
else:
alpha = alpha_0
S_dW, S_db = np.ones((np.sum([w.size for i, w in enumerate(model.W) if i != 0],))) * prop, np.ones((np.sum([b.size for j, b in enumerate(model.b) if j != 0],))) * prop
for t in range(X.shape[1] // batch_size):
X_batch, y_batch = X[:, t*batch_size:(t+1)*batch_size], y[:, t*batch_size:(t+1)*batch_size]
J, grads = model.cost_function(X_batch, y_batch)
dW, dB = grads
S_dW, S_db = beta * S_dW + (1 - beta) * (dW ** 2), beta * S_db + (1 - beta) * (dB ** 2)
W, B = model.get_parameters()
W, B = W - alpha * (dW/(np.sqrt(S_dW) + epsilon)), B - alpha * (dB/(np.sqrt(S_db) + epsilon))
model.set_parameters(W, B)
if show:
progress_bar(t+1, X.shape[1] // batch_size, length=30)
if 'loss' in metrics and show == True:
J, _ = model.cost_function(X, y)
print('Training Loss:', J, end='\t')
J_history.append(J)
if ('accuracy' in metrics or 'acc' in metrics) and show == True:
if model.loss == 'bce' or model.loss == 'ce':
acc = np.mean(model.forward_propagation(X)[0][-1] == y)
else:
pass
print('Training accuracy:', acc, end='\t')
acc_history.append(acc)
print()
return model, {'loss': J_history, 'acc': acc_history}