forked from qipeng/convolutionalRBM.m
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainCRBM.m
297 lines (251 loc) · 10.2 KB
/
trainCRBM.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
function [model, output] = trainCRBM(data, params, oldModel)
% TRAINCRBM Trains a convolutional restricted Boltzmann machine
% with the specified parameters.
%
% [model output] = TRAINCRBM(data, params, oldModel)
%
% data should be a structure, containing:
% data.x The input images / pooling states of the previous layer
% of CRBM. This matrix is 4-D the first three dimensions
% define an image (coloum-stored with a color channel),
% and the last dimension indexes through the batch of
% images. I.e. the four dimensions are: height, width,
% channels (1 for grayscale, 3 for RGB), and number of
% images.
%
% Written by: Peng Qi, Sep 27, 2012
% Last Updated: Feb 8, 2014
% Version: 0.3 alpha
if params.verbose > 0,
fprintf('Starting training CRBM with the following parameters:\n');
disp(params);
fprintf('Initializing parameters...');
end
useCuda = params.useCuda;
if isfield(params, 'method'),
if strcmp(params.method, 'CD'),
method = 1; % Contrastive Divergence
elseif strcmp(params.method, 'PCD'),
method = 2; % Persistent Contrastive Divergence
end
else
method = 1; % use Contrastive Divergence as default
end
%% initialization
N = size(data.x, 4);
Nfilters = params.nmap;
Wfilter = params.szFilter;
p = params.szPool;
H = size(data.x, 1);
W = size(data.x, 2);
colors = size(data.x, 3);
Hhidden = H - Wfilter + 1;
Whidden = W - Wfilter + 1;
Hpool = floor(Hhidden / p);
Wpool = floor(Whidden / p);
param_iter = params.iter;
param_szBatch = params.szBatch;
output_enabled = nargout > 1;
%vmasNfilters = conve(ones(nh), ones(m), useCuda);
hinit = 0;
if params.sparseness > 0,
hinit = -.2;
end
if exist('oldModel','var') && ~isempty(oldModel),
model = oldModel;
if (~isfield(model,'W')),
model.W = 0.01 * randn(Wfilter, Wfilter, colors, Nfilters);
else
if (size(model.W) ~= [Wfilter Wfilter colors Nfilters]), error('Incompatible input model.'); end
end
if (~isfield(model,'vbias')), model.vbias = zeros(1, colors);end
if (~isfield(model,'hbias')), model.hbias = ones(1, Nfilters) * hinit;end
if (~isfield(model,'sigma')),
if (params.sparseness > 0)
model.sigma = 0.1;
else
model.sigma = 1;
end
end
else
model.W = 0.01 * randn(Wfilter, Wfilter, colors, Nfilters);
model.vbias = zeros(1, colors);
model.hbias = ones(1, Nfilters) * hinit;
if (params.sparseness > 0)
model.sigma = 0.1;
else
model.sigma = 1;
end
end
dW = 0;
dvbias = 0;
dhbias = 0;
pW = params.pW;
pvbias = params.pvbias;
phbias = params.phbias;
if output_enabled,
output.x = zeros(Hpool, Wpool, Nfilters, N);
end
total_batches = ceil(N / param_szBatch);
if params.verbose > 0,
fprintf('Completed.\n');
end
hidq = params.sparseness;
lambdaq = 0.9;
if ~isfield(model,'iter')
model.iter = 0;
end
if (params.whitenData),
try
load(sprintf('whitM_%d_%d', params.szFilter, size(data.x,3)));
catch e,
if (params.verbose > 1), fprintf('\nComputing whitening matrix...');end
compWhitMatrix(data.x, params.szFilter);
load(sprintf('whitM_%d_%d', params.szFilter, size(data.x, 3)));
if (params.verbose > 1), fprintf('Completed.\n');end
end
if (params.verbose > 0), fprintf('Whitening data...\n'); end
data.x = whiten_data(data.x, whM, useCuda);
if (params.verbose > 0), fprintf('Whitening Completed.\n'); end
end
if method == 2,
phantom = randn(H, W, colors, N);
end
for iter = model.iter+1:param_iter,
% shuffle data
batch_idx = randperm(N);
if params.verbose > 0,
fprintf('Iteration %d\n', iter);
if params.verbose > 1,
fprintf('Batch progress (%d total): ', total_batches);
end
end
hidact = zeros(1, Nfilters);
errsum = 0;
if (iter > 5),
params.pW = .9;
params.pvbias = 0;
params.phbias = 0;
end
for batch = 1:total_batches,
batchdata = data.x(:,:,:,batch_idx((batch - 1) * param_szBatch + 1 : ...
min(batch * param_szBatch, N)));
if method == 2,
phantomdata = phantom(:,:,:,((batch - 1) * param_szBatch + 1 : ...
min(batch * param_szBatch, N)));
end
recon = batchdata;
%% positive phase
%% hidden update
model_W = model.W;
model_hbias = model.hbias;
model_vbias = model.vbias;
poshidacts = convs(recon, model_W, useCuda);
[poshidprobs, pospoolprobs, poshidstates] = poolHidden(poshidacts / model.sigma, model_hbias / model.sigma, p, useCuda);
if output_enabled && ~rem(iter, params.saveInterv),
output_x = pospoolprobs;
end
if output_enabled && ~rem(iter, params.saveInterv),
output.x(:,:,:,batch_idx((batch - 1) * param_szBatch + 1 : ...
batch * param_szBatch)) = output_x;
end
%% negative phase
%% reconstruct data from hidden variables
if method == 1,
recon = conve(poshidprobs, model_W, useCuda);
elseif method == 2,
recon = phantomdata;
end
recon = bsxfun(@plus, recon, reshape(model_vbias, [1 1 colors]));
if (params.sparseness > 0),
recon = recon + model.sigma * randn(size(recon));
end
%% mean field hidden update
neghidacts = convs(recon, model_W, useCuda);
neghidprobs = poolHidden(neghidacts / model.sigma, model_hbias / model.sigma, p, useCuda);
if (params.verbose > 1),
fprintf('.');
err = batchdata - recon;
errsum = errsum + sum(err(:).^2);
if (params.verbose > 4),
%% visualize data, reconstruction, and filters (still experimental)
figure(1);
for i = 1:16,subplot(4,8,i+16);imagesc(model.W(:,:,:,i));axis image off;end;colormap gray;drawnow;
subplot(2,2,1);imagesc(batchdata(:,:,1));colormap gray;axis off;title('data (ZCA''d)');
subplot(2,2,2);imagesc(recon(:,:,1));colormap gray;axis off;title('reconstruction');
drawnow;
end
end
%% contrast divergence update on params
if (params.sparseness > 0),
hidact = hidact + reshape(sum(sum(sum(pospoolprobs, 4), 2), 1), [1 Nfilters]);
else
dhbias = phbias * dhbias + ...
reshape((sum(sum(sum(poshidprobs, 4), 2), 1) - sum(sum(sum(neghidprobs, 4), 2), 1))...
/ Whidden / Hhidden / param_szBatch, [1 Nfilters]);
end
dvbias = pvbias * dvbias + ...
reshape((sum(sum(sum(batchdata, 4), 2), 1) - sum(sum(sum(recon, 4), 2), 1))...
/ H / W / param_szBatch, [1 colors]);
ddw = convs4(batchdata(Wfilter:H-Wfilter+1,Wfilter:W-Wfilter+1,:,:), poshidprobs(Wfilter:Hhidden-Wfilter+1,Wfilter:Whidden-Wfilter+1,:,:), useCuda) ...
- convs4( recon(Wfilter:H-Wfilter+1,Wfilter:W-Wfilter+1,:,:), neghidprobs(Wfilter:Hhidden-Wfilter+1,Wfilter:Whidden-Wfilter+1,:,:), useCuda);
dW = pW * dW + ddw / (Hhidden - 2 * Wfilter + 2) / (Whidden - 2 * Wfilter + 2) / param_szBatch;
model.vbias = model.vbias + params.epsvbias * dvbias;
if params.sparseness <= 0,
model.hbias = model.hbias + params.epshbias * dhbias;
end
model.W = model.W + params.epsW * (dW - params.decayw * model.W);
%% experimental code for saving debugging info for mex implementations
% save dbgInfo model poshidacts poshidprobs poshidstates recon neghidacts neghidprobs model_W
% if any(isnan(model.W(:))) || any(isnan(poshidacts(:))) || any(isnan(poshidprobs(:))) || any(isnan(poshidstates(:))) ...
% || any(isnan(recon(:))) || any(isnan(neghidacts(:))) || any(isnan(neghidprobs(:))),
% return;
% end
if method == 2,
phantom(:,:,:,batch_idx((batch - 1) * param_szBatch + 1 : ...
batch * param_szBatch)) = conve(neghidprobs, model_W, useCuda);
end
end
if (params.verbose > 1),
fprintf('\n\terror:%f', errsum);
end
if params.sparseness > 0,
hidact = hidact / Hhidden / Whidden / N;
hidq = hidq * lambdaq + hidact * (1 - lambdaq);
dhbias = phbias * dhbias + ((params.sparseness) - (hidq));
model.hbias = model.hbias + params.epshbias * dhbias;
if params.verbose > 0,
if (params.verbose > 1),
fprintf('\tsigma:%f', model.sigma);
end
fprintf('\n\tsparseness: %f\thidbias: %f\n', sum(hidact) / Nfilters, sum(model.hbias) / Nfilters);
end
if (model.sigma > 0.01),
model.sigma = model.sigma * 0.99;
end
end
if ~rem(iter, params.saveInterv),
if (params.verbose > 3),
%% visualize data, reconstruction, and filters (still experimental)
figure(1);
for i = 1:16,subplot(4,8,i+16);imagesc(model.W(:,:,:,i));axis image off;end;colormap gray;drawnow;
subplot(2,2,1);imagesc(batchdata(:,:,:,1));colormap gray;axis off;title('data (ZCA''d)');
subplot(2,2,2);imagesc(recon(:,:,:,1));colormap gray;axis off;title('reconstruction');
drawnow;
end
if output_enabled,
model.iter = iter;
save(params.saveName, 'model', 'output', 'iter');
if params.verbose > 1,
fprintf('Model and output saved at iteration %d\n', iter);
end
else
model.iter = iter;
save(params.saveName, 'model', 'iter');
if params.verbose > 1,
fprintf('Model saved at iteration %d\n', iter);
end
end
end
end
end