-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathabstractMeganetElement.m
443 lines (407 loc) · 15.7 KB
/
abstractMeganetElement.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
classdef abstractMeganetElement < handle
% need to find better name for this, but here go
% some derivative functions that simplify building Jacobians etc.
%
% those methods are used for all 'elements' of our networks
% (transformations, layers, networks, blocks,...)
%
% Abstractly, a MeganetElement does the following
%
% Y_k+1 = forwardProp(theta,Y_k)
%
%
% where 'forwardProp' can be everything from a single affine transformation to a
% ResNet block. All these operations have a similar structure, e.g.,
% provide derivatives w.r.t. theta and Y_k, ...
%
%
% Example: Consider a Neural network consisting of two layers.
% T1 = dense([12,8]);
% T2 = dense([24,12])
% net = NN({T1, T2});
%
% Calling forwardProp(net,theta,Y) results in a nested evaluation
%
% forwardProp(net,theta,Y) = forwardProp(T2, theta2, forwardProp(T1, theta1, Y));
%
% This example shows that each element of the network needs the following
% functions
%
% split - partition the input parameters into parameters of elements
% describing this object (in our case theta -> theta1, theta2
% forwardProp - evaluate the action (e.g., forward propagation, filtering, ..)
% in many cases this involves calling 'forwardProp' for other objects
% (e.g., for different layers, kernels,...)
% Jthetamv - compute the action of the Jacobian w.r.t theta on a vector
% JthetaTmv - compute the action of the transpose(Jacobian) w.r.t theta on a vector
% JYmv - compute the action of the Jacobian w.r.t Y on a vector
% JYTmv - compute the action of the transpose(Jacobian) w.r.t Y on a vector
%
% In addition, elements of this class also need to provide the folowing
% methods
%
% nTheta - return the number of parameters, numel(theta) for this
% element (may have to ask lower-level elements for this)
% sizeFeatIn - input feature dimensions
% sizeFeatOut - output feature dimensions
% numelFeatIn - input feature number of total elements
% numelFeatOut - ouput feature number of total elements
% initTheta - initialize parameters
methods
function this = setTimeY(this,varargin)
% set time steps for states
end
function n = nTheta(~)
% function n = nTheta(this)
%
% return number of parameters, i.e., numel(theta)
n = [];
error('children of abstractMeganetElement must provide method nTheta');
end
function n = sizeFeatIn(~)
% function n = sizeFeatIn(this)
%
% return dimensions of input features, i.e., for 2D Y, size(Y,1)
n = [];
error('children of abstractMeganetElement must provide method sizeFeatIn');
end
function n = sizeFeatOut(~)
% function n = sizeFeatOut(this)
%
% return dimensions of the output features, i.e., for 2D Y, size(Y,2)
n = [];
error('children of abstractMeganetElement must provide method sizeFeatOut');
end
function n = getHessian(~)
% function n = getHessian(~)
%
% return Hessian matrix
n = [];
error('Matrix-based Hessians are not implemented for objects of this type.');
end
function n = JthJthetaTmv(~)
% function n = JthJthetaTmv(~)
%
% return matrix vector products with Jacobian of J'*dYF
n = [];
error('Matrix-free implementations of JthJthetaTmv are not supported for objects of this type.');
end
function net = loadNet(this)
% by default, AbstractMeganetElements can be loaded from disk
net = this;
end
function n = numelFeatOut(this)
% function n = numelFeatOut(this)
%
% return number of output features
n = prod(sizeFeatOut(this));
% error('children of abstractMeganetElement must provide method numelFeatOut');
end
function n = numelFeatIn(this)
% function n = numelFeatIn(this)
%
% return number of output features
n = prod(sizeFeatIn(this));
% error('children of abstractMeganetElement must provide method numelFeatIn');
end
function varargout = split(~,~)
% function varargout = split(this,theta)
%
% split theta into different parts used by sub-elements.
varargout = [];
error('children of abstractMeganetElement must provide method split');
end
function theta = initTheta(~,~)
% function theta = initTheta(this)
%
% initialize theta
theta = [];
error('children of abstractMeganetElement must provide method initTheta');
end
% ---------derivatives for Y --------
function dY = JYTmv(this,W,theta,Y,tmp)
% dY = abstractMeganetElement.JYTmv(this,W,theta,Y,tmp)
%
% computes dY = transpose(J_Y(theta,Y))*W
%
% Input:
%
% W - vector or matrix
% theta - current theta
% Y - current Y
% tmp - intermediates used in derivative computations
% (e.g., hidden features, activations, derivatives,
% ... )
%
% Output:
%
% dY - directional derivative, numel(dY)==numel(Y)
[~,dY] = JTmv(this,W,theta,Y,tmp);
end
function dY = JYmv(this,dY,theta,Y,tmp)
% dZ = abstractMeganetElement.JYTmv(this,W,theta,Y,tmp)
%
% computes dZ = J_Y(theta,Y)*dY
%
% Input:
%
% dY - perturbation in Y
% theta - current theta
% Y - current Y
% tmp - intermediates used in derivative computations
% (e.g., hidden features, activations, derivatives,
% ... )
%
% Output:
%
% dZ - directional derivative, numel(dZ)==numel(Z)
dY = Jmv(this,[],dY,theta,Y,tmp);
end
function [this,theta] = prolongateWeights(this,theta)
end
function [Z,J] = linearizeY(this,theta,Y)
% function [K,dK] = linearizeY(this,theta,Y)
%
% linearization with respect to Y, i.e.,
%
% Z(theta,Y+dY) \approx Z(theta,Y) + J*dY
%
% Input:
%
% theta - current theta
% Y - current Y
% tmp - intermediates used in derivative computations
% (e.g., hidden features, activations, derivatives,
% ... )
%
% Output:
%
% Z - current output features
% J - Jacobian, LinearOperator
[Z,tmp] = forwardProp(this,theta,Y);
J = getJYOp(this,theta,Y,tmp);
end
function J = getJYOp(this,theta,Y,tmp)
% J = getJYOp(this,theta,Y,tmp)
%
% constructs Jacobian w.r.t. Y around current (theta,Y)
%
% Input:
%
% W - vector or matrix
% theta - current theta
% Y - current Y
% tmp - intermediates used in derivative computations
% (e.g., hidden features, activations, derivatives,
% ... )
%
% Output:
%
% dY - directional derivative, numel(dY)==numel(Y)
if nargin<4; tmp=[]; end
m = [sizeFeatOut(this) size(Y,ndims(Y))];
n = size(Y);
Amv = @(x) JYmv(this,x,theta,Y,tmp);
ATmv = @(x) JYTmv(this,x,theta,Y,tmp);
J = LinearOperator(m,n,Amv,ATmv);
end
% -------- derivatives for theta ---------
function dY = Jthetamv(this,dtheta,theta,Y,tmp)
% dZ = abstractMeganetElement.Jthetamv(this,W,theta,Y,tmp)
%
% computes dZ = J_theta(theta,Y)*dtheta
%
% Input:
%
% dtheta- perturbation in theta
% theta - current theta
% Y - current Y
% tmp - intermediates used in derivative computations
% (e.g., hidden features, activations, derivatives,
% ... )
%
% Output:
%
% dZ - directional derivative, numel(dZ)==numel(Z)
dY = Jmv(this,dtheta,[],theta,Y,tmp);
end
function dY = JthetaJYmv(this,dtheta,W,theta,Y,tmp)
dY = JJYmv(this,dtheta,[],W,theta,Y,tmp);
end
function dtheta = JthetaTmv(this,W,theta,Y,tmp,varargin)
% dY = abstractMeganetElement.JthetaTmv(this,W,theta,Y,tmp)
%
% computes dtheta = transpose(J_theta(theta,Y))*W
%
% Input:
%
% W - vector or matrix
% theta - current theta
% Y - current Y
% tmp - intermediates used in derivative computations
% (e.g., hidden features, activations, derivatives,
% ... )
%
% Output:
%
% dtheta - directional derivative, numel(dtheta)==numel(theta)
dtheta = JTmv(this,W,theta,Y,tmp,[],varargin{:});
end
function dtheta = JthetaTJYmv(this,Z,W,theta,Y,tmp)
dtheta = JTJYmv(this,Z,W,theta,Y,tmp);
end
function J = getJthetaOp(this,theta,Y,tmp)
% J = abstractMeganetElement.getJthetaOp(this,theta,Y,tmp)
%
% constructs Jacobian w.r.t. theta as LinearOperator
%
% Input:
%
% theta - current theta
% Y - current Y
% tmp - intermediates used in derivative computations
% (e.g., hidden features, activations, derivatives,
% ... )
%
% Output:
%
% J - Jacobian, LinearOperator
if nargin<4; tmp=[]; end
m = sizeFeatOut(this);
n = nTheta(this);
Amv = @(x) Jthetamv(this,x,theta,Y,tmp);
ATmv = @(x) JthetaTmv(this,x,theta,Y,tmp);
J = LinearOperator(m,n,Amv,ATmv);
end
function J = getJthetaJYOp(this,W,theta,Y,tmp)
if nargin<5; tmp=[]; end
m = sizeFeatOut(this);
n = nTheta(this);
Amv = @(x) JthetaJYmv(this,x,W,theta,Y,tmp);
ATmv = @(x) JthetaTJYmv(this,x,W,theta,Y,tmp);
J = LinearOperator(m,n,Amv,ATmv);
end
function [Z,J] = linearizeTheta(this,theta,Y)
% function [Z,J] = linearizeY(this,theta,Y)
%
% linearization with respect to theta, i.e.,
%
% Z(theta+dth,Y) \approx Z(theta,Y) + J*dth
%
% Input:
%
% theta - current theta
% Y - current Y
%
% Output:
%
% Z - output features
% J - Jacobian, LinearOperator
[Z,tmp] = forwardProp(this,theta,Y);
J = getJthetaOp(this,theta,Y,tmp);
end
% -------- combined derivatives ----------
function dZ = Jmv(this,dtheta,dY,theta,Y,tmp)
% dZ = abstractMeganetElement.Jmv(this,dtheta,dY,theta,Y,tmp)
%
% computes dZ = J_theta(theta,Y)*dtheta + J_Y(theta,Y)*dY
%
% Input:
%
% dtheta- perturbation in theta
% dY - perturbation of input features Y
% theta - current theta
% Y - current Y
% tmp - intermediates used in derivative computations
% (e.g., hidden features, activations, derivatives,
% ... )
%
% Output:
%
% dZ - directional derivative, numel(dZ)==numel(Z)
if nargin<4; tmp=[]; end
if isempty(dtheta) || norm(dtheta(:))==0
dZ = 0;
else
dZ = Jthetamv(this,dtheta,theta,Y,tmp);
end
if not(isempty(dY)) && norm(dY(:))>0
dZt = JYmv(this,dY,theta,Y,tmp);
dZ = dZ + dZt;
end
end
function [dtheta,dY] = JTmv(this,W,theta,Y,tmp,doDerivative,varargin)
% dZ = abstractMeganetElement.JTmv(this,W,theta,Y,tmp,doDerivative)
%
% computes [dtheta;dY] = [J_theta(theta,Y)'; J_Y(theta)']*W
%
% Input:
%
% Z - perturbation of output
% theta - current theta
% Y - current Y
% tmp - intermediates used in derivative computations
% (e.g., hidden features, activations, derivatives,
% ... )
% doDerivative - vector with two elements for theta and Y
% derivative. Important only when nargout==1. default=[1,0];
%
% Output: There are three different modes for the output
%
% if nargout==2
% dtheta - directional derivative, numel(dtheta)==numel(theta)
% dY - directional derivative, numel(dY)==numel(dY)
% elseif nargout==1 && all(doDerivative==1)
% dtheta = [dtheta(:); dY(:)]
% else
% dtheta = dtheta
% end
%
% There are different modes for the output. If nargout==2
if not(exist('tmp','var')); tmp=[]; end
if not(exist('doDerivative','var')) || isempty(doDerivative)
doDerivative =[1;0];
end
dtheta = JthetaTmv(this,W,theta,Y,tmp,varargin{:});
if nargout==2 || doDerivative(2)==1
dY = JYTmv(this,W,theta,Y,tmp);
end
if nargout==1 && all(doDerivative==1)
dtheta = [dtheta(:); dY(:)];
end
end
function [theta] = prolongateConvStencils(this,theta,getRP)
% prolongate convolution stencils. By default do nothing.
end
function [theta] = restrictConvStencils(this,theta,getRP)
% restrict convolution stencils. By default do nothing.
end
function J = getJOp(this,theta,Y,tmp)
% J = abstractMeganetElement.getJOp(this,Z,theta,Y,tmp)
%
% constructs Jacobian J(theta,Y) such that
%
% Z(theta+dth,Y+dY) \approx Z(theta,Y) + J*[dth; dY]
%
% Input:
%
% theta - current theta
% Y - current Y
% tmp - intermediates used in derivative computations
% (e.g., hidden features, activations, derivatives,
% ... )
%
% Output:
%
% J - Jacobian, LinearOperator
if nargin<4; tmp=[]; end
% nex = sizeLastDim(Y);
m = sizeFeatOut(this);
nth = nTheta(this);
nY = numel(Y);
Amv = @(x) Jmv(this,x(1:nth),reshape(x(nth+1:end),size(Y)),theta,Y,tmp);
ATmv = @(x) JTmv(this,x,theta,Y,tmp,[1;1]);
J = LinearOperator(m,nth+nY,Amv,ATmv);
end
end
end