-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_synthetic.py
313 lines (270 loc) · 15.2 KB
/
test_synthetic.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
sys.path.append("../../optimizers/penalty")
sys.path.append("../../optimizers/approxgrad")
sys.path.append("../../optimizers/rmd")
sys.path.append("../../optimizers/gd")
import numpy as np
from six.moves import xrange
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import matplotlib.backends.backend_pdf
import tensorflow as tf
from tensorflow.python.platform import flags
#from cleverhans.utils_mnist import data_mnist
#import keras
#from keras.datasets import cifar10, cifar100
#from keras.datasets import mnist
import time
import bilevel_penalty_aug_lag_synthetic
import bilevel_approxgrad_synthetic
import bilevel_rmd
import bilevel_gd
#import bilevel_invhess
import bilevel_synthetic
#############################################################################################################
lr_u = 1E-3
lr_v = 1E-4
lr_p = 1E-4
nepoch = 40001
nstep = 10
rho_0 = 1E0
lamb_0 = 1E1
eps_0 = 1E0
c_rho = 1.1
p,q,r = 10,10,10
ntrial = 20
Ts = [1,5,10]
std = 1
examples = ['SMD20','SMD21','SMD24','SMD23']
methods = ['GD','RMD','ApproxGrad','Penalty']
nTs = len(Ts)
nexamples = len(examples)
nmethods = len(methods)
nrec = len(np.arange(0,nepoch,nstep))
def main(argv=None):
tf.set_random_seed(1234)
for m,method in enumerate(methods):
for e,ex in enumerate(examples):
tf.reset_default_graph()
sess = tf.InteractiveSession()
if ex=='SMD20':
u1,u2,v1,v2,ulb,uub,vlb,vub = bilevel_synthetic.uv20(p,q,r,std)
f = bilevel_synthetic.f20(u1,u2,v1,v2)
g = bilevel_synthetic.g20(u1,u2,v1,v2)
err = bilevel_synthetic.err20(u1,u2,v1,v2)
elif ex=='SMD21':
u1,u2,v1,v2,ulb,uub,vlb,vub = bilevel_synthetic.uv21(p,q,r,std)
f = bilevel_synthetic.f21(u1,u2,v1,v2)
g = bilevel_synthetic.g21(u1,u2,v1,v2)
err = bilevel_synthetic.err21(u1,u2,v1,v2)
elif ex=='SMD22':
p_ = np.int(np.ceil(p*0.5))
A1 = np.float32(np.random.normal(size=(p_,p)))
A2 = np.float32(np.random.normal(size=(p_,p)))
# Err = P*u -> Orth proj onto row-space: P = A'*inv(AA')*A
P1 = np.matmul(A1.T,np.matmul(np.linalg.inv(np.matmul(A1,A1.T)),A1))
P2 = np.matmul(A2.T,np.matmul(np.linalg.inv(np.matmul(A2,A2.T)),A2))
u1,u2,v1,v2,ulb,uub,vlb,vub = bilevel_synthetic.uv22(p,q,r,std)
f = bilevel_synthetic.f22(u1,u2,v1,v2,A1,A2)
g = bilevel_synthetic.g22(u1,u2,v1,v2,A1,A2)
err = bilevel_synthetic.err22(u1,u2,v1,v2,P1,P2)
elif ex=='SMD22full':
p_ = p#np.int(np.ceil(p*0.5))
A1 = np.float32(np.random.normal(size=(p_,p)))
A2 = np.float32(np.random.normal(size=(p_,p)))
u1,u2,v1,v2,ulb,uub,vlb,vub = bilevel_synthetic.uv22(p,q,r,std)
f = bilevel_synthetic.f22(u1,u2,v1,v2,A1,A2)
g = bilevel_synthetic.g22(u1,u2,v1,v2,A1,A2)
err = bilevel_synthetic.err22(u1,u2,v1,v2,A1,A2)
elif ex=='SMD23':
p_ = np.int(np.ceil(p*0.5))
A1 = np.float32(np.random.normal(size=(p_,p)))
A2 = np.float32(np.random.normal(size=(p_,p)))
# Err = P*u -> Orth proj onto row-space: P = A'*inv(AA')*A
P1 = np.matmul(A1.T,np.matmul(np.linalg.inv(np.matmul(A1,A1.T)),A1))
P2 = np.matmul(A2.T,np.matmul(np.linalg.inv(np.matmul(A2,A2.T)),A2))
u1,u2,v1,v2,ulb,uub,vlb,vub = bilevel_synthetic.uv23(p,q,r,std)
f = bilevel_synthetic.f23(u1,u2,v1,v2,A1,A2)
g = bilevel_synthetic.g23(u1,u2,v1,v2,A1,A2)
err = bilevel_synthetic.err23(u1,u2,v1,v2,P1,P2)
elif ex=='SMD24':
p_ = np.int(np.ceil(p*0.5))
A1 = np.float32(np.random.normal(size=(p_,p)))
A2 = np.float32(np.random.normal(size=(p_,p)))
P1 = np.matmul(A1.T,np.matmul(np.linalg.inv(np.matmul(A1,A1.T)),A1))
P2 = np.matmul(A2.T,np.matmul(np.linalg.inv(np.matmul(A2,A2.T)),A2))
u1,u2,v1,v2,ulb,uub,vlb,vub = bilevel_synthetic.uv24(p,q,r,std)
f = bilevel_synthetic.f24(u1,u2,v1,v2,A1,A2)
g = bilevel_synthetic.g24(u1,u2,v1,v2,A1,A2)
err = bilevel_synthetic.err24(u1,u2,v1,v2,P1,P2)
if method=='GD': ## gradient descent
bl = bilevel_gd.bilevel_gd(sess,f,g,[u1,u2],[v1,v2],lr_u,lr_v,ulb,uub,vlb,vub)
for n,niter in enumerate(Ts):
errs = np.nan*np.ones((nrec,ntrial))
fs = np.nan*np.ones((nrec,ntrial))
gs = np.nan*np.ones((nrec,ntrial))
ts = np.nan*np.ones(ntrial)
for trial in range(ntrial):
print('\n%s, ex=%s, n=%d/%d, trial=%d/%d'%(method,ex,n+1,nTs,trial+1,ntrial))
sess.run(tf.global_variables_initializer())
## warm up
#for i in range(20): sess.run(bl.min_v)
cnt = 0
t0 = time.time()
for epoch in range(nepoch):
#tick = time.time()
fval,gval = bl.update({},niter)
if epoch%nstep==0:
terr = sess.run(err)
errs[cnt,trial] = terr
fs[cnt,trial] = fval
gs[cnt,trial] = gval
cnt +=1
#if np.isnan(fval) or np.isnan(gval): return#.any()
if False:#epoch%1000==0:
u1val,u2val,v1val,v2val = sess.run([u1,u2,v1,v2])
print('epoch %d: f=%f, g=%f, err=%f'%(epoch,fval,gval,terr))
tu1,tu2,tv1,tv2 = sess.run([u1,u2,v1,v2])
print('u1=%f,u2=%f,l1=%f,l2=%f'%(tu1[0],tu2[0],tv1[0],tv2[0]))
ts[trial] = time.time()-t0
print('time=%f'%(ts[trial]))
print('epoch %d: f=%f, g=%f, err=%f'%(epoch,fval,gval,terr))
##tu1,tu2,tv1,tv2 = sess.run([u1,u2,v1,v2])
##print('u1=%f,u2=%f,l1=%f,l2=%f'%(tu1,tu2,tv1,tv2))
if True:
np.save('./results/simple_ts_%s_%s_T%d.npy'%(method,ex,niter),ts)
np.save('./results/simple_errs_%s_%s_T%d.npy'%(method,ex,niter),errs)
np.save('./results/simple_fs_%s_%s_T%d.npy'%(method,ex,niter),fs)
np.save('./results/simple_gs_%s_%s_T%d.npy'%(method,ex,niter),gs)
elif method=='RMD': ## Reverse mode differentiation
bl = bilevel_rmd.bilevel_rmd(sess,f,g,[u1,u2],[v1,v2],lr_u,lr_v,ulb,uub,vlb,vub)
for n,niter in enumerate(Ts):
errs = np.nan*np.ones((nrec,ntrial))
fs = np.nan*np.ones((nrec,ntrial))
gs = np.nan*np.ones((nrec,ntrial))
ts = np.nan*np.ones(ntrial)
for trial in range(ntrial):
print('\n%s, ex=%s, n=%d/%d, trial=%d/%d'%(method,ex,n+1,nTs,trial+1,ntrial))
sess.run(tf.global_variables_initializer())
## warm up
#for i in range(20): sess.run(bl.min_v)
cnt = 0
t0 = time.time()
for epoch in range(nepoch):
fval,gval = bl.update({},niter)
if epoch%nstep==0:
terr = sess.run(err)
errs[cnt,trial] = terr
fs[cnt,trial] = fval
gs[cnt,trial] = gval
cnt +=1
#if np.isnan(fval) or np.isnan(gval): return#.any()
if epoch%1000==0:
print('epoch %d: f=%f, g=%f, err=%f'%(epoch,fval,gval,terr))
tu1,tu2,tv1,tv2 = sess.run([u1,u2,v1,v2])
print('u1=%f,u2=%f,l1=%f,l2=%f'%(tu1[0],tu2[0],tv1[0],tv2[0]))
ts[trial] = time.time()-t0
print('time=%f'%(ts[trial]))
print('epoch %d: f=%f, g=%f, err=%f'%(epoch,fval,gval,terr))
##tu1,tu2,tv1,tv2 = sess.run([u1,u2,v1,v2])
##print('u1=%f,u2=%f,l1=%f,l2=%f'%(tu1,tu2,tv1,tv2))
if True:
np.save('./results/simple_ts_%s_%s_T%d.npy'%(method,ex,niter),ts)
np.save('./results/simple_errs_%s_%s_T%d.npy'%(method,ex,niter),errs)
np.save('./results/simple_fs_%s_%s_T%d.npy'%(method,ex,niter),fs)
np.save('./results/simple_gs_%s_%s_T%d.npy'%(method,ex,niter),gs)
elif method=='ApproxGrad': ## Approximate hypergradient
bl = bilevel_approxgrad_synthetic.bilevel_approxgrad(sess,f,g,[u1,u2],[v1,v2],lr_u,lr_v,lr_p,ulb,uub,vlb,vub)
for n,niter in enumerate(Ts):
errs = np.nan*np.ones((nrec,ntrial))
fs = np.nan*np.ones((nrec,ntrial))
gs = np.nan*np.ones((nrec,ntrial))
ts = np.nan*np.ones(ntrial)
for trial in range(ntrial):
print('\n%s, ex=%s, n=%d/%d, trial=%d/%d'%(method,ex,n+1,nTs,trial+1,ntrial))
sess.run(tf.global_variables_initializer())
## warm up
#for i in range(20): sess.run(bl.min_v)
cnt = 0
t0 = time.time()
for epoch in range(nepoch):
fval,gval,hval = bl.update({},niter,niter)
if epoch%nstep==0:
terr = sess.run(err)
errs[cnt,trial] = terr
fs[cnt,trial] = fval
gs[cnt,trial] = gval
cnt +=1
#if np.isnan(fval) or np.isnan(gval): return#.any()
if epoch%1000==0:
print('epoch %d: f=%f, g=%f, h=%f, err=%f'%(epoch,fval,gval,hval,terr))
tu1,tu2,tv1,tv2 = sess.run([u1,u2,v1,v2])
print('u1=%f,u2=%f,l1=%f,l2=%f'%(tu1[0],tu2[0],tv1[0],tv2[0]))
ts[trial] = time.time()-t0
print('time=%f'%(ts[trial]))
print('epoch %d: f=%f, g=%f, h=%f, err=%f'%(epoch,fval,gval,hval,terr))
##tu1,tu2,tv1,tv2 = sess.run([u1,u2,v1,v2])
##print('u1=%f,u2=%f,l1=%f,l2=%f'%(tu1,tu2,tv1,tv2))
if True:
np.save('./results/simple_ts_%s_%s_T%d.npy'%(method,ex,niter),ts)
np.save('./results/simple_errs_%s_%s_T%d.npy'%(method,ex,niter),errs)
np.save('./results/simple_fs_%s_%s_T%d.npy'%(method,ex,niter),fs)
np.save('./results/simple_gs_%s_%s_T%d.npy'%(method,ex,niter),gs)
elif method=='Penalty': ## Penalty method
bl = bilevel_penalty_aug_lag_synthetic.bilevel_penalty(sess,f,g,[u1,u2],[v1,v2],lr_u,lr_v,rho_0,lamb_0,eps_0,c_rho,ulb,uub,vlb,vub)
#bl = bilevel_penalty.bilevel_penalty(sess,f,g,[u1,u2],[v1,v2],lr_u,lr_v,rho_0,lamb_0,eps_0,c_rho,c_lamb,c_eps)
for n,niter in enumerate(Ts):
errs = np.nan*np.ones((nrec,ntrial))
rhos = np.nan*np.ones((nrec,ntrial))
fs = np.nan*np.ones((nrec,ntrial))
gs = np.nan*np.ones((nrec,ntrial))
ftilde = np.nan*np.ones((nrec,ntrial))
ts = np.nan*np.ones(ntrial)
for trial in range(ntrial):
print('\n%s, ex=%s, n=%d/%d, trial=%d/%d'%(method,ex,n+1,nTs,trial+1,ntrial))
sess.run(tf.global_variables_initializer())
#bl.reset_optim()
## warm up
#for i in range(20): sess.run(bl.min_v)
cnt = 0
t0 = time.time()
for epoch in range(nepoch):
fval,gvnorm,lamb_g = bl.update({},niter)
if epoch%nstep==0:
rho_t,lamb_t,eps_t = sess.run([bl.rho_t,bl.lamb_t,bl.eps_t])
gval,terr = sess.run([bl.f,err])
errs[cnt,trial] = terr
fs[cnt,trial] = fval
gs[cnt,trial] = gval
ftilde[cnt,trial] = fval+gvnorm+lamb_g
rhos[cnt,trial] = rho_t
cnt +=1
#if np.isnan(fval) or np.isnan(gval): return#.any()
if False:#epoch%1000==0:
print('epoch %d (rho=%f, lamb=%f, eps=%f): h=%f + %f + %f= %f, err=%f'%
(epoch,rho_t,lamb_t,eps_t,fval,gvnorm,lamb_g,fval+gvnorm+lamb_g,terr))
tu1,tu2,tv1,tv2 = sess.run([u1,u2,v1,v2])
print('u1=%f,u2=%f,l1=%f,l2=%f'%(tu1[0],tu2[0],tv1[0],tv2[0]))
ts[trial] = time.time()-t0
print('time=%f'%(ts[trial]))
print('epoch %d (rho=%f, lamb=%f, eps=%f): h=%f + %f + %f= %f, err=%f'%
(epoch,rho_t,lamb_t,eps_t,fval,gvnorm,lamb_g,fval+gvnorm+lamb_g,terr))
##tu1,tu2,tv1,tv2 = sess.run([u1,u2,v1,v2])
##print('u1=%f,u2=%f,l1=%f,l2=%f'%(tu1,tu2,tv1,tv2))
if True:
np.save('./results/simple_ts_%s_%s_T%d.npy'%(method,ex,niter),ts)
np.save('./results/simple_errs_%s_%s_T%d.npy'%(method,ex,niter),errs)
np.save('./results/simple_fs_%s_%s_T%d.npy'%(method,ex,niter),fs)
np.save('./results/simple_gs_%s_%s_T%d.npy'%(method,ex,niter),gs)
np.save('./results/simple_ftilde_%s_%s_T%d.npy'%(method,ex,niter),ftilde)
np.save('./results/simple_rhos_%s_%s_T%d.npy'%(method,ex,niter),rhos)
sess.close()
return
##############################################################################################################
if __name__ == '__main__':
tf.app.run()