/
rbm.m
175 lines (140 loc) · 5.21 KB
/
rbm.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
% This program trains Restricted Boltzmann Machine in which
% visible, binary, stochastic pixels are connected to
% hidden, binary, stochastic feature detectors using symmetrically
% weighted connections. Learning is done with 1-step Contrastive Divergence.
% The program assumes that the following variables are set externally:
% maxepoch -- maximum number of epochs
% numhid -- number of hidden units
% batchdata -- the data that is divided into batches (numcases numdims numbatches)
% restart -- set to 1 if learning starts from beginning
colormap(gray(64));
epsilonw = 0.1; % Learning rate for weights
epsilonvb = 0.1; % Learning rate for biases of visible units
epsilonhb = 0.1; % Learning rate for biases of hidden units
weightcost = 0.0002;
initialmomentum = 0.5;
finalmomentum = 0.9;
[numcases numdims numbatches] = size(batchdata);
if restart == 1
restart=0;
epoch=1;
% Symmetric weights
vishid = 0.1 * randn(numdims, numhid);
% Biases
hidbiases = zeros(1, numhid);
visbiases = zeros(1, numdims);
poshidprobs = zeros(numcases, numhid);
neghidprobs = zeros(numcases, numhid);
posprods = zeros(numdims, numhid);
negprods = zeros(numdims, numhid);
% For tracking momentum
vishidinc = zeros(numdims, numhid);
hidbiasinc = zeros(1, numhid);
visbiasinc = zeros(1, numdims);
batchposhidprobs=zeros(numcases, numhid, numbatches);
end
for epoch = epoch:maxepoch
fprintf(1, 'epoch %d\r', epoch);
errsum=0;
for batch = 1:20
%for batch = 1:numbatches
fprintf(1, 'epoch %d batch %d\r', epoch, batch);
%%% Start positive phase %%%
data = batchdata(:,:,batch);
poshidprobs = sigmoid(data*vishid + repmat(hidbiases, numcases, 1));
batchposhidprobs(:,:,batch)=poshidprobs;
posprods = data' * poshidprobs;
poshidact = sum(poshidprobs);
posvisact = sum(data);
if mod(batch, 100) == 0 && mod(batch, 200) != 0
clf;
pixels = [poshidprobs];
pixels = [pixels; repmat(ones(1, numhid), 5, 1)];
pixels = [pixels; repmat(sigmoid(hidbiases), 10, 1)];
pixels = [pixels; repmat(ones(1, numhid), 5, 1)];
vis1 = visbiases(1:length(visbiases)/2);
vis1 = repmat([sigmoid(vis1) ones(1, numhid - length(vis1))], 10, 1);
vis2 = visbiases((length(visbiases)/2)+1:length(visbiases));
vis2 = repmat([sigmoid(vis2) ones(1, numhid - length(vis2))], 10, 1);
pixels = [pixels; vis1];
pixels = [pixels; vis2];
pixels = uint8(round(pixels .* 64));
imagesc(pixels);
axis image off
drawnow;
end
if numdims == (28*28) && mod(batch, 200) == 0
rows = [];
row = [];
for i = 1:numhid;
w_i = vishid(:,i);
w_i = sigmoid(w_i);
w_i = reshape(w_i, 28, 28)';
row = [row, w_i];
if mod(i, 30) == 0
rows = [rows; row];
row = [];
end
end
if length(row) > 0
numentries = length(row)/28;
for i = 1:(30 - numentries)
row = [row, zeros(28, 28)];
end
rows = [rows; row];
end
imshow(rows, [0.0 1.0]);
axis image off
drawnow;
end
%%% End of positive phase %%%
poshidstates = poshidprobs > rand(numcases, numhid);
%%% Start negative phase %%%
negdata = sigmoid(poshidstates*vishid' + repmat(visbiases, numcases, 1));
neghidprobs = sigmoid(negdata*vishid + repmat(hidbiases, numcases, 1));
negprods = negdata'*neghidprobs;
neghidact = sum(neghidprobs);
negvisact = sum(negdata);
%%% End of negative phase %%%
err = sum(sum( (data-negdata).^2 ));
errsum = err + errsum;
if epoch > 5
momentum = finalmomentum;
else
momentum = initialmomentum;
end
%%% Update weights and biases %%%
vishidinc = momentum*vishidinc + ...
epsilonw*( (posprods-negprods)/numcases - weightcost*vishid);
visbiasinc = momentum*visbiasinc + (epsilonvb/numcases)*(posvisact-negvisact);
hidbiasinc = momentum*hidbiasinc + (epsilonhb/numcases)*(poshidact-neghidact);
vishid = vishid + vishidinc;
visbiases = visbiases + visbiasinc;
hidbiases = hidbiases + hidbiasinc;
end
fprintf(1, 'epoch %4i error %6.1f \n', epoch, errsum);
if numdims == (28*28)
rows = [];
row = [];
for i = 1:numhid;
w_i = vishid(:,i);
w_i = sigmoid(w_i + visbiases');
w_i = reshape(w_i, 28, 28)';
row = [row, w_i];
if mod(i, 30) == 0
rows = [rows; row];
row = [];
end
end
if length(row) > 0
numentries = length(row)/28;
for i = 1:(30 - numentries)
row = [row, zeros(28, 28)];
end
rows = [rows; row];
end
imshow(rows, [0.0 1.0]);
axis image off
drawnow;
end
end;