-
Notifications
You must be signed in to change notification settings - Fork 2
/
ltmlp_train2.m
217 lines (193 loc) · 6.59 KB
/
ltmlp_train2.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
function [net, res] = ltmlp_train2(net, input, output, test_input, test_output)
% Training algorithm with burn-in (increasing momentum, exponentially
% decaying learning rate). This is specifically for classification task.
% Shows error in number of items classified incorrecly.
% Runtime (opt.runtime) in epochs.
opt = net.options;
momentum = opt.mominit;
nlayers = numel(net.layers);
layers = net.layers;
nonlintypes = net.layertypes;
gradW = cell(nlayers,nlayers-1);
gradbias = cell(nlayers,1);
stepsizeW = zeros(nlayers,nlayers-1);
gammareco = cell(1,nlayers);
stepsize = opt.stepsize;
% Stepsize for shortcut connections is smaller
for l=2:nlayers,
for ll=1:l-1,
stepsizeW(l,ll) = 1/2^(l-ll-1);
end
end
% Initialize momentum to zero
if momentum > 0
directionW = cell(nlayers,nlayers-1);
directionbias = cell(nlayers,1);
for l=1:nlayers,
directionbias{l} = zeros(layers(l), 1);
for ll=1:l-1,
directionW{l,ll} = zeros(size(net.W{l,ll}));
end
end
end
if nargout > 1 || opt.verbose
save_data = 1;
res.training_errors = ones(1,opt.runtime)*Inf;
res.test_errors = ones(1,opt.runtime)*Inf;
res.iters = ones(1,opt.runtime)*Inf;
res.gradW = ones(1,opt.runtime)*Inf;
res.cputimes = ones(1,opt.runtime)*Inf;
num_evals_complete = 0;
else
save_data = 0;
end
[datadim, dlen] = size(input);
blen = opt.minibatchsize;
i0 = 0;
iter = 1;
cpustart = cputime;
epoch = 0;
while epoch < opt.runtime
% Choose mini batch
inds = (i0+1):min(dlen,i0+blen);
i0 = i0+blen;
if i0 >= dlen, i0 = 0;end
% Add noise to activations for regularization
if opt.input_noise > 0
current_input = input(:,inds) + opt.input_noise * randn(datadim, numel(inds));
else
current_input = input(:,inds);
end
% Update transformations
if opt.num_transf > 0
if num_evals_complete == 0 || res.test_errors(num_evals_complete) > 100
net = ltmlp_transform(net, current_input);
elseif mod(blen*iter,dlen) == 0
net = ltmlp_transform(net, input + opt.input_noise * randn(datadim, size(input,2)));
end
end
% Feedforward
[current_output, net] = ltmlp_ff(net, current_input);
% Compute reconstruction error
reco_err = current_output - output(:,inds);
% Backpropagation part
gammareco{nlayers} = nonlin(net.X{nlayers}, nonlintypes{nlayers-1}, net.nonlintrans{nlayers}, 1) .* reco_err;
for l = nlayers-1:-1:2,
gammareco{l} = 0;
for ll=(l+1):nlayers,
if ~isempty(net.W{ll,l})
gammareco{l} = gammareco{l} + (net.W{ll,l}' * gammareco{ll}) .* ...
nonlin(net.X{l}, nonlintypes{l-1}, net.nonlintrans{l}, 1);
end
end
end
% Gradient computations
for l = 2:nlayers
% Regular gradient
gradbias{l} = sum(gammareco{l}, 2) / numel(inds);
for ll=1:l-1,
if ~isempty(net.W{l,ll})
gradW{l,ll} = gammareco{l} * net.Y{ll}' / numel(inds);
end
end
if opt.weight_decay>0
% Weight decay
gradbias{l} = gradbias{l} + opt.weight_decay * net.bias{l};
for ll=1:l-1,
if ~isempty(net.W{l,ll})
gradW{l,ll} = gradW{l,ll} + opt.weight_decay * net.W{l,ll};
end
end
end
end
% Update momentum
if momentum > 0
for l=2:nlayers
directionbias{l} = gradbias{l} + opt.momentum * directionbias{l};
% update rule from dropout paper:
%directionbias{l} = momentum * directionbias{l} - ...
% (1 - momentum) * stepsize * stepsizebias(l) * gradbias{l};
for ll=1:l-1,
if ~isempty(gradW{l,ll})
directionW{l,ll} = gradW{l,ll} + momentum * directionW{l,ll};
% update rule from dropout paper:
%directionW{l,ll} = momentum * directionW{l,ll} - ...
% (1 - momentum) * stepsize * stepsizeW(l,ll) * gradW{l,ll};
end
end
end
end
% Update weights
for l = 2:nlayers
if momentum > 0
net.bias{l} = net.bias{l} - stepsize * directionbias{l};
% update rule from dropout paper
%net.bias{l} = net.bias{l} + directionbias{l};
else
net.bias{l} = net.bias{l} - stepsize * gradbias{l};
end
for ll=1:l-1,
if (~isempty(net.W{l,ll}))
if momentum > 0
net.W{l,ll} = net.W{l,ll} - stepsize * stepsizeW(l,ll) * directionW{l,ll};
% update rule from dropout paper:
% net.W{l,ll} = net.W{l,ll} + directionW{l,ll};
else
net.W{l,ll} = net.W{l,ll} - stepsize * stepsizeW(l,ll) * gradW{l,ll};
end
end
end
end
iter = iter + 1;
% Error calculations
if mod(blen*iter,dlen) == 0 % && save_data
epoch = epoch+1;
% Exponentially decreasing stepsize
% linearly increasing momentum
if epoch >= opt.burnin
stepsize = stepsize * opt.ratedecay;
momentum = opt.mominit;
else
momentum = (epoch/opt.burnin)*opt.momentum + (1-(epoch/opt.burnin))*opt.mominit;
end
if save_data
cputemp = cputime;
num_evals_complete = num_evals_complete + 1;
res.iters(num_evals_complete) = iter;
res.cputimes(num_evals_complete) = cputime-cpustart;
% Training error
current_training_output = ltmlp_ff(net, input);
if strcmp(opt.task,'regression') || strcmp(opt.task,'autoencoder')
res.training_errors(num_evals_complete) = mean(sum((current_training_output-output).^2,1),2);
elseif strcmp(opt.task,'classification')
[~,maxI1] = max(current_training_output);
[~,maxI2] = max(output);
res.training_errors(num_evals_complete) = sum(maxI1~=maxI2);
else
res.training_errors(num_evals_complete) = NaN;
end
% Test error
current_test_output = ltmlp_ff(net, test_input);
if strcmp(opt.task,'regression') || strcmp(opt.task,'autoencoder')
res.test_errors(num_evals_complete) = mean(sum((current_test_output-test_output).^2,1),2);
elseif strcmp(opt.task,'classification')
[~,maxI1]=max(current_test_output);
[~,maxI2]=max(test_output);
res.test_errors(num_evals_complete) = sum(maxI1~=maxI2);
else
res.test_errors(num_evals_complete) = NaN;
end
if momentum > 0
res.gradW(num_evals_complete) = sum(ltmlp_params2vector(directionW, net.num_params).^2);
else
res.gradW(num_evals_complete) = sum(ltmlp_params2vector(gradWm, net.num_params).^2);
end
if opt.verbose
fprintf('Epoch %4d: training error = %d, test error = %d, cputime = %d, grad = %.4f\n', ...
epoch, res.training_errors(num_evals_complete), res.test_errors(num_evals_complete), ...
floor(cputime-cpustart), res.gradW(num_evals_complete));
end
cpustart = cpustart + cputime - cputemp;
end
end
end