forked from probml/pyprobml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstability_sgd_demo.py
More file actions
150 lines (122 loc) · 3 KB
/
instability_sgd_demo.py
File metadata and controls
150 lines (122 loc) · 3 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
# Demo of gradient descent vs proximal methods on a quadratic objective.
# We show that GD can "blow up" if the initial step size is too large,
# but proximal methods are robust.
import numpy as np
np.set_printoptions(precision=3)
import matplotlib.pyplot as plt
import os
figdir = "../figures" # set this to '' if you don't want to save figures
def save_fig(fname):
if figdir:
plt.savefig(os.path.join(figdir, fname))
beta = 0.5
T0 = 5
T = 10
x0 = 0.5
lr_critical = 3 * T0**beta
lrs = [1.1*lr_critical, lr_critical, 0.5*lr_critical]
# Quadratic objective
# f(x)= 0.5 x*2
# Gradient descent
# x(t+1) = x(t) - lr*f'(x(t-1)) = (1-lr)*x(t-1)
def gd(x0, T, lr0, beta):
xs = np.zeros(T)
xs[0] = x0
for t in range(1, T):
lr = lr0/(t**beta)
xs[t] = (1-lr)*xs[t-1]
return xs
# Proximal point method
# x(t+1) = min_x [f(x) + 1/2lr * (x-x(t))^2] = x(t)/(1+lr)
def prox(x0, T, lr0, beta):
xs = np.zeros(T)
xs[0] = x0
for t in range(1, T):
lr = lr0/(t**beta)
xs[t] = xs[t-1]/(1+lr)
return xs
# Approximate proximal point, using truncated linear model of f.
# x(t+1) = min_x [ max{0,f(x(t)) + f'(x(t))*(x-x(t))} + 1/2lr * (x-x(t))^2]
# = max{-0, (1-lr)*x(t)}
def trunc(x0, T, lr0, beta):
xs = np.zeros(T)
xs[0] = x0
for t in range(1, T):
lr = lr0/(t**beta)
xs[t] = max(0, (1-lr)*xs[t-1])
return xs
expts = {}
for i, lr0 in enumerate(lrs):
x_trace = gd(x0, T, lr0, beta)
name = 'GD-{:0.3f}'.format(lr0)
expts[name] = x_trace
plt.figure()
for name in expts.keys():
plt.plot(expts[name], label=name)
plt.legend()
save_fig('instability-gd.pdf')
plt.show()
expts = {}
for i, lr0 in enumerate(lrs):
x_trace = prox(x0, T, lr0, beta)
name = 'prox-{:0.3f}'.format(lr0)
expts[name] = x_trace
plt.figure()
for name in expts.keys():
plt.plot(expts[name], label=name)
plt.legend()
save_fig('instability-prox.pdf')
plt.show()
expts = {}
for i, lr0 in enumerate(lrs):
x_trace = trunc(x0, T, lr0, beta)
name = 'trunc-{:0.3f}'.format(lr0)
expts[name] = x_trace
plt.figure()
for name in expts.keys():
plt.plot(expts[name], label=name)
plt.legend()
save_fig('instability-trunc.pdf')
plt.show()
###############
'''
beta = 0.5
T0 = 5
T = 10
lrs = np.zeros(T)
lr0 = 3 * T0**beta
lrs[0] = lr0
# learning rate schedule
for t in range(1, T):
lrs[t] = lr0/(t**beta)
# Gradient descent
xs = np.zeros(T)
xs[0] = 0.5 # random non-zero value
for t in range(1, T):
xs[t] = (1-lrs[t])*xs[t-1]
plt.figure()
plt.plot(xs, label='GD')
plt.legend()
save_fig('instability-gd.pdf')
plt.show()
# Proximal point method
ys = np.zeros(T)
ys[0] = xs[0]
for t in range(1,T):
ys[t] = ys[t-1]/(1+lrs[t])
plt.figure()
plt.plot(ys, label='Prox')
plt.legend()
save_fig('instability-prox.pdf')
plt.show()
# Aprox trunc
zs = np.zeros(T)
zs[0] = xs[0]
for t in range(1,T):
zs[t] = max(0, (1-lrs[t])*zs[t-1])
plt.figure()
plt.plot(zs, label='Aprox-Trunc')
plt.legend()
save_fig('instability-trunc.pdf')
plt.show()
'''