-
Notifications
You must be signed in to change notification settings - Fork 4
/
nystromCoRe_train.m
385 lines (298 loc) · 13.6 KB
/
nystromCoRe_train.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
function [ output ] = nystromCoRe_train( X , Y , varargin )
% nystromRegularization Nystrom computational regularization - Early Stopping cross validation
% Performs selection of the Nystrom regularization parameter
% (subsampling level m)
% in the context of Nystrom low-rank kernel approximation
%
% INPUT
% =====
%
% X : Input samples
%
% Y : Output signals
%
% config. \\ optional configuration structure. See config_set.m for
% \\ default values
%
% data.
% shuffle : 1/0 flag - Shuffle the training indexes
%
% crossValidation.
% storeTrainingError : 1/0 - Store training error
% flag
%
% validationPart : in (0,1) - Fraction of the
% training set used for validation
%
% recompute : 1/0 flag - Recompute solution using the
% whole training set after cross validation
%
% errorFunction : handle to the function used for
% error computation
%
% codingFunction : handle to the function used for
% coding (in classification tasks)
%
% stoppingRule : handle to the stopping rule function
%
% windowSize : Size of the window used by the
% stopping rule (default = 10)
%
% threshold : Threshold used by the
% stopping rule (default = 0)
%
% filter.
% lambdaGuesses : Vector of guesses for the Tikhonov
% regularization parameter
%
% kernel.
% kernelFunction : handle to the kernel function
%
% kernelParameter : vector of size r. r is the number of
% parameters required by kernelFunction.
%
% fixedM : Integer - fixed Nystrom subsampling level
% (no crossvalidation)
%
% minM : Integer - Minimum Nystrom subsampling level
%
% maxM : Integer - Maximium Nystrom subsampling level
%
% numStepsM : Integer - Number of Nystrom subsampling
% level guesses (iterative steps)
%
% OUTPUT
% ======
%
% output.
%
% best.
% validationError
% m
% alpha
% lambda
% lambdaIdx
%
% nysIdx : Vector - selected Nystrom approximation indexes
%
% time.
% kernelComputation
% crossValidationTrain
% crossValidationEval
% crossValidationTotal
%
% errorPath.
% training
% validation
% Check config struct
if nargin >2
config = varargin{1};
else
config = config_set(); % Construct default configuration structure
end
ntr = size(Y,1);
t = size(Y,2); % number of output signals
% Best parameters variables init
output.best = struct();
% output.best.alpha = zeros(config.kernel.m,t);
if isempty(config.kernel.fixedM) && isempty(config.kernel.numStepsM)
error('Specify either a fixed or a number of steps for the subsampling level m')
elseif (isempty(config.kernel.fixedM) && ~isempty(config.kernel.numStepsM)) || ...
(~isempty(config.kernel.fixedM) && config.filter.numLambdaGuesses > 1 )
% Set m range
if isempty(config.kernel.fixedM) && ~isempty(config.kernel.numStepsM)
config.kernel.mGuesses = round(linspace(config.kernel.minM, config.kernel.maxM , config.kernel.numStepsM));
output.best.m = config.kernel.maxM;
else
config.kernel.mGuesses = config.kernel.fixedM;
output.best.m = config.kernel.fixedM;
config.kernel.numStepsM = 1;
end
%%% Perform cross validation
output.best.validationError = Inf;
% Error buffers
output.errorPath.validation = zeros(config.filter.numLambdaGuesses,config.kernel.numStepsM) * NaN;
if config.crossValidation.storeTrainingError == 1
output.errorPath.training = zeros(config.filter.numLambdaGuesses,config.kernel.numStepsM) * NaN;
else
output.errorPath.training = [];
end
% Init time structures
output.time.kernelComputation = 0;
output.time.crossValidationTrain = 0;
output.time.crossValidationEval = 0;
% Subdivide training set in training1 and validation
ntr1 = floor( ntr * ( 1 - config.crossValidation.validationPart ));
if config.data.shuffle == 1
shuffledIdx = randperm(ntr);
trainIdx = shuffledIdx(1 : ntr1);
valIdx = shuffledIdx(ntr1 + 1 : end);
else
trainIdx = 1 : ntr1;
valIdx = ntr1 + 1 : ntr;
end
Xtr1 = X(trainIdx,:);
Ytr1 = Y(trainIdx,:);
Xval = X(valIdx,:);
Yval = Y(valIdx,:);
X(1 : ntr1 , :) = X(trainIdx , :);
X(ntr1 + 1 : end , :) = X(valIdx , :);
Y(1 : ntr1 , :) = Y(trainIdx , :);
Y(ntr1 + 1 : end , :) = Y(valIdx , :);
for i = 1:config.filter.numLambdaGuesses
l = config.filter.lambdaGuesses(i);
for j = 1:config.kernel.numStepsM
m = config.kernel.mGuesses(j);
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Incremental Update Rule %
%%%%%%%%%%%%%%%%%%%%%%%%%%%
if j == 1
%%% Initialization (j = 1)
% Preallocate matrices
A = zeros(ntr1 , config.kernel.maxM);
Aty = zeros(config.kernel.maxM , t);
R = cell(size(config.filter.lambdaGuesses));
[R{:,:}] = deal(zeros(config.kernel.maxM));
alpha = cell(size(config.filter.lambdaGuesses));
[alpha{:,:}] = deal(zeros(config.kernel.maxM,t));
% Sample columns and compute
samp = 1:m;
Xs = Xtr1(samp,:);
tic
A(:,samp) = config.kernel.kernelFunction(Xtr1 , Xs , config.kernel.kernelParameter);
B = A(samp,samp);
output.time.kernelComputation = output.time.kernelComputation + toc;
tic
Aty(samp,:) = A(:,samp)' * Ytr1;
R{i}(1:m,1:m) = ...
chol(full(A(:,1:m)' * A(:,1:m) ) + ...
ntr1 * l * B);
% alpha
alpha{i} = R{i}(1:m,1:m) \ ...
( R{i}(1:m,1:m)' \ ...
( Aty(1:m,:) ) );
output.time.crossValidationTrain = output.time.crossValidationTrain + toc;
else
%%% Generic j-th incremental update step (j > 1)
mPrev = config.kernel.mGuesses(j - 1);
% Sample new columns of K
samp = (mPrev + 1) : m;
XsNew = Xtr1(samp,:);
Xs = [Xs ; XsNew];
% Computer a, b, beta
tic
a = config.kernel.kernelFunction(Xtr1 , XsNew , config.kernel.kernelParameter);
output.time.kernelComputation = output.time.kernelComputation + toc;
b = a( 1:mPrev , : );
beta = a( samp , : );
tic
% Compute c, gamma
c = A(:,1:mPrev)' * a + ntr1 * l * b;
gamma = a' * a + ntr1 * l * beta;
% Update A, Aty
A( : , (mPrev+1) : m ) = a ;
Aty( (mPrev+1) : m , : ) = a' * Ytr1 ;
% Compute u, v
u = [ c / ( 1 + sqrt( 1 + gamma) ) ; ...
sqrt( 1 + gamma) ];
v = [ c / ( 1 + sqrt( 1 + gamma) ) ; ...
-1 ];
% Update R
R{i}(1:m,1:m) = ...
cholupdatek( R{i}(1:m,1:m) , u , '+');
R{i}(1:m,1:m) = ...
cholupdatek(R{i}(1:m,1:m) , v , '-');
% Recompute alpha
alpha{i} = R{i}(1:m,1:m) \ ...
( R{i}(1:m,1:m)' \ ...
( Aty(1:m,:) ) );
output.time.crossValidationTrain = output.time.crossValidationTrain + toc;
end
% Evaluate validation error and select model
% Initialize TrainVal kernel
tic
Kval = config.kernel.kernelFunction(Xval, Xs , config.kernel.kernelParameter);
% Compute validation predictions matrix
YvalPred = Kval * alpha{i};
% Compute validation performance
if ~isempty(config.crossValidation.codingFunction)
YvalPred = config.crossValidation.codingFunction(YvalPred);
end
output.errorPath.validation(i,j) = config.crossValidation.errorFunction(Yval , YvalPred);
output.time.crossValidationEval = output.time.crossValidationEval + toc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Store performance matrices %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if config.crossValidation.storeTrainingError == 1
% Compute training predictions matrix
YtrainPred = A * alpha{i};
% Compute training performance
if ~isempty(config.crossValidation.codingFunction)
YtrainPred = config.crossValidation.codingFunction(YtrainPred);
end
output.errorPath.training(i,j) = config.crossValidation.errorFunction(Ytr1 , YtrainPred);
end
%%%%%%%%%%%%%%%%%%%%
% Store best model %
%%%%%%%%%%%%%%%%%%%%
if output.errorPath.validation(i,j) < output.best.validationError
% Update best filter parameter
output.best.lambda = l;
output.best.lambdaIdx = i;
% Update best sampling level m
output.best.m = m;
% Update internal model samples matrix
output.best.sampledPoints = Xs;
%Update best validation performance measurement
output.best.validationError = output.errorPath.validation(i,j);
% Update coefficients vector
output.best.alpha = alpha{i};
end
end
end
output.time.crossValidationTotal = output.time.crossValidationTrain + output.time.crossValidationEval ;
if config.crossValidation.recompute == 1
%%% Retrain on whole dataset
tic
% Sample columns and compute
samp = 1:output.best.m;
Xs = Xtr1(samp,:);
A = config.kernel.kernelFunction(X , Xs , config.kernel.kernelParameter);
B = A(samp,samp);
tic
Aty = A(:,samp)' * Y;
R = chol(full(A(:,1:output.best.m)' * A(:,1:output.best.m) ) + ...
ntr * output.best.lambda * B);
% alpha
output.best.recomputedAlpha = R \ ( R' \ ( Aty ) );
output.time.retraining = toc;
end
elseif ~isempty(config.kernel.fixedM) && numel(config.filter.lambdaGuesses) == 1
%%% Just train on whole dataset
tic
% Sample columns and compute
if config.data.shuffle == 1
samp = randperm(ntr);
X = X(samp , :);
Y = Y(samp , :);
Xs = X(1:config.kernel.fixedM,:);
else
samp = 1:config.kernel.fixedM;
Xs = X(samp,:);
end
A(:,1:config.kernel.fixedM) = config.kernel.kernelFunction(X , Xs , config.kernel.kernelParameter);
B = A(1:config.kernel.fixedM,1:config.kernel.fixedM);
Aty(1:config.kernel.fixedM,:) = A(:,1:config.kernel.fixedM)' * Y;
R(1:config.kernel.fixedM,1:config.kernel.fixedM) = ...
chol(full(A(:,1:config.kernel.fixedM)' * A(:,1:config.kernel.fixedM) ) + ...
ntr * config.filter.lambdaGuesses * B);
% alpha
output.best.alpha = R(1:config.kernel.fixedM,1:config.kernel.fixedM) \ ...
( R(1:config.kernel.fixedM,1:config.kernel.fixedM)' \ ...
( Aty(1:config.kernel.fixedM,:) ) );
output.time.fullTraining = toc;
output.best.sampledPoints = Xs;
output.best.lambda = config.filter.lambdaGuesses;
end
output.config = config;
end