-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain_1d_20190924.py
518 lines (422 loc) · 17.9 KB
/
main_1d_20190924.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#author: Zhiqin Xu 许志钦
#email: [email protected]
#2019-09-24
# coding: utf-8
'''
Reference:
1 Training behavior of deep neural network in frequency domain: https://arxiv.org/abs/1807.01251
2 Frequency Principle: Fourier Analysis Sheds Light on Deep Neural Networks: https://arxiv.org/abs/1901.06523
3 Explicitizing an Implicit Bias of the Frequency Principle in Two-layer Neural Networks: https://arxiv.org/abs/1905.10264
4 Theory of the Frequency Principle for General Deep Neural Networks: https://arxiv.org/abs/1906.09235
'''
#author: Zhiqin Xu 许志钦
#email: [email protected]
#2019-09-24
# coding: utf-8
#import sys
import matplotlib
matplotlib.use('Agg')
# In[2]:
import pickle
import time, os
import shutil
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from BasicFunc import mySaveFig, univAprox
from BasicFunc import my_fft, SelectPeakIndex
# In[3]:
isShowPic=0
Leftp=0.18
Bottomp=0.18
Widthp=0.88-Leftp
Heightp=0.9-Bottomp
pos=[Leftp,Bottomp,Widthp,Heightp]
ComputeStepFFT=20
LowFreqDrawAdd=1e-5 # for plot. plot y+this number, in case of 0 in log-log
SD=0.0 ### noise standard deviation in sample data, used in the fitting function
# y_name='|x|'
#y_name='sigmoid'
y_name='sinx'
# y_name='inv x'
def sigmoid(xx):
return (1 / (1 + np.exp(-xx)))
def func0(xx,SD):
y_sin=np.sin(xx)+np.sin(4*xx)#+np.sin(5*xx)
return y_sin
### discretized the func0 by sin_div
def func_to_approx(xx,sin_div,SD):
y_sin=func0(xx,SD)
if sin_div==0:
return y_sin
out_y = np.round(y_sin/sin_div)
out_y2 = out_y * sin_div
return out_y2
R_variable={} ### used for saved all parameters and data
### mkdir a folder to save all output
R_variable['iscontinue']=0
if R_variable['iscontinue']:
FolderName='Errordata/%s/'%('50129')
else:
BaseDir = 'Errordata/'
subFolderName = '%s'%(int(np.absolute(np.random.normal([1])*100000))//int(1))
FolderName = '%s%s/'%(BaseDir,subFolderName)
if not os.path.isdir(BaseDir):
os.mkdir(BaseDir)
os.mkdir(FolderName)
os.mkdir('%smodel/'%(FolderName))
R_variable['FolderName']=FolderName
### initialization standard deviation
R_variable['astddev']=0.05 # for weight
R_variable['bstddev']=0.05 # for bias terms2
### the length to discretized the continuous function
R_variable['sin_div']=0
### noise standard deviation in sample data, used in the fitting function
R_variable['SD']=SD
### hidden layer structure
# R_variable['hidden_units']=[20,10]
# R_variable['hidden_units']=[40,20]
R_variable['hidden_units']=[200,200,200,100]
#R_variable['hidden_units']=[1500,1500,500,500]
#R_variable['hidden_units']=[1]
# R_variable['hidden_units']=[800,800,400,400]
R_variable['learning_rate']=2e-5
R_variable['learning_rateDecay']=0
R_variable['rateDecayStep']=2000
### setup for activation function
R_variable['seed']=0
R_variable['ActFuc']=1 ### 0: ReLU; 1: Tanh; 3:sin;4: x**5,, 5: sigmoid 6 sigmoid derivate
R_variable['train_size']=61; ### training size
R_variable['batch_size']=int(np.floor(R_variable['train_size'])) ### batch size
R_variable['test_size']=int(201) ### test size
R_variable['x_start']=-10 #math.pi*3 ### start point of input
R_variable['x_end']=10 #math.pi*3 ### end point of input
R_variable['isRec_y_test']=0
R_variable['isFFT']=1 #compute FFT or not
R_variable['ismovie']=0 # make a training movie
R_variable['tol']=3e-3
R_variable['Total_Step']=600000 ### the training step. Set a big number, if it converges, can manually stop training
R_variable['Record_Step']=1 ### every R_step compute Entropy or other values
R_variable['id']=0 ### index for how many step recorded
R_variable['y_name']=y_name ### the target fitting function
R_variable['FolderName']=FolderName ### folder for save images
# initialization for variables
x_end=R_variable['x_end']
R_variable['test_inputs'] =np.reshape(np.linspace(R_variable['x_start'], R_variable['x_end'], num=R_variable['test_size'],
endpoint=True),[R_variable['test_size'],1])
R_variable['train_inputs']=np.reshape(np.linspace(R_variable['x_start'], R_variable['x_end'], num=R_variable['train_size'],
endpoint=True),[R_variable['train_size'],1])
# ###randomly select training set from test set
# indperm = np.random.permutation(test_size)
# ind = indperm[0:Size_in]
# R_variable['train_inputs']=R_variable['test_inputs'][ind]
# ###randomly select training set from test set
R_variable['loss_test']=[]
R_variable['loss_train']=[]
R_variable['fft_fit_test']=[]
R_variable['fft_true_test']=[]
R_variable['fft_0_train']=[]
R_variable['fft_fit_train']=[]
R_variable['fft_true_train']=[]
R_variable['fft_0_test']=[]
R_variable['y_test_all']=[]
R_variable['y_train_all']=[]
R_variable['y_test']=[]
R_variable['y_true_test']=[]
sin_div=R_variable['sin_div']
test_inputs=R_variable['test_inputs']
train_inputs=R_variable['train_inputs']
t0=time.time()
# In[16]:
y_0_test = func0(test_inputs,0)
y_0_train = func0(train_inputs,0)
y_true_test = func_to_approx(test_inputs,R_variable['sin_div'],R_variable['SD'])
y_true_train = func_to_approx(train_inputs,R_variable['sin_div'],R_variable['SD'])
R_variable['y_0_train']=y_0_train
R_variable['y_0_test']=y_0_test
R_variable['y_true_test']= y_true_test
R_variable['y_true_train']=y_true_train
if R_variable['isFFT']:
fft_0_test=my_fft(y_0_test,ComputeStepFFT)
R_variable['fft_0_test']=fft_0_test
fft_0_train=my_fft(y_0_train,ComputeStepFFT)
R_variable['fft_0_train']=fft_0_train
fft_true_test=my_fft(y_true_test,ComputeStepFFT)
R_variable['fft_true_test']=fft_true_test
fft_true_train=my_fft(y_true_train,ComputeStepFFT)
R_variable['fft_true_train']=fft_true_train
### compute a FFT for FFT Training.
fft_y_true_train=np.fft.fft(np.squeeze(y_true_train))
if R_variable['isFFT']:
Sel_fre_true=np.array(R_variable['fft_true_train'][0:ComputeStepFFT])
Peak_ind1=SelectPeakIndex(Sel_fre_true, endpoint=False)
Peak_ind=Peak_ind1
Hand_Add_peak=[1] # sometimes, first few points are also very important. such as x^2
Hand_Add_peak=[]
Peak_ind=np.concatenate([Peak_ind,Hand_Add_peak],axis=0)
Peak_ind=np.sort(Peak_ind)
Peak_len=len(Peak_ind)
Peak_ind=np.int32(Peak_ind)
Peak_id=0 # count peak for peak training
#tf.reset_default_graph()
with tf.variable_scope('Graph',reuse=tf.AUTO_REUSE) as scope:
# Our inputs will be a batch of values taken by our functions
x = tf.placeholder(tf.float32, shape=[None, 1], name="x")
y_true = tf.placeholder_with_default(input=[[0.0]], shape=[None, 1], name="y")
y,w_Sess,b_Sess,L2w_all_out = univAprox(x, R_variable['hidden_units'],
astddev=R_variable['astddev'],bstddev=R_variable['bstddev'],
ActFuc=R_variable['ActFuc'],seed=R_variable['seed'])
with tf.variable_scope('Loss',reuse=tf.AUTO_REUSE):
loss=tf.reduce_mean(tf.square(y - y_true))
# We define our train operation using the Adam optimizer
learning_rate=R_variable['learning_rate']
adam = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = adam.minimize(loss)
# train_op = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
saver = tf.train.Saver()
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)
if R_variable['iscontinue']:
saver.restore(sess, "%smodel/model.ckpt"%(FolderName))
else:
sess.run(tf.global_variables_initializer())
for i in range(R_variable['Total_Step']):
if (i) % R_variable['Record_Step'] == 0:
y_test, loss_test_tmp,w_tmp,b_tmp= sess.run([y,loss,w_Sess,b_Sess],
feed_dict={x: test_inputs, y_true: y_true_test})
y_train,loss_train_tmp = sess.run([y,loss],feed_dict={x: train_inputs, y_true: y_true_train})
if i==0:
y_test_ini=y_test
R_variable['y_test_ini']=y_test_ini
R_variable['loss_test'].append(loss_test_tmp)
R_variable['loss_train'].append(loss_train_tmp)
if R_variable['isRec_y_test']:
R_variable['y_test_all'].append(np.squeeze(y_test))
if R_variable['isFFT']:
R_variable['fft_fit_test'].append(my_fft(y_test,ComputeStepFFT))
R_variable['fft_fit_train'].append(my_fft(y_train,ComputeStepFFT))
if loss_train_tmp<R_variable['tol']:
print('total step:%s; total error:%s'%(i,loss_train_tmp))
break
indperm = np.random.permutation(R_variable['train_size'])
ind = indperm[0:R_variable['batch_size']]
_ = sess.run(train_op, feed_dict={x: train_inputs[ind], y_true: y_true_train[ind]})
if (i%250==0 and i<2000000):
print('batch: %d, test loss: %f' % (i + 1, R_variable['loss_test'][-1]))
print('batch: %d, train loss: %f' % (i + 1, R_variable['loss_train'][-1]))
R_variable['y_test']=y_test
R_variable['y_train']=y_train
t1=time.time()
print('time cost:%s'%(t1-t0))
shutil.rmtree('%smodel/'%(FolderName))
os.mkdir('%smodel/'%(FolderName))
save_path = saver.save(sess, "%smodel/model.ckpt"%(FolderName))
with open('%s/objs.pkl'%(FolderName), 'wb') as f: # Python 3: open(..., 'wb')
pickle.dump(R_variable, f, protocol=4)
text_file = open("%s/Output.txt"%(FolderName), "w")
for para in R_variable:
if np.size(R_variable[para])>20:
continue
text_file.write('%s: %s\n'%(para,R_variable[para]))
text_file.close()
plt.figure()
ax=plt.gca()
plt.plot(train_inputs, y_train,'m.', label='Train_fit')
plt.plot(train_inputs, R_variable['y_true_train'],'b-', label='Train_true')
plt.legend(fontsize=16)
plt.xlabel('x',fontsize=18)
plt.ylabel('y',fontsize=18)
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
plt.title('epoch: %s'%(i),fontsize=18)
ax.set_position(pos, which='both')
fntmp = '%sytrain%s'%(R_variable['FolderName'],i)
mySaveFig(plt,fntmp,ax=ax,iseps=0)
plt.figure()
ax=plt.gca()
plt.plot(test_inputs, y_test,'r.', label='Test_fit')
plt.plot(test_inputs, R_variable['y_true_test'],'g-', label='Test_true')
plt.legend(fontsize=16)
plt.xlabel('x',fontsize=18)
plt.ylabel('y',fontsize=18)
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
plt.title('epoch: %s'%(i),fontsize=18)
ax.set_position(pos, which='both')
fntmp = '%sytest%s'%(R_variable['FolderName'],i)
mySaveFig(plt,fntmp,ax=ax,iseps=0)
plt.figure()
ax = plt.gca()
y1 = R_variable['loss_test']
y2 = R_variable['loss_train']
plt.plot(y1,'ro',label='Test')
plt.plot(y2,'g*',label='Train')
#ax.set_xscale('log')
ax.set_yscale('log')
plt.xlabel('epoch',fontsize=18)
plt.ylabel('loss',fontsize=18)
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
plt.legend(fontsize=18)
ax.set_position(pos, which='both')
fntmp = '%sloss'%(FolderName)
mySaveFig(plt,fntmp,ax=ax,isax=1,iseps=0)
if R_variable['isFFT']:
plt.figure()
ax=plt.gca()
y1 = R_variable['fft_true_train'] / R_variable['train_size']
y2 = R_variable['fft_fit_train'][-1] / R_variable['train_size']
plt.semilogy(y1+LowFreqDrawAdd,'ro-',label='Trn_true')
plt.semilogy(y2+LowFreqDrawAdd,'g*-',label='Trn_fit')
plt.xlabel('freq index',fontsize=18)
plt.ylabel('|FFT|',fontsize=18)
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
plt.legend(fontsize=18)
plt.title('Train, epoch: %s'%(i),fontsize=18)
ax.set_position(pos, which='both')
fntmp = '%strainfft%s'%(FolderName,i)
mySaveFig(plt, fntmp,ax=ax,iseps=0)
plt.figure()
ax=plt.gca()
y1 = R_variable['fft_true_test'] / R_variable['test_size']
y2 = R_variable['fft_fit_test'][-1] / R_variable['test_size']
plt.semilogy(y1+LowFreqDrawAdd,'ro-',label='Trn_true')
plt.semilogy(y2+LowFreqDrawAdd,'g*-',label='Trn_fit')
plt.xlabel('freq index',fontsize=18)
plt.ylabel('|FFT|',fontsize=18)
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
plt.legend(fontsize=18)
plt.title('Test, epoch: %s'%(i),fontsize=18)
ax.set_position(pos, which='both')
fntmp = '%stestfft%s'%(FolderName,i)
mySaveFig(plt, fntmp,ax=ax,iseps=0)
print("for over")
R_variable['traintime']=time.time()-t0
print(R_variable['traintime'])
#save data
with open('%s/objs.pkl'%(FolderName), 'wb') as f: # Python 3: open(..., 'wb')
pickle.dump(R_variable, f, protocol=4)
text_file = open("%s/Output.txt"%(FolderName), "w")
for para in R_variable:
if np.size(R_variable[para])>20:
# print(para)
continue
text_file.write('%s: %s\n'%(para,R_variable[para]))
text_file.close()
#with open('objs.pkl', 'rb') as f: # Python 3: open(..., 'rb')
# R_variable = pickle.load(f)
#FolderName=''
test_inputs=R_variable['test_inputs']
plt.figure()
ax=plt.gca()
plt.plot(test_inputs, R_variable['y_test_ini'],'g-', label='initial')
plt.legend(fontsize=18)
ax.set_xlabel('x',fontsize=18)
ax.set_ylabel('y',fontsize=18)
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
ax.set_position(pos, which='both')
fntmp = '%sy_ini'%(FolderName)
mySaveFig(plt, fntmp,ax=ax)
plt.figure()
ax=plt.gca()
plt.plot(train_inputs, y_train,'m.', label='Train_fit')
plt.plot(train_inputs, R_variable['y_true_train'],'b-', label='Train_true')
plt.legend(fontsize=16)
plt.xlabel('x',fontsize=18)
plt.ylabel('y',fontsize=18)
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
plt.title('epoch: %s'%(i),fontsize=18)
ax.set_position(pos, which='both')
fntmp = '%sytrain'%(R_variable['FolderName'])
mySaveFig(plt,fntmp,ax=ax,iseps=0)
plt.figure()
ax=plt.gca()
plt.plot(test_inputs, y_test,'r.', label='Test_fit')
plt.plot(test_inputs, R_variable['y_true_test'],'g-', label='Test_true')
plt.legend(fontsize=16)
plt.xlabel('x',fontsize=18)
plt.ylabel('y',fontsize=18)
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
plt.title('epoch: %s'%(i),fontsize=18)
ax.set_position(pos, which='both')
fntmp = '%sytest'%(R_variable['FolderName'])
mySaveFig(plt,fntmp,ax=ax,iseps=0)
plt.figure()
ax = plt.gca()
y1 = R_variable['loss_test']
y2 = R_variable['loss_train']
plt.plot(y1,'ro',label='Test')
plt.plot(y2,'g*',label='Train')
#ax.set_xscale('log')
ax.set_yscale('log')
plt.xlabel('epoch',fontsize=18)
plt.ylabel('loss',fontsize=18)
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
plt.legend(fontsize=18)
ax.set_position(pos, which='both')
fntmp = '%sloss'%(FolderName)
mySaveFig(plt,fntmp,ax=ax,isax=1,iseps=0)
plt.figure()
ax=plt.gca()
plt.plot(test_inputs, R_variable['y_true_test'],'b-', label='true')
plt.legend(fontsize=16)
ax.set_xlabel('x',fontsize=18)
ax.set_ylabel('y',fontsize=18)
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
ax.set_position(pos, which='both')
fntmp = '%sy_true'%(FolderName)
mySaveFig(plt, fntmp,ax=ax,iseps=0)
if R_variable['isFFT']:
UsedPeak=np.arange(len(Peak_ind))
# plot fft with important peak
plt.figure()
ax=plt.gca()
ind = np.arange(ComputeStepFFT)
y1 = R_variable['fft_true_train']/R_variable['train_size']
plt.semilogy( y1[0:ComputeStepFFT],'r-',label='Trn_true')
UsedPeaktmp=np.arange(len(Peak_ind))
plt.semilogy(Peak_ind[UsedPeak],y1[Peak_ind[UsedPeak]]+1e-6,'ks')
plt.ylim([1e-5,10])
ax.set_xlabel('freq index',fontsize=18)
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
ax.set_position(pos, which='both')
fntmp = '%sfftPeak'%(FolderName)
mySaveFig(plt, fntmp,ax=ax,iseps=0)
d_step_fft_true_train_all=np.zeros([ComputeStepFFT,len(R_variable['fft_fit_train'])])
abs_err_all=np.zeros([ComputeStepFFT,len(R_variable['fft_fit_train'])])
fft_train_fitAll=np.asarray(np.abs(R_variable['fft_fit_train']))
for itfes in range(ComputeStepFFT):
tmp1=fft_train_fitAll[:,itfes]
tmp2=R_variable['fft_true_train'][itfes]
d_step_fft_true_train_all[itfes,:] = (np.absolute(tmp1-tmp2))/(1e-5+tmp2)
abs_err_all[itfes,:] = np.absolute(tmp1-tmp2)
DrawDis=1
DrawLastStep=len(R_variable['fft_fit_train'])
z_min=0.1
z_max=1
plt.figure()
ax=plt.gca()
im=plt.pcolor(d_step_fft_true_train_all[Peak_ind[UsedPeak],:DrawLastStep:DrawDis], cmap='RdBu', vmin=z_min, vmax=z_max)
ax.set_xlabel('epoch',fontsize=18)
ax.set_ylabel('freq peak index',fontsize=18)
#ax.set_xscale('log')
#ax.set_xlim([1,10])
plt.rc('xtick',labelsize=18)
plt.rc('ytick',labelsize=18)
ax.set_yticks([0,1,2])
ax.set_position(pos, which='both')
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax,ticks=[0,0.2,0.4,0.6,0.8,1])
fntmp = '%speakerror'%(FolderName)
mySaveFig(plt, fntmp,ax=ax,iseps=0)