-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathStochastic-gradient-descent.py
More file actions
167 lines (125 loc) · 4.6 KB
/
Stochastic-gradient-descent.py
File metadata and controls
167 lines (125 loc) · 4.6 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 5 10:45:34 2021
@author: Sarat Moka
Python code for generating contour plots to show the progress
of the gradient descent and the stochastic gradient descent algorithms
for two hypothetical example functions.
"""
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import Polygon
from descartes.patch import PolygonPatch
textsize = 15
#%%
# =============================================================================
# Function definitions
# =============================================================================
def C(i, tht):
return c[i]*(tht[0] - X[i][0])**2 + (tht[1] - X[i][1])**2
def Cost(tht):
n = X.shape[0]
cost_ = 0
for i in range(n):
cost_ += C(i, tht)
return cost_/n
def grad_C(i, tht):
return np.array([2*c[i]*(tht[0] - X[i][0]), 2*(tht[1] - X[i][1])])
def grad_Cost(tht):
n = X.shape[0]
gC = np.zeros(X.shape[1])
for i in range(n):
gC = np.add(gC, grad_C(i, tht))
return gC/n
# =============================================================================
# Gradient descent algorithm
# =============================================================================
def GradientDescent(Cost, grad_Cost, tht, alpha, gamma, niter):
g = grad_Cost(tht)
#norm = np.linalg.norm(g)
d = -g#/norm
t = tht
t_seq = [t]
d_seq = [d]
for _ in range(niter):
alpha *= gamma
# Theta update
t = t + alpha*d
t_seq.append(t)
g = grad_Cost(t)
#norm = np.linalg.norm(g)
d = -g#/norm
d_seq.append(d)
print('Final point of GD:', t)
t_seq = np.array(t_seq)
d_seq = np.array(d_seq)
return t_seq, d_seq
# =============================================================================
# Stochastic gradient descent algorithm
# =============================================================================
def StochasticGradientDescent(C, grad_C, tht, alpha, gamma, niter):
n = X.shape[0]
I = np.random.choice(n)
g = grad_C(I, tht)
#norm = np.linalg.norm(g)
d = -g#/norm
t = tht
t_seq = [t]
d_seq = [d]
for _ in range(niter):
alpha *= gamma
# Theta update
t = t + alpha*d
t_seq.append(t)
I = np.random.choice(n)
g = grad_C(I, t)
#norm = np.linalg.norm(g)
d = -g#/norm
d_seq.append(d)
print('Final point of SGD:', t)
t_seq = np.array(t_seq)
d_seq = np.array(d_seq)
return t_seq, d_seq
#%%
np.random.seed(0)
# =============================================================================
# Select one of the following two options for X
# =============================================================================
X = np.array([[1,1], [2,0], [3,1], [2,3], [1,2]])
#X = np.array([[2,2], [2,2], [2,2], [2,2], [2,2]])
# =============================================================================
# Main code starts here
# =============================================================================
c = np.array([1,2,3,2,4])
alpha_init = 0.01 # Initial alpha
gamma = 1#0.99
beta = 0.8
t_init = np.array([6, 6]) # initial point
niter = 1000 # no of iterations
t1_range = np.arange(-3, 7, 0.5)
t2_range = np.arange(-3, 7, 0.5)
A = np.meshgrid(t1_range, t2_range)
Z = Cost(A)
# =============================================================================
# Plotting
# =============================================================================
fig, ax = plt.subplots()
ax.contour(t1_range, t2_range, Z, levels=np.exp(np.arange(-100, 4.5, 0.1)), cmap='Blues')
t_seq_sgd, d_seq_sgd = StochasticGradientDescent(C, grad_C, t_init, alpha_init, gamma, niter)
ax.plot(t_seq_sgd[:, 0], t_seq_sgd[:, 1], '-b', label='SGD', alpha=1, linewidth=1)
t_seq_gd, d_seq_gd = GradientDescent(Cost, grad_Cost, t_init, alpha_init, gamma, niter)
ax.plot(t_seq_gd[:, 0], t_seq_gd[:, 1], '-r', label='GD', alpha=1, linewidth=1)
polygon1 = Polygon(X)
patch = PolygonPatch(polygon1, facecolor=[0,0.5,0], alpha=0.3, zorder=2)
x_pg, y_pg = polygon1.exterior.xy
ax.add_patch(patch)
ax.plot(x_pg, y_pg, c='g')
ax.plot(np.concatenate((X[:, 0], np.array([X[0, 0]]))), np.concatenate((X[:, 1], np.array([X[0, 1]]))), '-og')
plt.xlabel(r'$\theta_1$', fontsize=textsize)
plt.ylabel(r'$\theta_2$', fontsize=textsize)
plt.legend(loc='lower right', fontsize=textsize)
plt.plot(t_init[0], t_init[1], 'o', color='black', zorder=2)
plt.text(t_init[0]- 3.5, t_init[1], r'Initial point $\theta^{(0)}$', fontsize=textsize)
plt.rcParams["figure.figsize"] = (6,6)
plt.show()