-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGradientDescent.py
343 lines (271 loc) · 9.7 KB
/
GradientDescent.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
# coding: utf-8
# In[93]:
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_blobs
import numpy as np
from random import randint
import math
from sklearn.utils import shuffle
# In[182]:
(X, y) = make_blobs(n_samples=300, n_features=2, centers=2,cluster_std=3.05,random_state=95) #Original Data
X = np.c_[np.ones((X.shape[0])), X] #For absorbing bias
# In[183]:
plt.figure()
plt.scatter(X[:, 1], X[:, 2], marker="o", c=y)
# In[184]:
con_fac = 0.0005
alpha = 0.01
epochs = 100
batchsize = 1 #same bathcsize for every algorithms
W1 = np.random.uniform(size=(X.shape[1],)) #same initial weights
W1 = tuple(W1)
# In[185]:
def gradient_descent(X,y,alpha,epochs):
W = []
W = list(W1)
loss_list = []
flag=True
num = epochs
for epoch in range(0,epochs):
prediction = 1.0 / (1 + np.exp(-(X.dot(W))))
error = prediction - y
loss = np.sum(error ** 2)
loss_list.append(loss/len(X))
gradient = X.T.dot(error) / X.shape[0]
W += -(alpha) * gradient
if(loss/len(X)<con_fac and flag==True): #for storing number of epochs to reach the convergence factor
num = epoch
flag=False
return loss_list,W,num
# In[186]:
def stochastic_gradient_descent(X,y,alpha,epochs,batchsize):
W = []
W = list(W1)
flag = True
num = epochs
loss_list = []
variance_loss_list =[] #for showing the fluctuations
for epoch in range(0,epochs):
X, y = shuffle(X, y, random_state=0)
loss_curr = []
for rand_no in range(0,len(X),batchsize):
X_new = X[rand_no:rand_no+batchsize]
y_new = y[rand_no:rand_no+batchsize]
prediction = 1.0 / (1 + np.exp(-(X_new.dot(W))))
error = prediction - y_new
loss = np.sum(error ** 2)
loss_curr.append(loss)
gradient = X_new.T.dot(error) / X_new.shape[0]
W += -(alpha) * gradient
loss_list.append(np.average(loss_curr))
if(np.average(loss_curr)<con_fac and flag==True):
num = epoch
flag = False
return loss_list,W,num
# In[187]:
def sgd_momentum(X,y,alpha,epochs,batchsize):
W = []
flag = True
num = epochs
W = list(W1)
beta=0.9
v = np.zeros(len(W))
loss_list = []
for epoch in range(0,epochs):
X, y = shuffle(X, y, random_state=0)
loss_curr = []
for rand_no in range(0,len(X),batchsize):
X_new = X[rand_no:rand_no+batchsize]
y_new = y[rand_no:rand_no+batchsize]
prediction = 1.0 / (1 + np.exp(-(X_new.dot(W))))
error = prediction - y_new
loss = np.sum(error ** 2)
loss_curr.append(loss)
gradient = X_new.T.dot(error) / X_new.shape[0]
v = (beta*(v)) + (alpha*gradient)
W -= v
loss_list.append(np.average(loss_curr))
if(np.average(loss_curr)<con_fac and flag==True):
num = epoch
flag = False
return loss_list,W,num
# In[188]:
def adagrad(X,y,alpha,epochs,batchsize):
num = epochs
W = []
W = list(W1)
flag = True
loss_list = []
grad_history = np.zeros(len(W))
fudge_factor = 1e-6
loss_list = []
for epoch in range(0,epochs):
X, y = shuffle(X, y, random_state=0)
loss_curr = []
for rand_no in range(0,len(X),batchsize):
X_new = X[rand_no:rand_no+batchsize]
y_new = y[rand_no:rand_no+batchsize]
prediction = 1.0 / (1 + np.exp(-(X_new.dot(W))))
error = prediction - y_new
loss = np.sum(error ** 2)
loss_curr.append(loss)
gradient = X_new.T.dot(error) / X_new.shape[0]
grad_history+= (gradient ** 2)
new_grad = gradient / np.sqrt(fudge_factor + (grad_history))
W += -(alpha) * new_grad
loss_list.append(np.average(loss_curr))
if(np.average(loss_curr)<con_fac and flag==True):
num = epoch
flag = False
return loss_list,W,num
# In[189]:
def adadelta(X,y,alpha,epochs,batchsize):
flag = True
num = epochs
W = []
W = list(W1)
loss_list = []
decay_grad = np.zeros(len(W))
decay_param = np.zeros(len(W))
del_W = np.zeros(len(W))
fudge_factor = 1e-6
decay_rate = 0.9
loss_list = []
for epoch in range(0,epochs):
X, y = shuffle(X, y, random_state=0)
loss_curr = []
for rand_no in range(0,len(X),batchsize):
X_new = X[rand_no:rand_no+batchsize]
y_new = y[rand_no:rand_no+batchsize]
prediction = 1.0 / (1 + np.exp(-(X_new.dot(W))))
error = prediction - y_new
loss = np.sum(error ** 2)
loss_curr.append(loss)
grad = X_new.T.dot(error) / X_new.shape[0]
decay_grad = (decay_rate*decay_grad) + ((1-decay_rate)*(grad ** 2))
RMS_decay_grad = np.sqrt(fudge_factor + (decay_grad))
del_W = - (np.sqrt(fudge_factor + (decay_param)) * grad) / RMS_decay_grad
decay_param = (decay_rate*decay_param) + ((1-decay_rate)*(del_W ** 2))
RMS_decay_param = np.sqrt(fudge_factor + (decay_param))
W += - (RMS_decay_param*grad) / RMS_decay_grad
loss_list.append(np.average(loss_curr))
if(np.average(loss_curr)<con_fac and flag==True):
num = epoch
flag = False
return loss_list,W,num
# In[190]:
def RMSprop(X,y,alpha,epochs,batchsize):
flag = True
num = epochs
W = []
W = list(W1)
loss_list = []
decay_grad = np.zeros(len(W))
decay_param = np.zeros(len(W))
fudge_factor = 1e-6
decay_rate = 0.9
loss_list = []
for epoch in range(0,epochs):
X, y = shuffle(X, y, random_state=0)
loss_curr = []
for rand_no in range(0,len(X),batchsize):
X_new = X[rand_no:rand_no+batchsize]
y_new = y[rand_no:rand_no+batchsize]
prediction = 1.0 / (1 + np.exp(-(X_new.dot(W))))
error = prediction - y_new
loss = np.sum(error ** 2)
loss_curr.append(loss)
grad = X_new.T.dot(error) / X_new.shape[0]
decay_grad = (decay_rate*decay_grad) + ((1-decay_rate)*(grad ** 2))
RMS_decay_grad = np.sqrt(fudge_factor + (decay_grad))
W += - (alpha*grad) / RMS_decay_grad
loss_list.append(np.average(loss_curr))
if(np.average(loss_curr)<con_fac and flag==True):
num = epoch
flag = False
return loss_list,W,num
# In[191]:
def Adam(X,y,alpha,epochs,batchsize):
flag = True
num = epochs
W = []
W = list(W1)
loss_list = []
m_t = np.zeros(len(W))
v_t = np.zeros(len(W))
fudge_factor = 1e-6
beta_1 = 0.9
beta_2 = 0.999
loss_list = []
t=0
for epoch in range(0,epochs):
X, y = shuffle(X, y, random_state=0)
loss_curr = []
for rand_no in range(0,len(X),batchsize):
t+=1
X_new = X[rand_no:rand_no+batchsize]
y_new = y[rand_no:rand_no+batchsize]
prediction = 1.0 / (1 + np.exp(-(X_new.dot(W))))
error = prediction - y_new
loss = np.sum(error ** 2)
loss_curr.append(loss)
g_t = X_new.T.dot(error) / X_new.shape[0]
m_t = beta_1*m_t + (1-beta_1)*g_t
v_t = beta_2*v_t + (1-beta_2)*(g_t*g_t)
m_cap = m_t/(1-(beta_1**t))
v_cap = v_t/(1-(beta_2**t))
W = W - (alpha*m_cap)/(np.sqrt(v_cap)+fudge_factor)
loss_list.append(np.average(loss_curr))
if(np.average(loss_curr)<con_fac and flag==True):
num = epoch
flag = False
return loss_list,W,num
# In[192]:
def plot_graphs(loss_list,weights,method_name):
Y = (-weights[0] - (weights[1] * X)) / weights[2]
plt.figure()
plt.scatter(X[:, 1], X[:, 2], marker="o", c=y)
plt.plot(X, Y, "r-")
fig = plt.figure()
plt.plot(np.arange(0, epochs), loss_list)
fig.suptitle("Training Loss: "+ method_name)
plt.xlabel("No. of Epochs")
plt.ylabel("Loss")
plt.show()
# In[193]:
loss_list1,weights1,num1 = gradient_descent(X,y,alpha,epochs)
plot_graphs(loss_list1, weights1, "gradient descent")
loss_list2,weights2,num2 = stochastic_gradient_descent(X,y,alpha,epochs,batchsize)
plot_graphs(loss_list2, weights2, "SGD")
loss_list3,weights3,num3 = sgd_momentum(X,y,alpha,epochs,batchsize)
plot_graphs(loss_list3, weights3,"SGD with momentum")
loss_list4,weights4,num4 = adagrad(X,y,alpha,epochs,batchsize)
plot_graphs(loss_list4, weights4, "AdaGrad")
loss_list5,weights5,num5 = adadelta(X,y,alpha,epochs,batchsize)
plot_graphs(loss_list5, weights5, "AdaDelta")
loss_list6,weights6,num6 = RMSprop(X,y,alpha,epochs,batchsize)
plot_graphs(loss_list6, weights6, "RMSprop")
loss_list7,weights7,num7 = Adam(X,y,alpha,epochs,batchsize)
plot_graphs(loss_list7, weights7, "Adam")
# In[194]:
fig = plt.figure()
plt.plot(np.arange(0, epochs), loss_list1)
plt.plot(np.arange(0, epochs), loss_list2)
plt.plot(np.arange(0, epochs), loss_list3)
plt.plot(np.arange(0, epochs), loss_list4)
plt.plot(np.arange(0, epochs), loss_list5)
plt.plot(np.arange(0, epochs), loss_list6)
plt.plot(np.arange(0, epochs), loss_list7)
plt.legend(['GD','SGD', 'SGDM', 'Adagrad', 'Adadelta', 'RMS', 'Adam'], loc='upper right')
plt.plot()
# In[195]:
iteration_num = [num1,num2,num3,num4,num5,num6,num7]
label = ['GD','SGD', 'SGDM', 'Adagrad', 'Adadelta', 'RMS', 'Adam']
# In[196]:
index = np.arange(len(label)) #bar graph
plt.bar(index, iteration_num)
plt.xlabel('Algorithm', fontsize=10)
plt.ylabel('No of Epochs', fontsize=10)
plt.xticks(index, label, fontsize=10, rotation=30)
plt.title('No. of Epochs req. to converge upto convergence factor')
plt.show()