-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex3.py
303 lines (224 loc) · 8.9 KB
/
ex3.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
# # #moment
from memory_profiler import profile, memory_usage
# # Define the quadratic function (example: f(x) = ax^2 + bx + c)
import numpy as np
import matplotlib.pyplot as plt
# Define the 2D quadratic function
def quadratic_function(x):
return 5 * (x[0] - 2) ** 2 + (x[1] - 3) ** 2
# Define the gradient of the 2D quadratic function
def gradient_quadratic(x):
return np.array([10 * x[0] - 20, 2 * x[1] - 6])
def momentum_gradient_descent(x, learning_rate, momentum, num_iterations, eps=1e-3):
points = np.zeros((num_iterations, len(x)))
velocity = 0
for i in range(num_iterations):
grad = gradient_quadratic(x)
velocity = momentum * velocity + learning_rate * grad
points[i] = x
x -= velocity
if np.linalg.norm(grad) < eps:
break
return points[:i + 1], i + 1
# Hyperparameters for momentum gradient descent
initial_x = -4.0
initial_y = 3.0
learning_rate = 0.01
momentum = 0.8
num_iterations = 1000
# Perform momentum gradient descent
points, iter = momentum_gradient_descent(np.array([initial_x, initial_y]), learning_rate, momentum, num_iterations)
print(points, iter)
# Generate contour plot of the quadratic function
# Generate contour plot of the quadratic function
x = np.linspace(-4, 30, 500)
y = np.linspace(-4, 14, 500)
X, Y = np.meshgrid(x, y)
Z = quadratic_function(np.array([X, Y]))
# Create a colormap for contour lines
levels = np.arange(0, 2000, 125)
# Plot the contour lines
contour = plt.contour(X, Y, Z, levels=levels, colors=plt.cm.jet(np.linspace(0, 1, len(levels))), linewidths=0.5, )
plt.colorbar(contour, label='Function Value')
# Plot the gradient descent path
plt.plot(points[:, 0], points[:, 1], '-o', color='red', label='Gradient Descent', linewidth=1, markersize=3)
# Mark the starting point
plt.scatter([initial_x], [initial_y], color='green', marker='o', s=50, label='Start', zorder=5)
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Gradient Descent on 2D Quadratic Function')
plt.grid(True)
plt.xlim(-4, 30) # Adjust the x-axis limits
plt.ylim(-4, 14) # Adjust the y-axis limits
plt.show()
# nesterov
def nesterov_gradient_descent(x, learning_rate, alpha, num_iterations, eps=1e-3):
points = np.zeros((num_iterations, len(x)))
velocity = 0
for i in range(num_iterations):
grad = gradient_quadratic(x - alpha * velocity)
velocity = alpha * velocity + learning_rate * grad
points[i] = x
x -= velocity
if np.linalg.norm(grad) < eps:
break
return points[:i + 1], i + 1
# Hyperparameters for momentum gradient descent
learning_rate = 0.01
momentum = 0.8
num_iterations = 1000
# Perform momentum gradient descent
points, iter = nesterov_gradient_descent(np.array([initial_x, initial_y]), learning_rate, momentum, num_iterations)
print(points, iter)
# Generate contour plot of the quadratic function
# Generate contour plot of the quadratic function
x = np.linspace(-4, 30, 500)
y = np.linspace(-4, 14, 500)
X, Y = np.meshgrid(x, y)
Z = quadratic_function(np.array([X, Y]))
# Create a colormap for contour lines
levels = np.arange(0, 2000, 125)
# Plot the contour lines
contour = plt.contour(X, Y, Z, levels=levels, colors=plt.cm.jet(np.linspace(0, 1, len(levels))), linewidths=0.5, )
plt.colorbar(contour, label='Function Value')
# Plot the gradient descent path
plt.plot(points[:, 0], points[:, 1], '-o', color='red', label='Gradient Descent', linewidth=1, markersize=3)
# Mark the starting point
plt.scatter([initial_x], [initial_y], color='green', marker='o', s=50, label='Start', zorder=5)
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Gradient Descent on 2D Quadratic Function')
plt.grid(True)
plt.xlim(-4, 30) # Adjust the x-axis limits
plt.ylim(-4, 14) # Adjust the y-axis limits
plt.show()
# AdaGrad
def adagrad_gradient_descent(x, learning_rate, alpha, num_iterations, eps=1e-3):
points = np.zeros((num_iterations, len(x)))
G = 0
for i in range(num_iterations):
grad = gradient_quadratic(x)
G += grad * grad.T
points[i] = x
x -= learning_rate * grad / np.sqrt(G + eps)
if np.linalg.norm(grad) < eps:
break
return points[:i + 1], i + 1
# Hyperparameters for momentum gradient descent
learning_rate = 7
momentum = 0.8
num_iterations = 1000
# Perform momentum gradient descent
points, iter = adagrad_gradient_descent(np.array([initial_x, initial_y]), learning_rate, momentum, num_iterations)
print(points, iter)
# Generate contour plot of the quadratic function
# Generate contour plot of the quadratic function
x = np.linspace(-4, 30, 500)
y = np.linspace(-4, 14, 500)
X, Y = np.meshgrid(x, y)
Z = quadratic_function(np.array([X, Y]))
# Create a colormap for contour lines
levels = np.arange(0, 2000, 125)
# Plot the contour lines
contour = plt.contour(X, Y, Z, levels=levels, colors=plt.cm.jet(np.linspace(0, 1, len(levels))), linewidths=0.5, )
plt.colorbar(contour, label='Function Value')
# Plot the gradient descent path
plt.plot(points[:, 0], points[:, 1], '-o', color='red', label='Gradient Descent', linewidth=1, markersize=3)
# Mark the starting point
plt.scatter([initial_x], [initial_y], color='green', marker='o', s=50, label='Start', zorder=5)
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Gradient Descent on 2D Quadratic Function')
plt.grid(True)
plt.xlim(-4, 30) # Adjust the x-axis limits
plt.ylim(-4, 14) # Adjust the y-axis limits
plt.show()
# RMSProp
def rmsprop_gradient_descent(x, learning_rate, gamma, num_iterations, eps=1e-3):
points = np.zeros((num_iterations, len(x)))
G = 0
alpha = 0
for i in range(num_iterations):
grad = gradient_quadratic(x)
G = grad * grad.T
alpha = gamma * alpha + (1 - gamma) * G
points[i] = x
x -= learning_rate * grad / np.sqrt(alpha + eps)
if np.linalg.norm(grad) < eps:
break
return points[:i + 1], i + 1
# Hyperparameters for momentum gradient descent
learning_rate = 0.6
momentum = 0.9
num_iterations = 1000
# Perform momentum gradient descent
points, iter = rmsprop_gradient_descent(np.array([initial_x, initial_y]), learning_rate, momentum, num_iterations)
print(points, iter)
# Generate contour plot of the quadratic function
# Generate contour plot of the quadratic function
x = np.linspace(-4, 30, 500)
y = np.linspace(-4, 14, 500)
X, Y = np.meshgrid(x, y)
Z = quadratic_function(np.array([X, Y]))
# Create a colormap for contour lines
levels = np.arange(0, 3000, 125)
# Plot the contour lines
contour = plt.contour(X, Y, Z, levels=levels, colors=plt.cm.jet(np.linspace(0, 1, len(levels))), linewidths=0.5, )
plt.colorbar(contour, label='Function Value')
# Plot the gradient descent path
plt.plot(points[:, 0], points[:, 1], '-o', color='red', label='Gradient Descent', linewidth=1, markersize=3)
# Mark the starting point
plt.scatter([initial_x], [initial_y], color='green', marker='o', s=50, label='Start', zorder=5)
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Gradient Descent on 2D Quadratic Function')
plt.grid(True)
plt.xlim(-4, 30) # Adjust the x-axis limits
plt.ylim(-4, 14) # Adjust the y-axis limits
plt.show()
# adam
memory = []
def adam_gradient_descent(x, alpha, beta, num_iterations, lr=0.7, eps=1e-3):
points = np.zeros((num_iterations, len(x)))
m = 0
v = 0
for i in range(1, num_iterations + 1):
grad = gradient_quadratic(x)
G = grad * grad.T
m = alpha * m + (1 - alpha) * grad
v = beta * v + (1 - beta) * G
vHat = v / (1 - beta ** i)
mHat = m / (1 - alpha ** i)
points[i - 1] = x
x -= lr * mHat / (np.sqrt(vHat) + eps)
memory.append(memory_usage(max_usage=True))
if np.linalg.norm(grad) < eps:
break
return points[:i], i
learning_rate = 0.8
momentum = 0.999
num_iterations = 1000
points, iter = adam_gradient_descent(np.array([initial_x, initial_y]), learning_rate, momentum, num_iterations)
print(points, iter)
print(memory)
x = np.linspace(-4, 30, 500)
y = np.linspace(-4, 14, 500)
X, Y = np.meshgrid(x, y)
Z = quadratic_function(np.array([X, Y]))
levels = np.arange(0, 3000, 125)
contour = plt.contour(X, Y, Z, levels=levels, colors=plt.cm.jet(np.linspace(0, 1, len(levels))), linewidths=0.5, )
plt.colorbar(contour, label='Function Value')
plt.plot(points[:, 0], points[:, 1], '-o', color='red', label='Gradient Descent', linewidth=1, markersize=3)
plt.scatter([initial_x], [initial_y], color='green', marker='o', s=50, label='Start', zorder=5)
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Gradient Descent on 2D Quadratic Function')
plt.grid(True)
plt.xlim(-4, 30) # Adjust the x-axis limits
plt.ylim(-4, 14) # Adjust the y-axis limits
plt.show()