-
Notifications
You must be signed in to change notification settings - Fork 9
/
dcautoCost2.m
217 lines (203 loc) · 7.99 KB
/
dcautoCost2.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 dcost = dcautoCost2(p, data, num_conv, nt, patches, extra_hidden)
% dcautoCost2.m
% A gradient of the cost function for a convolutional or translationally invariant autoencoder
% See the accompanying cost function cautoCost2.m for variable definitions.
%
[nsamples, ndim] = size(data);
nf = ndim/nt;
num_hidden = patches(1).Nx*patches(2).Ny;
W = p(1:num_conv*(num_hidden+1));
W = reshape(W, [num_conv length(W)/num_conv]);
if isempty(extra_hidden) || extra_hidden(1) <= 0
R = p(num_conv*(num_hidden+1)+1:num_conv*(num_hidden+1)+ndim*(num_conv*numel(patches)+1));
R = reshape(R, [ndim length(R)/ndim]);
else
extra_layers = extra_hidden(1);
extra_num = extra_hidden(2);
H = cell(1,extra_layers);
if extra_layers == 1
index = num_conv*(patches(1).Nx*patches(2).Ny+1)+1:num_conv*(patches(1).Nx*patches(2).Ny+1)+extra_num*(num_conv*length(patches)+1);
H{1} = p(index);
H{1} = reshape(H{1}, [extra_num length(H{1})/extra_num]);
else
index = num_conv*(patches(1).Nx*patches(2).Ny+1)+1:num_conv*(patches(1).Nx*patches(2).Ny+1)+extra_num*(num_conv*length(patches)+1);
H{1} = p(index);
H{1} = reshape(H{1}, [extra_num length(H{1})/extra_num]);
for j=2:extra_layers
index = index(end)+1:index(end)+extra_num*(extra_num+1);
H{j} = p(index);
H{j} = reshape(H{j}, [extra_num length(H{j})/extra_num]);
end
end
index = index(end)+1:index(end)+ndim*(extra_num+1);
R = p(index);
R = reshape(R, [ndim length(R)/ndim]);
end
Z = zeros(size(W,1)*length(patches),nsamples);
TZ = cell(1,length(patches));
parfor k = 1:length(patches)
Xind = zeros(patches(k).Ny, patches(k).Nx);
ind = 1;
yrange = patches(k).y0:patches(k).y0+patches(k).Ny-1;
xrange = patches(k).x0:patches(k).x0+patches(k).Nx-1;
for x=xrange
Xind(:,ind) = (x-1)*nf+patches(k).y0:(x-1)*nf+patches(k).y0+patches(k).Ny-1;
ind = ind+1;
end
Xind(yrange < 1 | yrange > nf, :) = -1;
Xind(:, xrange < 1 | xrange > nt) = -1;
Xind = Xind(:);
tmp = W(:,2:end);
tmp = tmp(:, Xind' > 0 & Xind' <= size(data,2));
Xind(Xind <= 0 | Xind > size(data,2)) = [];
TZ{k} = 1./(1+exp( repmat(W(:,1), [1 nsamples]) + (data(:,Xind)*tmp')' ));
end
for k = 1:length(patches)
Z((k-1)*size(W,1)+1:k*size(W,1),:) = TZ{k};
end
if isempty(extra_hidden) || extra_hidden(1) <= 0
x = repmat(R(:,1), [1 nsamples]) + R(:,2:end)*Z;
else
Y = cell(1,extra_layers);
tmp = H{1};
Y{1} = 1./(1+exp( repmat(tmp(:,1), [1 nsamples]) + tmp(:,2:end)*Z ));
for j=2:extra_layers
tmp = H{j};
Y{j} = 1./(1+exp( repmat(tmp(:,1), [1 nsamples]) + tmp(:,2:end)*Y{j-1} ));
end
x = repmat(R(:,1), [1 nsamples]) + R(:,2:end)*Y{extra_layers};
end
dcb = ((x'-data)/size(data,1))';
if isempty(extra_hidden) || extra_hidden(1) <= 0
drb = R(:,2:end)'*dcb.*(Z.*(Z-1));
else
drb = R(:,2:end)'*dcb.*(Y{extra_layers}.*(Y{extra_layers}-1));
dhb = cell(1,extra_layers);
tmp = H{extra_layers};
if extra_layers == 1
dhb{1} = tmp(:,2:end)'*drb.*(Z.*(Z-1));
else
dhb{extra_layers} = tmp(:,2:end)'*drb.*(Y{extra_layers-1}.*(Y{extra_layers-1}-1));
for j=extra_layers-1:-1:2
tmp = H{j};
dhb{j} = tmp(:,2:end)'*dhb{j+1}.*(Y{j-1}.*(Y{j-1}-1));
end
tmp = H{1};
dhb{1} = tmp(:,2:end)'*dhb{2}.*(Z.*(Z-1));
end
end
dR = zeros(size(R(:,2:end)));
if isempty(extra_hidden) || extra_hidden(1) <= 0
parfor j=1:size(R,1)
dR(j,:) = sum(Z.*repmat(dcb(j,:), [size(R,2)-1 1]),2)';
end
else
parfor j=1:size(R,1)
dR(j,:) = sum(Y{extra_layers}.*repmat(dcb(j,:), [size(R,2)-1 1]),2)';
end
dH = cell(1,extra_layers);
if extra_layers == 1
tmp = H{1};
tmp = zeros(size(tmp(:,2:end)));
parfor j=1:size(tmp,1)
tmp(j,:) = sum(Z.*repmat(drb(j,:), [size(H{1},2)-1 1]),2)';
end
dH{1} = tmp;
else
tmp = H{extra_layers};
tmp = zeros(size(tmp(:,2:end)));
parfor j=1:size(tmp,1)
tmp(j,:) = sum(Y{extra_layers-1}.*repmat(drb(j,:), [size(H{extra_layers},2)-1 1]),2)';
end
dH{extra_layers} = tmp;
parfor k=extra_layers-1:-1:2
tmp = H{k};
tmp = zeros(size(tmp(:,2:end)));
d = dhb{k+1};
for j=1:size(tmp,1)
tmp(j,:) = sum(Y{k-1}.*repmat(d(j,:), [size(H{j},2)-1 1]),2)';
end
dH{k} = tmp;
end
tmp = H{1};
tmp = zeros(size(tmp(:,2:end)));
d = dhb{2};
parfor j=1:size(tmp,1)
tmp(j,:) = sum(Z.*repmat(d(j,:), [size(H{1},2)-1 1]),2)';
end
dH{1} = tmp;
end
end
dW = zeros(size(W(:,2:end)));
if isempty(extra_hidden) || extra_hidden(1) <= 0
for k = 1:length(patches)
Xind = zeros(patches(k).Ny, patches(k).Nx);
ind = 1;
yrange = patches(k).y0:patches(k).y0+patches(k).Ny-1;
xrange = patches(k).x0:patches(k).x0+patches(k).Nx-1;
for x=xrange
Xind(:,ind) = (x-1)*nf+patches(k).y0:(x-1)*nf+patches(k).y0+patches(k).Ny-1;
ind = ind+1;
end
Xind(yrange < 1 | yrange > nf, :) = -1;
Xind(:, xrange < 1 | xrange > nt) = -1;
Xind = Xind(:);
Sind = Xind;
Sind(Sind < 1 | Sind > size(data,2)) = [];
for j=1:size(W,1)
dW(j,Xind > 0 & Xind <= size(data,2)) = dW(j,Xind > 0 & Xind <= size(data,2)) + sum(data(:,Sind)'.*repmat(drb((k-1)*size(W,1)+j,:), [length(Sind) 1]), 2)';
end
end
else
for k = 1:length(patches)
Xind = zeros(patches(k).Ny, patches(k).Nx);
ind = 1;
yrange = patches(k).y0:patches(k).y0+patches(k).Ny-1;
xrange = patches(k).x0:patches(k).x0+patches(k).Nx-1;
for x=xrange
Xind(:,ind) = (x-1)*nf+patches(k).y0:(x-1)*nf+patches(k).y0+patches(k).Ny-1;
ind = ind+1;
end
Xind(yrange < 1 | yrange > nf, :) = -1;
Xind(:, xrange < 1 | xrange > nt) = -1;
Xind = Xind(:);
Sind = Xind;
Sind(Sind < 1 | Sind > size(data,2)) = [];
d = dhb{1};
for j=1:size(W,1)
dW(j,Xind > 0 & Xind <= size(data,2)) = dW(j,Xind > 0 & Xind <= size(data,2)) + sum(data(:,Sind)'.*repmat(d((k-1)*size(W,1)+j,:), [length(Sind) 1]), 2)';
end
end
end
DR = [sum(dcb,2) dR];
drb = sum(drb,2);
if isempty(extra_hidden) || extra_hidden(1) <= 0
drb = reshape(drb, [length(drb)/length(patches) length(patches)]);
drb = sum(drb,2);
DW = [drb dW];
else
if extra_layers == 1
DH = [drb dH{1}];
else
DH = [];
for j=1:extra_layers-1
tmp = [sum(dhb{j+1},2) dH{j}];
DH = [DH reshape(tmp, [1 numel(tmp)])];
end
tmp = [drb dH{extra_layers}];
DH = [DH reshape(tmp, [1 numel(tmp)])];
end
dhb{1} = sum(dhb{1},2);
dhb{1} = reshape(dhb{1}, [length(dhb{1})/length(patches) length(patches)]);
dhb{1} = sum(dhb{1},2);
DW = [sum(dhb{1},2) dW];
end
DW = reshape(DW, [1 numel(DW)]);
DR = reshape(DR, [1 numel(DR)]);
if isempty(extra_hidden) || extra_hidden(1) <= 0
dcost = [DW DR];
else
DH = reshape(DH, [1 numel(DH)]);
dcost = [DW DH DR];
end
end