-
Notifications
You must be signed in to change notification settings - Fork 4
/
OptimizationSolver.m
620 lines (521 loc) · 22.7 KB
/
OptimizationSolver.m
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
function OptimizedNN=OptimizationSolver(data,label,NN,option)
% v1.1.8
NN.OptimizationHistory=zeros(2,1);
NN.StepSizeHistory=zeros(2,1);
NN.LineSearchIteration=zeros(2,1);
NN.numOfData=size(data,2); NN.MeanFactor=1/size(data,2);
if strcmp(NN.Cost,'Entropy')==1
NN.MeanFactor=1/size(data,2);
elseif strcmp(NN.Cost,'MSE')==1
NN.MeanFactor=2/size(data,2);
elseif strcmp(NN.Cost,'MAE')==1
NN.MeanFactor=1/size(data,2);
elseif strcmp(NN.Cost,'SSE')==1
NN.MeanFactor=2;
end
if isfield(option,'Solver')==0 && strcmp(NN.Cost,'Entropy')==0
option.Solver='Auto';
elseif isfield(option,'Solver')==0
option.Solver='ADAM';
end
if isfield(option,'Solver')==0
option.Solver='Auto';
end
solver=option.Solver;
NN.Solver=option.Solver;
if isfield(option,'s0')==0
option.s0=2e-3;
end
if isfield(option,'BatchSize')==0
option.BatchSize=round(size(data,2)/10);
end
if strcmp(NN.InputAutoScaling,'on')
InputScaleVector=std(data,0,2);
InputCenterVector=mean(data,2);
NN.InputCenterVector=InputCenterVector./InputScaleVector;
NN.InputScaleVector=1./InputScaleVector;
end
if strcmp(NN.LabelAutoScaling,'on')
LabelScaleVector=std(label,0,2);
LabelCenterVector=mean(label,2);
label=(label-LabelCenterVector)./LabelScaleVector;
NN.LabelCenterVector=LabelCenterVector;
NN.LabelScaleVector=LabelScaleVector;
end
if isfield(NN,'activeDerivate')==0
disp('Please provide the derivatives of activation functions.');
end
WeightedFlag=isfield(option,'weighted');
if WeightedFlag==1
NN.SampleWeight=[];
NN.Weighted=option.weighted;
NN.WeightedFlag=1;
else
NN.WeightedFlag=0;
end
switch solver
case 'BFGS'
OptimizedNN=QuasiNewtonSolver(data,label,NN,option);
case 'AdamW'
OptimizedNN=StochasticSolver(data,label,NN,option);
case 'ADAM'
OptimizedNN=StochasticSolver(data,label,NN,option);
case 'SGDM'
OptimizedNN=StochasticSolver(data,label,NN,option);
case 'SGD'
OptimizedNN=StochasticSolver(data,label,NN,option);
case 'RMSprop'
OptimizedNN=StochasticSolver(data,label,NN,option);
case 'Auto'
%------------ First Stage Optimization ----------------------
tic
if isfield(option,'MaxIteration')==1
TotalIteration=option.MaxIteration;
else
TotalIteration=800;
end
option.Solver='ADAM';
option.s0=2e-3;
option.MaxIteration=round(TotalIteration/4);
option.BatchSize=round(size(data,2)/10);
NN=StochasticSolver(data,label,NN,option);
disp('------------------------------------------------------')
DisplayWord=['First Stage Optimization Finished in ', num2str(option.MaxIteration), ' Iteration.'];
disp(DisplayWord)
disp('------------------------------------------------------')
%------------ Second Stage Optimization ----------------------
option.Solver='BFGS';
option.MaxIteration=TotalIteration-round(TotalIteration/4);
OptimizedNN=QuasiNewtonSolver(data,label,NN,option);
NN.OptimizationTime=toc;
end
NetworkType=NN.NetworkType;
switch NetworkType
case'ANN'
Net=@(x,NN) ANN(x,NN);
case 'ResNet'
Net=@(x,NN) ResNet(x,NN);
end
if strcmp(NN.LabelAutoScaling,'on')==1
OptimizedNN.Evaluate=@(x) NN.LabelScaleVector.*Net(x,OptimizedNN)+NN.LabelCenterVector;
Error=(NN.LabelScaleVector.*label+NN.LabelCenterVector)-OptimizedNN.Evaluate(data);
else
OptimizedNN.Evaluate=@(x) Net(x,OptimizedNN);
Error=label-OptimizedNN.Evaluate(data);
end
if strcmp(NN.Cost,'Entropy')==0
OptimizedNN.Derivate=@(x) AutomaticDerivate(x,OptimizedNN);
OptimizedNN.MeanAbsoluteError=sum(abs(Error),[1 2])/NN.numOfData;
disp('------------------------------------------------------')
FormatSpec = 'Max Iteration : %d , Cost : %16.8f \n';
FinalCost=CostFunction(data,label,OptimizedNN);
fprintf(FormatSpec,OptimizedNN.Iteration,FinalCost);
fprintf('Optimization Time : %5.1f\n',OptimizedNN.OptimizationTime);
fprintf('Mean Absolute Error : %8.4f\n',OptimizedNN.MeanAbsoluteError)
disp('------------------------------------------------------')
else
OptimizedNN.ComputeAccuracy=@(data,label) ComputeAccuracy(data,label,OptimizedNN);
Accuracy=OptimizedNN.ComputeAccuracy(data,label);
OptimizedNN.Predict=@(data) ClassPredict(data,OptimizedNN);
OptimizedNN.Accuracy=Accuracy;
disp('------------------------------------------------------')
FormatSpec = 'Max Iteration : %d , Cost : %16.8f \n';
FinalCost=CostFunction(data,label,OptimizedNN);
fprintf(FormatSpec,OptimizedNN.Iteration,FinalCost);
fprintf('Accuracy : %6.2f %% \n',OptimizedNN.Accuracy);
fprintf('Optimization Time : %5.1f\n',OptimizedNN.OptimizationTime);
disp('------------------------------------------------------')
end
%% Numerical Optimization Solver
function OptimizedNN=StochasticSolver(data,label,NN,option)
Counter=0;
% ------------- Select Gradient Solver----------------
NetworkType=NN.NetworkType;
if isfield(option,'GradientSolver')==0
switch NetworkType
case 'ANN'
AutoGrad=@(data,label,NN) AutomaticGradient(data,label,NN);
case'ResNet'
AutoGrad=@(data,label,NN) ElementWiseRNAG(data,label,NN);
end
else
switch NetworkType
case 'ANN'
GradientSolver=option.GradientSolver;
switch GradientSolver
case 'Element'
AutoGrad=@(data,label,NN) ElementWiseAG(data,label,NN);
case 'Column'
AutoGrad=@(data,label,NN) ColumnWiseAG(data,label,NN);
case 'General'
AutoGrad=@(data,label,NN) ComplexStepGradient(data,label,NN);
end
case'ResNet'
GradientSolver=option.GradientSolver;
switch GradientSolver
case 'Element'
AutoGrad=@(data,label,NN) ElementWiseRNAG(data,label,NN);
case 'Column'
AutoGrad=@(data,label,NN) ColumnWiseRNAG(data,label,NN);
case 'General'
AutoGrad=@(data,label,NN) ComplexStepGradient(data,label,NN);
end
end
end
% ------------- Select Gradient Solver----------------
BatchCost=zeros(2,1);
BatchSize=option.BatchSize;
tic
for j=1:option.MaxIteration
NN.Iteration=j;
if option.BatchSize==NN.numOfData
Sample.Data{1}=data; Sample.Label{1}=label;
else
Sample=Shuffle(data,label,BatchSize);
end
for k=1:numel(Sample.Label)
Counter=Counter+1;
NN.StochasticCounter=Counter;
ShuffledData=Sample.Data{k};
ShuffledLabel=Sample.Label{k};
if NN.WeightedFlag==1
NN.SampleWeight=NN.Weighted(Sample.Index{k});
end
[dw,db]=AutoGrad(ShuffledData,ShuffledLabel,NN);
NN=StochasticUpdateRule(dw,db,NN,option);
BatchCost(Counter)=CostFunction(ShuffledData,ShuffledLabel,NN);
end
CurrentCost=CostFunction(data,label,NN);
if rem(j,floor(option.MaxIteration/20))==0
FormatSpec = 'Iteration : %d , Cost : %16.8f \n';
fprintf(FormatSpec,j,CurrentCost);
end
NN.OptimizationHistory(j)=CurrentCost;
end
NN.OptimizationTime=toc; NN.BatchCost=BatchCost;
OptimizedNN=NN;
end
function OptimizedNN=QuasiNewtonSolver(data,label,NN,option)
if isfield(NN,'TerminationContion')==0
TerminationNorm=1e-5;
else
TerminationNorm=option.TerminateCondition;
end
NetworkType=NN.NetworkType;
if isfield(option,'Damping')==0
option.Damping='DoubleDamping';
end
if strcmp(NN.LineSearcher,'Off')==1
option.Damping='DoubleDamping';
end
NN.Damping=option.Damping;
% ------------- Select Gradient Solver----------------
if isfield(option,'GradientSolver')==0
switch NetworkType
case 'ANN'
AutoGrad=@(data,label,NN) AutomaticGradient(data,label,NN);
case'ResNet'
AutoGrad=@(data,label,NN) ElementWiseRNAG(data,label,NN);
end
else
switch NetworkType
case 'ANN'
GradientSolver=option.GradientSolver;
switch GradientSolver
case 'Element'
AutoGrad=@(data,label,NN) ElementWiseAG(data,label,NN);
case 'Column'
AutoGrad=@(data,label,NN) ColumnWiseAG(data,label,NN);
case 'General'
AutoGrad=@(data,label,NN) ComplexStepGradient(data,label,NN);
end
case'ResNet'
GradientSolver=option.GradientSolver;
switch GradientSolver
case 'Element'
AutoGrad=@(data,label,NN) ElementWiseRNAG(data,label,NN);
case 'Column'
AutoGrad=@(data,label,NN) ColumnWiseRNAG(data,label,NN);
case 'General'
AutoGrad=@(data,label,NN) ComplexStepGradient(data,label,NN);
end
end
end
% ------------- Select Gradient Solver----------------
H0=speye(NN.numOfWeight+NN.numOfBias);
[dwNew,dbNew]=AutoGrad(data,label,NN);
H=H0;
NN.Termination=0; NN.OptimizationFail=0;
delta=1e-3;
tic
for m=1:option.MaxIteration
NN.Iteration=m;
NN=QuasiNewtonUpdate(NN);
CurrentCost=NN.OptimizationHistory(m);
if NN.Termination==1
disp('------------------------------------------------------')
FormatSpec = 'Reach Stop Criteria in %d Iterations, Cost :%16.8f\n';
fprintf(FormatSpec,m,CurrentCost);
fprintf('First Order Optimality : %8.7f\n',NN.FirstOrderOptimality)
break
end
if NN.OptimizationFail==1
disp('Opimization Fail');
break
end
if rem(m,floor(option.MaxIteration/20))==0
FormatSpec = 'Iteration : %d , Cost : %16.8f \n';
fprintf(FormatSpec,m,CurrentCost);
end
end
NN.OptimizationTime=toc;
OptimizedNN=NN;
function UpdatedNN=QuasiNewtonUpdate(NN)
solver=option.Solver;
switch solver
case 'BFGS'
dw=dwNew; db=dbNew;
dwVec=LocalMtoV(dw);
dbVec=LocalMtoV(db);
weight0=LocalMtoV(NN.weight);
bias0=LocalMtoV(NN.bias);
p0=[weight0;bias0];
dp=[dwVec;dbVec];
if NN.Iteration==1 && strcmp(NN.LineSearcher,'Off')==0
dp=delta*dp;
end
% Quasi Newton Descent
SearchDirection=-H*dp;
NN.SearchDirection=SearchDirection;
SearchResults=LineSearch(SearchDirection,dp,data,label,NN);
if SearchResults.OptimalStep~=0
s0=SearchResults.OptimalStep;
else
s0=option.s0;
end
NN.OptimizationFail=SearchResults.Termination;
NN.StepSizeHistory(m)=s0;
NN.LineSearchIteration(m)=SearchResults.Iteration;
NN.OptimizationHistory(m)=SearchResults.Cost;
s=s0*SearchDirection;
p0=p0+s;
if NN.OptimizationFail==0
weight0=p0(1:NN.numOfWeight);
bias0=p0(NN.numOfWeight+1:end);
NN.weight=LocalVtoM(weight0);
NN.bias=LocalVtoM(bias0);
[dwNew,dbNew]=AutoGrad(data,label,NN);
dwNewVec=LocalMtoV(dwNew);
dbNewVec=LocalMtoV(dbNew);
dpNew=[dwNewVec;dbNewVec];
NN.Gradient=dpNew;
% BFGS Inverse Hessian Approximation Update
y=dpNew-dp;
rho=1/(y'*s);
NN.FirstOrderOptimality=max(abs(dpNew));
NN.rho(m)=rho;
% ------- Safegaurd -------
if NN.FirstOrderOptimality<=TerminationNorm
NN.Termination=1;
end
%-------------------------------------------------
if isfield(option,'Damping')==0
DampingCase='DoubleDamping';
else
DampingCase=option.Damping;
end
switch DampingCase
case 'DoubleDamping'
% Quasi-Newton for DNN, Yi-Ren, Goldfarb 2022
mu1=0.2; mu2=0.001;
Quadratic=y'*H*y; InvRho=s'*y;
if InvRho<mu1*Quadratic
theta=(1-mu1)*Quadratic/(Quadratic-InvRho);
NN.CurvatureConditon(m)=0;
else
theta=1;
NN.CurvatureConditon(m)=1;
end
s=theta*s+(1-theta)*H*y;
y=y+mu2*s;
%LM Damping
Rho=1/(s'*y);
H=H+(Rho^2)*(s'*y+y'*H*y)*(s*s')-Rho*(H*y*s'+s*y'*H);
case 'Powell'
% Quasi-Newton for DNN training, Goldfarb 2020 (Double Damping)
% Powell's Damping on H, B=I.
mu1=0.2; mu2=0.001;
Quadratic=y'*H*y; InvRho=s'*y;
if InvRho<mu1*Quadratic
theta=(1-mu1)*Quadratic/(Quadratic-InvRho);
NN.CurvatureConditon(m)=0;
else
theta=1;
NN.CurvatureConditon(m)=1;
end
s=theta*s+(1-theta)*H*y;
NewInvRho=s'*y; Snorm=s'*s;
if NewInvRho<mu2*Snorm
theta2=(1-mu2)*Snorm/(Snorm-NewInvRho);
else
theta2=1;
end
y=theta2*y+(1-theta2)*s;
Rho=1/(s'*y); InvRho=s'*y; Quadratic=y'*H*y;
if Quadratic*InvRho<=2/mu1
H=H+(Rho^2)*(InvRho+Quadratic)*(s*s')-Rho*(H*y*s'+s*y'*H);
end
case 'Skip'
Rho=1/(s'*y); Quadratic=y'*H*y;
if rho>1e-8
NN.CurvatureConditon(m)=1;
H=H+(Rho^2)*(s'*y+Quadratic)*(s*s')-Rho*(H*y*s'+s*y'*H);
else
NN.CurvatureConditon(m)=0;
end
case 'None'
Rho=1/(s'*y); Quadratic=y'*H*y;
H=H+(Rho^2)*(s'*y+Quadratic)*(s*s')-Rho*(H*y*s'+s*y'*H);
end
%-------------------------------------------------
NN.BFGS=H;
UpdatedNN=NN;
else
UpdatedNN=NN;
end
end
%%
function ParaStruct=LocalVtoM(v)
if numel(v)==NN.numOfWeight
NumOfVariable=0;
for i=1:NN.depth
NumOfLocalWeight=NN.LayerStruct(1,i)*NN.LayerStruct(2,i);
for j=1:NumOfLocalWeight
NumOfVariable=NumOfVariable+1;
NN.weight{i}(j)=v(NumOfVariable);
end
end
ParaStruct=NN.weight;
else
NumOfVariable=0;
for i=1:NN.depth
NumOfLocalBias=NN.LayerStruct(2,i);
for j=1:NumOfLocalBias
NumOfVariable=NumOfVariable+1;
NN.bias{i}(j)=v(NumOfVariable);
end
end
ParaStruct=NN.bias;
end
end
function Vector=LocalMtoV(S)
VariableList=zeros(NN.depth,1);
for i=1:NN.depth
VariableList(i)=numel(S{i});
end
TempVector=zeros(sum(VariableList),1);
NumOfVariable=0;
for i=1:NN.depth
for j=1:VariableList(i)
NumOfVariable=NumOfVariable+1;
TempVector(NumOfVariable)=S{i}(j);
end
end
Vector=TempVector;
end
end
end
%------------------------------------------------------------------------------%
function UpdatedNN=StochasticUpdateRule(dw,db,NN,option)
solver=option.Solver;
s0=option.s0;
switch solver
case "SGD"
for j=1:NN.depth
NN.weight{j}=NN.weight{j}-s0*dw{j};
NN.bias{j}=NN.bias{j}-s0*db{j};
end
case "SGDM"
m=0.9;
for j=1:NN.depth
NN.FirstMomentW{j}=(m)*NN.FirstMomentW{j}+(1-m)*dw{j};
NN.FirstMomentB{j}=(m)*NN.FirstMomentB{j}+(1-m)*db{j};
NN.weight{j}=NN.weight{j}-s0*NN.FirstMomentW{j};
NN.bias{j}=NN.bias{j}-s0*NN.FirstMomentB{j};
end
case "RMSprop"
for j=1:NN.depth
[DescentW,NN.FirstMomentW{j}]=RMSprop(dw{j},NN.FirstMomentW{j});
[DescentB,NN.FirstMomentB{j}]=RMSprop(db{j},NN.FirstMomentB{j});
NN.weight{j}=NN.weight{j}-s0*DescentW;
NN.bias{j}=NN.bias{j}-s0*DescentB;
end
case "ADAM"
for j=1:NN.depth
[DescentW,FW,SW]=ADAM(dw{j},NN.FirstMomentW{j},NN.SecondMomentW{j});
[DescentB,FB,SB]=ADAM(db{j},NN.FirstMomentB{j},NN.SecondMomentB{j});
NN.FirstMomentW{j}=FW; NN.SecondMomentW{j}=SW;
NN.FirstMomentB{j}=FB; NN.SecondMomentB{j}=SB;
NN.weight{j}=NN.weight{j}-s0*DescentW;
NN.bias{j}=NN.bias{j}-s0*DescentB;
end
case "AdamW"
r=option.Regulator;
for j=1:NN.depth
[DescentW,FW,SW]=ADAM(dw{j},NN.FirstMomentW{j},NN.SecondMomentW{j});
[DescentB,FB,SB]=ADAM(db{j},NN.FirstMomentB{j},NN.SecondMomentB{j});
NN.FirstMomentW{j}=FW; NN.SecondMomentW{j}=SW;
NN.FirstMomentB{j}=FB; NN.SecondMomentB{j}=SB;
NN.weight{j}=NN.weight{j}-s0*(DescentW+r*NN.weight{j});
NN.bias{j}=NN.bias{j}-s0*(DescentB+r*NN.bias{j});
end
end
UpdatedNN=NN;
function [d,Mnew,Vnew]=ADAM(dw,Mprev,Vprev)
iter=NN.StochasticCounter;
beta1=0.9; beta2=0.999;
Mnew=(beta1)*Mprev+(1-beta1)*dw;
Vnew=(beta2)*Vprev+(1-beta2)*(dw.^2);
Mt=Mnew/(1-beta1^iter); Vt=Vnew/(1-beta2^iter);
epsilon=(1e-8);
d=Mt./(sqrt(Vt)+epsilon);
end
function [d,Vnew]=RMSprop(dw,Vprev)
beta=0.9;
Vnew=(beta)*Vprev+(1-beta)*(dw.^2);
epsilon=(1e-8);
d=dw./(sqrt(Vnew)+epsilon);
end
end
end
%% Auxiliary Function
function Sample=Shuffle(data,label,BatchSize)
NumOfData=numel(data(1,:));
NumOfBatch=floor(NumOfData/BatchSize)+1;
LastBatch=rem(NumOfData,BatchSize);
Index=randperm(NumOfData);
for i=1:NumOfBatch
if i~=NumOfBatch
Rand=Index((i-1)*BatchSize+1:i*BatchSize);
Sample.Data{i}=data(:,Rand);
Sample.Label{i}=label(:,Rand);
Sample.Index{i}=Rand;
elseif i==NumOfBatch && LastBatch~=0
Rand=Index(NumOfData-LastBatch+1:end);
Sample.Data{i}=data(:,Rand);
Sample.Label{i}=label(:,Rand);
Sample.Index{i}=Rand;
end
end
end
function Accuracy=ComputeAccuracy(data,label,NN)
Probability=NN.Evaluate(data);
[~,PredictIndex]=max(Probability);
LabelIndex=NN.HotToIndex(label);
CorrectVector=LabelIndex==PredictIndex;
Accuracy=100*mean(CorrectVector);
end
function Class=ClassPredict(data,NN)
Probability=NN.Evaluate(data);
[~,Class]=max(Probability);
end