-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizers.py
133 lines (78 loc) · 4.96 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
import numpy as np
class Adam():
"""Adam Optimizer """
def __init__(self, learning_rate = 0.001, beta1 = 0.9, beta2 = 0.999):
self.learning_rate = learning_rate
self.beta1 = beta1
self.beta2 = beta2
self.epsilon = 1e-7
self.v = dict()
self.s = dict()
def optimizer_name(self):
return "Adam"
def init_params(self,layer_dims):
for i in range(1,len(layer_dims)):
self.v["dW" + str(i)] = np.zeros((layer_dims[i], layer_dims[i-1]))
self.v["db" + str(i)] = np.zeros((layer_dims[i], 1))
self.s["dW" + str(i)] = np.zeros((layer_dims[i], layer_dims[i-1]))
self.s["db" + str(i)] = np.zeros((layer_dims[i], 1))
def update_params_using_Adam(self, grads, t, layer_dims,parameters):
new_learning_rate = self.learning_rate * np.divide(np.sqrt(1 - np.power(self.beta2, t)), 1- np.power(self.beta1, t))
new_parameters = dict()
for i in range(1, len(layer_dims)):
self.v["dW" + str(i)] = self.beta1 * self.v["dW" + str(i)] + (1 - self.beta1) * grads["dW" + str(i)]
self.v["db" + str(i)] = self.beta1 * self.v["db" + str(i)] + (1 - self.beta1) * grads["db" + str(i)]
self.s["dW" + str(i)] = self.beta2 * self.s["dW" + str(i)] + (1 - self.beta2) * grads["dW" + str(i)] ** 2
self.s["db" + str(i)] = self.beta2 * self.s["db" + str(i)] + (1 - self.beta2) * grads["db" + str(i)] ** 2
new_parameters["W" + str(i)] = parameters["W" + str(i)] - new_learning_rate * np.divide(self.v["dW" + str(i)], np.sqrt(self.s["dW" + str(i)]) + self.epsilon)
new_parameters["b" + str(i)] = parameters["b" + str(i)] - new_learning_rate * np.divide(self.v["db" + str(i)], np.sqrt(self.s["db" + str(i)]) + self.epsilon)
return new_parameters
class Adamax():
"""Adamax Optimizer """
def __init__(self, learning_rate = 0.001, beta1 = 0.9, beta2 = 0.999):
self.learning_rate = learning_rate
self.beta1 = beta1
self.beta2 = beta2
self.epsilon = 1e-7
self.v = dict()
self.s = dict()
def optimizer_name(self):
return "Adamax"
def init_params(self,layer_dims):
for i in range(1,len(layer_dims)):
self.v["dW" + str(i)] = np.zeros((layer_dims[i], layer_dims[i-1]))
self.v["db" + str(i)] = np.zeros((layer_dims[i], 1))
self.s["dW" + str(i)] = np.zeros((layer_dims[i], layer_dims[i-1]))
self.s["db" + str(i)] = np.zeros((layer_dims[i], 1))
def update_params_using_Adamax(self, grads, t, layer_dims,parameters):
new_learning_rate = self.learning_rate / (1 - self.beta1 ** t)
new_parameters = dict()
for i in range(1, len(layer_dims)):
self.v["dW" + str(i)] = self.beta1 * self.v["dW" + str(i)] + (1 - self.beta1) * grads["dW" + str(i)]
self.v["db" + str(i)] = self.beta1 * self.v["db" + str(i)] + (1 - self.beta1) * grads["db" + str(i)]
self.s["dW" + str(i)] = np.maximum(self.beta2 * self.s["dW" + str(i)], np.absolute(grads["dW" + str(i)]))
self.s["db" + str(i)] = np.maximum(self.beta2 * self.s["db" + str(i)], np.absolute(grads["db" + str(i)]))
new_parameters["W" + str(i)] = parameters["W" + str(i)] - new_learning_rate * np.divide(self.v["dW" + str(i)], self.s["dW" + str(i)] + self.epsilon)
new_parameters["b" + str(i)] = parameters["b" + str(i)] - new_learning_rate * np.divide(self.v["db" + str(i)], self.s["db" + str(i)] + self.epsilon)
return new_parameters
class RmsProp():
"""RmsProp Optimizer """
def __init__(self,learning_rate = 0.1,beta = 0.9):
self.learning_rate = learning_rate
self.beta = beta
self.epsilon = 1e-8
self.v = dict()
def optimizer_name(self):
return "RMS"
def init_params(self,layer_dims):
for i in range(1,len(layer_dims)):
self.v["dW" + str(i)] = np.zeros((layer_dims[i], layer_dims[i-1]))
self.v["db" + str(i)] = np.zeros((layer_dims[i], 1))
def update_params_using_RmsProp(self,grads,layer_dims,parameters):
new_parameters = dict()
for i in range(1,len(layer_dims)):
self.v["dW" + str(i)] = self.beta * self.v["dW" + str(i)] + (1 - self.beta) * grads["dW" + str(i)]
self.v["db" + str(i)] = self.beta * self.v["db" + str(i)] + (1 - self.beta) * grads["db" + str(i)]
new_parameters["W" + str(i)] = parameters["W" + str(i)] - self.learning_rate * self.v["dW" + str(i)]
new_parameters["b" + str(i)] = parameters["b" + str(i)] - self.learning_rate * self.v["db" + str(i)]
return new_parameters