-
Notifications
You must be signed in to change notification settings - Fork 95
/
dbn.m
341 lines (285 loc) · 11.2 KB
/
dbn.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
% dbn - training a DBN with up-down algorithm
% Copyright (C) 2011 KyungHyun Cho, Tapani Raiko, Alexander Ilin
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
%
function [D] = dbn(D, patches);
actual_lrate = D.learning.lrate;
n_samples = size(patches, 1);
layers = D.structure.layers;
n_layers = length(layers);
if layers(1) ~= size(patches, 2)
error('Data is not properly aligned');
end
minibatch_sz = D.learning.minibatch_sz;
n_minibatches = ceil(n_samples / minibatch_sz);
n_epochs = D.iteration.n_epochs;
momentum = D.learning.momentum;
weight_decay = D.learning.weight_decay;
rec.biases_grad = cell(n_layers-1, 1);
rec.W_grad = cell(n_layers-1, 1);
rec.biases_grad_old = cell(n_layers-1, 1);
rec.W_grad_old = cell(n_layers-1, 1);
for l = 1:n_layers-1
rec.biases_grad{l} = zeros(size(D.rec.biases{l}))';
if l < n_layers
rec.W_grad{l} = zeros(size(D.rec.W{l}));
end
rec.biases_grad_old{l} = zeros(size(D.rec.biases{l}))';
if l < n_layers
rec.W_grad_old{l} = zeros(size(D.rec.W{l}));
end
end
gen.biases_grad = cell(n_layers-1, 1);
gen.W_grad = cell(n_layers-1, 1);
gen.biases_grad_old = cell(n_layers-1, 1);
gen.W_grad_old = cell(n_layers-1, 1);
for l = 1:n_layers-1
gen.biases_grad{l} = zeros(size(D.gen.biases{l}))';
if l < n_layers
gen.W_grad{l} = zeros(size(D.gen.W{l}));
end
gen.biases_grad_old{l} = zeros(size(D.gen.biases{l}))';
if l < n_layers
gen.W_grad_old{l} = zeros(size(D.gen.W{l}));
end
end
top.W_grad = zeros(size(D.top.W));
top.vbias_grad = zeros(size(D.top.vbias));
top.hbias_grad = zeros(size(D.top.hbias));
top.W_grad_old = zeros(size(D.top.W));
top.vbias_grad_old = zeros(size(D.top.vbias));
top.hbias_grad_old = zeros(size(D.top.hbias));
min_recon_error = Inf;
min_recon_error_update_idx = 0;
stopping = 0;
anneal_counter = 0;
actual_lrate0 = actual_lrate;
if D.debug.do_display == 1
figure(D.debug.display_fid);
end
try
use_gpu = gpuDeviceCount;
catch errgpu
use_gpu = false;
disp(['Could not use CUDA. Error: ' errgpu.identifier])
end
for step=1:n_epochs
if D.verbose
fprintf(2, 'Epoch %d/%d: ', step, n_epochs)
end
if use_gpu
% push
for l = 1:n_layers-1
if l < n_layers-1
D.rec.W{l} = gpuArray(single(D.rec.W{l}));
D.gen.W{l} = gpuArray(single(D.gen.W{l}));
end
D.rec.biases{l} = gpuArray(single(D.rec.biases{l}));
D.gen.biases{l} = gpuArray(single(D.gen.biases{l}));
end
D.top.W = gpuArray(single(D.top.W));
D.top.vbias = gpuArray(single(D.top.vbias));
D.top.hbias = gpuArray(single(D.top.hbias));
end
for mb=1:n_minibatches
D.iteration.n_updates = D.iteration.n_updates + 1;
v0 = patches((mb-1) * minibatch_sz + 1:min(mb * minibatch_sz, n_samples), :);
mb_sz = size(v0,1);
if use_gpu > 0
v0 = gpuArray(single(v0));
end
% learning rate
if D.learning.lrate_anneal > 0 && (step >= D.learning.lrate_anneal * n_epochs)
anneal_counter = anneal_counter + 1;
actual_lrate = actual_lrate0 / anneal_counter;
else
if D.learning.lrate0 > 0
actual_lrate = D.learning.lrate / (1 + D.iteration.n_updates / D.learning.lrate0);
else
actual_lrate = D.learning.lrate;
end
actual_lrate0 = actual_lrate;
end
D.signals.lrates = [D.signals.lrates actual_lrate];
% recognition (wake)
h0r = cell(n_layers-1, 1);
h0r{1} = v0;
if D.learning.ffactored == 0
h0r{1} = binornd(1, h0r{1});
end
for l = 2:(n_layers-1)
h0r{l} = sigmoid(bsxfun(@plus, h0r{l-1} * D.rec.W{l-1}, D.rec.biases{l}'));
if D.learning.ffactored == 0
h0r{l} = binornd(1, h0r{l});
end
end
r0 = cell(n_layers-1, 1);
for l = 1:(n_layers-2)
r0{l} = sigmoid(bsxfun(@plus, h0r{l+1} * D.gen.W{l}', D.gen.biases{l}'));
end
% update generative weight
for l = 1:(n_layers-2)
gen.W_grad{l} = ((h0r{l} - r0{l})' * h0r{l+1}) / size(v0, 1);
gen.W_grad_old{l} = (1 - momentum) * gen.W_grad{l} + momentum * gen.W_grad_old{l};
D.gen.W{l} = D.gen.W{l} + actual_lrate * (gen.W_grad_old{l} - D.learning.weight_decay * D.gen.W{l});
gen.biases_grad{l} = mean(h0r{l} - r0{l}, 1);
gen.biases_grad_old{l} = (1 - momentum) * gen.biases_grad{l} + momentum * gen.biases_grad_old{l};
D.gen.biases{l} = D.gen.biases{l} + actual_lrate * (gen.biases_grad_old{l}' - D.learning.weight_decay * D.gen.biases{l});
end
% contrastive steps
if D.learning.persistent_cd == 0 || D.iteration.n_updates == 1
vtop = h0r{end};
end
for cs = 1:D.learning.contrastive_step
htop = sigmoid(bsxfun(@plus, vtop * D.top.W, D.top.hbias'));
htop = binornd(1, htop);
vtop = sigmoid(bsxfun(@plus, htop * D.top.W', D.top.vbias'));
vtop = binornd(1, vtop);
end
tv0 = h0r{end};
th0 = sigmoid(bsxfun(@plus, tv0 * D.top.W, D.top.hbias'));
tv1 = vtop;
th1 = sigmoid(bsxfun(@plus, tv1 * D.top.W, D.top.hbias'));
top.W_grad = (tv0' * th0)/size(tv0,1) - (tv1' * th1)/size(tv1,1);
top.vbias_grad = mean(tv0, 1) - mean(tv1, 1);
top.hbias_grad = mean(th0, 1) - mean(th1, 1);
top.W_grad_old = (1 - momentum) * top.W_grad + momentum * top.W_grad_old;
top.vbias_grad_old = (1 - momentum) * top.vbias_grad' + momentum * top.vbias_grad_old;
top.hbias_grad_old = (1 - momentum) * top.hbias_grad' + momentum * top.hbias_grad_old;
D.top.W = D.top.W + actual_lrate * (top.W_grad_old - D.learning.weight_decay * D.top.W);
D.top.vbias = D.top.vbias + actual_lrate * (top.vbias_grad_old - D.learning.weight_decay * D.top.vbias);
D.top.hbias = D.top.hbias + actual_lrate * (top.hbias_grad_old - D.learning.weight_decay * D.top.hbias);
% generation (sleep)
h0r{n_layers-1} = tv1;
for l = (n_layers-2):-1:1
h0r{l} = sigmoid(bsxfun(@plus, h0r{l+1} * D.gen.W{l}', D.gen.biases{l}'));
if D.learning.ffactored == 0
h0r{l} = binornd(1, h0r{l});
end
end
for l = 2:(n_layers - 1)
r0{l} = sigmoid(bsxfun(@plus, h0r{l-1} * D.rec.W{l-1}, D.rec.biases{l}'));
end
% update recognition weight
for l = 1:(n_layers-1)
if l < n_layers - 1
rec.W_grad{l} = (h0r{l}' * (h0r{l+1} - r0{l+1})) / size(h0r{1}, 1);
rec.W_grad_old{l} = (1 - momentum) * rec.W_grad{l} + momentum * rec.W_grad_old{l};
D.rec.W{l} = D.rec.W{l} + actual_lrate * (rec.W_grad_old{l} - D.learning.weight_decay * D.rec.W{l});
end
if l > 1
rec.biases_grad{l} = mean(h0r{l} - r0{l}, 1);
rec.biases_grad_old{l} = (1 - momentum) * rec.biases_grad{l} + momentum * rec.biases_grad_old{l};
D.rec.biases{l} = D.rec.biases{l} + actual_lrate * (rec.biases_grad_old{l}' - D.learning.weight_decay * D.rec.biases{l});
end
end
clear h0r r0 htop tv0 th0 tv1 th1;
if D.learning.persistent_cd == 0
clear vtop;
end
% compute reconstruction error
hr = v0;
for l = 2:n_layers-1
hr = sigmoid(bsxfun(@plus, hr * D.rec.W{l-1}, D.rec.biases{l}'));
end
hr = sigmoid(bsxfun(@plus, hr * D.top.W, D.top.hbias'));
hr = sigmoid(bsxfun(@plus, hr * D.top.W', D.top.vbias'));
for l = n_layers-1:-1:2
hr = sigmoid(bsxfun(@plus, hr * D.gen.W{l-1}', D.gen.biases{l-1}'));
end
rerr = mean(sum((v0 - hr).^2,2));
if use_gpu > 0
rerr = gather(rerr);
end
D.signals.recon_errors = [D.signals.recon_errors rerr];
if D.verbose == 1
fprintf(2, '.');
end
if use_gpu > 0
clear v0 h0d h0e v0_clean vr hr deltae deltad
end
if D.stop.criterion > 0
if D.stop.criterion == 1
if min_recon_error > D.signals.recon_errors(end)
min_recon_error = D.signals.recon_errors(end);
min_recon_error_update_idx = D.iteration.n_updates;
else
if D.iteration.n_updates > min_recon_error_update_idx + D.stop.recon_error.tolerate_count
fprintf(2, '\nStopping criterion reached (recon error) %f > %f\n', ...
D.signals.recon_errors(end), min_recon_error);
stopping = 1;
break;
end
end
else
error ('Unknown stopping criterion %d', D.stop.criterion);
end
end
if length(D.hook.per_update) > 1
err = D.hook.per_update{1}(D, D.hook.per_update{2});
if err == -1
stopping = 1;
break;
end
end
if D.debug.do_display == 1 && mod(D.iteration.n_updates, D.debug.display_interval) == 0
D.debug.display_function (D.debug.display_fid, D, v0, v1, h0, h1, W_grad, vbias_grad, hbias_grad);
drawnow;
end
end
if use_gpu > 0
% pull
for l = 1:n_layers-1
if l < n_layers-1
D.rec.W{l} = gather(D.rec.W{l});
D.gen.W{l} = gather(D.gen.W{l});
end
D.rec.biases{l} = gather(D.rec.biases{l});
D.gen.biases{l} = gather(D.gen.biases{l});
end
D.top.W = gather(D.top.W);
D.top.vbias = gather(D.top.vbias);
D.top.hbias = gather(D.top.hbias);
end
if length(D.hook.per_epoch) > 1
err = D.hook.per_epoch{1}(D, D.hook.per_epoch{2});
if err == -1
stopping = 1;
end
end
if stopping == 1
break;
end
if D.verbose == 1
fprintf(2, '\n');
end
fprintf(2, 'Epoch %d/%d - recon_error: %f\n', step, n_epochs, ...
D.signals.recon_errors(end));
end
if use_gpu > 0
% pull
for l = 1:n_layers-1
if l < n_layers-1
D.rec.W{l} = gather(D.rec.W{l});
D.gen.W{l} = gather(D.gen.W{l});
end
D.rec.biases{l} = gather(D.rec.biases{l});
D.gen.biases{l} = gather(D.gen.biases{l});
end
D.top.W = gather(D.top.W);
D.top.vbias = gather(D.top.vbias);
D.top.hbias = gather(D.top.hbias);
end