-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdnnObjFctn2.m
298 lines (246 loc) · 10 KB
/
dnnObjFctn2.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
classdef dnnObjFctn2 < objFctn
% classdef dnnObjFctn2 < objFctn
%
% objective function for deep neural networks
%
% J(theta) = loss(Y(theta), C) + Rtheta(theta);
%
% This function is similar to dnnObjFctn but does not handle the
% weights of the last layer of the network differently than the others.
properties
net
pReg
pLoss
Y
C
matrixFree % flag for matrix-free computation, default = 1
gnHessian % flag for Gauss-Newton approximation of Hessian
useGPU
precision
end
methods
function this = dnnObjFctn2(net,pReg,pLoss,Y,C,varargin)
if nargout==0 && nargin==0
this.runMinimalExample;
return;
end
useGPU = [];
precision = [];
matrixFree = 1;
gnHessian = 1;
for k=1:2:length(varargin) % overwrites default parameter
eval([varargin{k},'=varargin{',int2str(k+1),'};']);
end
this.net = net;
this.pReg = pReg;
this.pLoss = pLoss;
if not(isempty(useGPU))
this.useGPU = useGPU;
end
if not(isempty(precision))
this.precision=precision;
end
if not(numelFeatOut(net) == size(C,1))
error('numelFeatOut(net)=%d != %d=size(C,1)',numelFeatOut(net),size(C,1));
end
[Y,C] = gpuVar(this.useGPU,this.precision,Y,C);
this.Y = Y;
this.C = C;
this.matrixFree = matrixFree;
this.gnHessian = gnHessian;
end
function [theta] = split(this,theta)
end
function [Jc,para,dJ,H,PC] = eval(this,theta,idx)
if not(exist('idx','var')) || isempty(idx)
Y = this.Y;
C = this.C;
else
colons = repmat( {':'} , 1 , ndims(this.Y)-1 );
Y = this.Y( colons{:} ,idx);
C = this.C(:,idx);
end
compGrad = nargout>2;
compHess = nargout>3;
nex = sizeLastDim(Y); % number of examples to compute loss over
dJ = []; H = []; PC = [];
% evaluate loss
% [YN,Yall,dA] = fwd(this.net,Kb,this.Y);
if compGrad || compHess
[YN,tmp] = forwardProp(this.net,theta,Y); % forward propagation
else
YN = forwardProp(this.net,theta,Y);
end
szYN = size(YN);
YN = reshape(YN,[],nex); % loss expects 2D input
% evaluate loss function
[F,para,dF,d2F] = eval(this.pLoss,YN,C,'reduceDim',0);
Jc = sum(F);
if compGrad && (this.matrixFree || not(compHess))
dJ = JthetaTmv(this.net,dF,theta,Y,tmp);
elseif compGrad
Jac = getJacobians(this.net,theta,Y,tmp);
dJ = Jac'*dF(:);
end
if compHess
if not(this.gnHessian)
error('nyi');
end
if this.matrixFree
Hmv = @(x) JthetaTmv(this.net,reshape(d2F*Jthetamv(this.net,x,theta,Y,tmp),szYN),theta,Y,tmp); % JTmv(this.net, reshape(d2YF* Jmv(this.net,x,[],Kb,Yall,dA),size(YN)), Kb,Yall,dA);
H = LinearOperator(numel(theta),numel(theta),Hmv,Hmv);
else
H = Jac' * d2F * Jac;
end
end
para = struct('F',Jc,'accuracy',100 * (1 - para(3) / sizeLastDim(Y)));
% evaluate regularizer for DNN weights
if not(isempty(this.pReg))
[R,hisR,dR,d2R] = regularizer(this.pReg,theta);
Jc = Jc + R;
if compGrad
dJ = dJ + dR;
end
if compHess
H = H + d2R;
end
para.R = R;
para.hisR = hisR;
end
if nargout>4
PC = opEye(numel(theta));
end
end
function [str,frmt] = hisNames(this)
[str,frmt] = hisNames(this.pLoss);
str = {'loss','accuracy'};
frmt = {'%-12.2e','%-12.2f'};
if not(isempty(this.pReg))
[s,f] = hisNames(this.pReg);
s{1} = [s{1} '(theta)'];
str = [str, s{:}];
frmt = [frmt, f{:}];
end
end
function his = hisVals(this,para)
his = [para.F,para.accuracy];
if not(isempty(this.pReg))
his = [his, hisVals(this.pReg,para.hisR)];
end
end
function str = objName(this)
str = 'dnnObjFun';
end
% ------- functions for handling GPU computing and precision ----
function this = set.useGPU(this,value)
if isempty(value)
return
elseif(value~=0) && (value~=1)
error('useGPU must be 0 or 1.')
else
if not(isempty(this.net)); this.net.useGPU = value; end
if not(isempty(this.pReg)); this.pReg.useGPU = value; end
[this.Y,this.C] = gpuVar(value,this.precision,...
this.Y,this.C);
end
end
function this = set.precision(this,value)
if isempty(value)
return
elseif not(strcmp(value,'single') || strcmp(value,'double'))
error('precision must be single or double.')
else
if not(isempty(this.net)); this.net.precision = value; end
if not(isempty(this.pReg)); this.pReg.precision = value; end
[this.Y,this.C] = gpuVar(this.useGPU,value,...
this.Y,this.C);
end
end
function useGPU = get.useGPU(this)
useGPU = -ones(2,1);
if not(isempty(this.net)) && not(isempty(this.net.useGPU))
useGPU(1) = this.net.useGPU;
end
if not(isempty(this.pReg)) && not(isempty(this.pReg.useGPU))
useGPU(2) = this.pReg.useGPU;
end
useGPU = useGPU(useGPU>=0);
if all(useGPU==1)
useGPU = 1;
elseif all(useGPU==0)
useGPU = 0;
else
error('useGPU flag must agree');
end
end
function precision = get.precision(this)
isSingle = -ones(2,1);
isSingle(1) = strcmp(this.net.precision,'single');
if not(isempty(this.pReg)) && not(isempty(this.pReg.precision))
isSingle(2) = strcmp(this.pReg.precision,'single');
end
isSingle = isSingle(isSingle>=0);
if all(isSingle==1)
precision = 'single';
elseif all(isSingle==0)
precision = 'double';
else
error('precision flag must agree');
end
end
function runMinimalExample(~)
nex = 100; nf =2;
blocks = cell(0,1);
blocks{end+1} = NN({singleLayer(dense([2*nf nf]))});
% blocks{end+1} = ResNN(doubleLayer(dense([2*nf 2*nf]),dense([2*nf 2*nf])),2,.1);
blocks{end+1} = NN({singleLayer(dense([2,2*nf]),'activation',@identityActivation,'Bin',ones(2,1))});
net = Meganet(blocks);
nth = nTheta(net);
theta = randn(nth,1);
% training data
Y = randn(nf,nex);
C = zeros(nf,nex);
C(1,Y(2,:)>Y(1,:).^2) = 1;
C(2,Y(2,:)<=Y(1,:).^2) = 1;
% validation data
Yv = randn(nf,nex);
Cv = zeros(nf,nex);
Cv(1,Yv(2,:)>Yv(1,:).^2) = 1;
Cv(2,Yv(2,:)<=Yv(1,:).^2) = 1;
pLoss = regressionLoss();
pReg = tikhonovReg(eye(numel(theta)));
fctn = dnnObjFctn2(net,pReg,pLoss,Y,C,'matrixFree',0);
fval = dnnObjFctn2(net,[],pLoss,Yv,Cv);
% [Jc,para,dJ,H,PC] = fctn([Kb(:);W(:)]);
% checkDerivative(fctn,[Kb(:);W(:)])
opt1 = newton('out',1,'maxIter',20);
opt2 = sd('out',1,'maxIter',20);
opt3 = nlcg('out',1,'maxIter',20);
[KbWopt1,His1] = solve(opt1,fctn,theta(:),fval);
[KbWopt2,His2] = solve(opt2,fctn,theta(:),fval);
[KbWopt3,His3] = solve(opt3,fctn,theta(:),fval);
figure(1); clf;
subplot(1,3,1);
semilogy(His1.his(:,2)); hold on;
semilogy(His2.his(:,2));
semilogy(His3.his(:,2)); hold off;
legend('newton','sd','nlcg');
title('objective');
subplot(1,3,2);
semilogy(His1.his(:,4)); hold on;
semilogy(His2.his(:,4));
semilogy(His3.his(:,4)); hold off;
legend('newton','sd','nlcg');
title('opt.cond');
subplot(1,3,3);
plot(His1.his(:,10)); hold on;
plot(His1.his(:,end),'--'); hold on;
plot(His2.his(:,8));
plot(His2.his(:,end),'--');
plot(His3.his(:,8));
plot(His3.his(:,end),'--'); hold off;
legend('newton-train','newton-val','sd-train','sd-val','nlcg-train','nlcg-val');
title('loss');
end
end
end