-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnytro_train.m
279 lines (229 loc) · 9.37 KB
/
nytro_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
function [ output ] = nytro_train( X , Y , varargin )
% NYTRO NYstrom iTerative RegularizatiOn - Early Stopping cross validation
% Performs selection of the Early Stopping regularization parameter
% 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.
% fixedIterations : Integer - fixed number of iterations
%
% maxIterations : Integer - maximum number of iterations
% (for cross validation)
%
% gamma : Scalar - override gradient descent step
%
% kernel.
% kernelFunction : handle to the kernel function
%
% kernelParameters : vector of size r. r is the number of
% parameters required by kernelFunction.
%
% m : Integer - Nystrom subsampling level
%
% OUTPUT
% ======
%
% output.
%
% best.
% validationError
% iteration
% alpha
%
% 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.filter.fixedIterations) && isempty(config.filter.maxIterations)
error('Specify either a fixed or a maximum number of iterations')
elseif isempty(config.filter.fixedIterations) && ~isempty(config.filter.maxIterations)
%%% Perform cross validation
output.best.validationError = Inf;
output.best.iteration = Inf;
% Error buffers
output.errorPath.validation = zeros(1,config.filter.maxIterations) * NaN;
if config.crossValidation.storeTrainingError == 1
output.errorPath.training = zeros(1,config.filter.maxIterations) * NaN;
else
output.errorPath.training = [];
end
% Init time structures
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,:);
% Initialize Train kernel
% Subsample training examples for Nystrom approximation
nysIdx = randperm(ntr1 , config.kernel.m);
output.nysIdx = trainIdx(nysIdx);
% Compute kernel
tic
Knm = config.kernel.kernelFunction(X, Xtr1(nysIdx,:), config.kernel.kernelParameters);
Kmm = Knm(trainIdx(nysIdx),:);
R = chol( ( Kmm + Kmm') / 2 + 1e-10 * eye(config.kernel.m)); % Compute upper Cholesky factor of Kmm
output.time.kernelComputation = toc;
alpha = zeros(config.kernel.m,t);
beta = zeros(config.kernel.m,t);
if isempty(config.filter.gamma)
gamma = 1/(norm(Knm/R)^2);
else
gamma = config.filter.gamma;
end
indZ = zeros(ntr, t);
indZ(trainIdx,:) = ones(ntr1,t);
for iter = 1:config.filter.maxIterations
% Update filter
tic
tmp0 = Knm * alpha - Y;
tmp0 = tmp0 .* indZ;
beta = beta - gamma * ( R' \ ( Knm' * tmp0 ) );
output.time.crossValidationTrain = output.time.crossValidationTrain + toc;
% Evaluate validation error
tic
alpha = R\beta; % Compute alpha
YtrainValPred = Knm * alpha;
if ~isempty(config.crossValidation.codingFunction)
YvalPred = config.crossValidation.codingFunction(YtrainValPred(valIdx,:));
else
YvalPred = YtrainValPred(valIdx,:);
end
output.errorPath.validation(iter) = config.crossValidation.errorFunction(Y(valIdx,:) , YvalPred);
output.time.crossValidationEval = output.time.crossValidationEval + toc;
if output.errorPath.validation(iter) < output.best.validationError
output.best.validationError = output.errorPath.validation(iter);
output.best.iteration = iter;
output.best.alpha = alpha;
end
if config.crossValidation.storeTrainingError == 1
% Evaluate training error
if ~isempty(config.crossValidation.codingFunction)
YtrainPred = config.crossValidation.codingFunction(YtrainValPred(trainIdx,:));
else
YtrainPred = YtrainValPred(trainIdx,:);
end
output.errorPath.training(iter) = config.crossValidation.errorFunction(Y(trainIdx,:) , YtrainPred);
end
% Apply Stopping Rule
if ~isempty(config.crossValidation.stoppingRule)
stop = config.crossValidation.stoppingRule(...
output.errorPath.validation(1:iter) , ...
config.crossValidation.windowSize , ...
config.crossValidation.threshold);
if stop == 1
break
end
end
output.time.crossValidationTotal = output.time.crossValidationEval + output.time.crossValidationTrain;
end
if config.crossValidation.recompute == 1
%%% Retrain on whole dataset
tic
beta = zeros(config.kernel.m,t);
if isempty(config.filter.gamma)
gamma = 1/(norm(Knm/R))^2;
else
gamma = config.filter.gamma;
end
% Compute solution
for iter = 1:output.best.iteration
% Update filter
beta = beta - gamma * (R' \ ( Knm' * ( Knm * (R \ beta) - Y ) ) );
end
output.best.alpha = R\beta; % Get alpha from beta
output.time.fullTraining = toc;
end
elseif ~isempty(config.filter.fixedIterations)
%%% Just train
% Initialize Train kernel
% Subsample training examples for Nystrom approximation
nysIdx = randperm(ntr , config.kernel.m);
output.nysIdx = nysIdx;
% Compute kernels
tic
Knm = config.kernel.kernelFunction(X, X(nysIdx,:), config.kernel.kernelParameters);
Kmm = Knm(nysIdx,:);
R = chol( ( Kmm + Kmm') / 2 + 1e-10 * eye(config.kernel.m)); % Compute upper Cholesky factor of Kmm
output.time.kernelComputation = toc;
tic
beta = zeros(config.kernel.m,t);
if isempty(config.filter.gamma)
gamma = 1/(norm(Knm/R))^2;
else
gamma = config.filter.gamma;
end
% Compute solution with a fixed number of steps
for iter = 1:config.filter.fixedIterations
% Update filter
beta = beta - gamma * (R' \ ( Knm' * ( Knm * (R \ beta) - Y ) ) );
end
output.best.alpha = R\beta; % Get alpha from beta
output.time.fullTraining = toc;
end
output.config = config;
end