-
Notifications
You must be signed in to change notification settings - Fork 2
/
Layer.m
314 lines (291 loc) · 14.9 KB
/
Layer.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
classdef Layer < handle
properties
winningIndex,
numberOfFilters,
filterDimension,
prevDimension,
layerType, % 1 - Convolutional, 2 - Pooling, 3 - FUlly connected
activationFunction % 0 - Relu, 1 - Sigmoid, 2 - Softmax
filters,
layerOutput,
error,
sigma,
layerInput,
bias,
fanIn,
fanOut
end
methods
%function layer = Layer(layerType,numberOfFilters, filterDimension, activationFunction, prevDimension)
%layer.layerType = layerType;
%layer.numberOfFilters = numberOfFilters;
%layer.filterDimension = filterDimension; %Make it robust with optional arguments
%layer.prevDimension = prevDimension;
%end
function output = Layer(attributesArray)
if(nargin~=0)
size_attribute_array = size(attributesArray);
m = size_attribute_array(1);
output(m+1) = output;
for i = 1:m
output(i).layerType = attributesArray(i, 1);
output(i).numberOfFilters = attributesArray(i, 2);
output(i).filterDimension = attributesArray(i, 3);
output(i).activationFunction = attributesArray(i, 4);
output(i).prevDimension = attributesArray(i, 5);
output(i).fanIn = attributesArray(i,6);
output(i).fanOut = attributesArray(i,7);
randomFilters = rand(output(i).filterDimension,output(i).filterDimension,output(i).prevDimension, output(i).numberOfFilters);
intervalValue = sqrt(2)/(output(i).fanIn);
randomBias = zeros(output(i).numberOfFilters,1);
if(output(i).layerType==3)
% Should match input dimensionality
%randomFilters = rand(output(i).prevDimension, output(i).filterDimension);
randomFilters = rand(output(i).numberOfFilters, output(i).prevDimension);
intervalValue = sqrt(2)/size(randomFilters, 2);
end
% randomFilters = -intervalValue + 2*intervalValue*randomFilters;
randomFilters = randomFilters * intervalValue;
output(i).filters = randomFilters;
output(i).bias = randomBias;
% output(i).bias = -intervalValue + 2*intervalValue*randomBias;
%Handle case for fully connected
end
end
end
function output = activation(layer, input)
output = 0;
switch(layer.activationFunction)
case 0
if input < 0
output = 0;
else
output = input;
end
case 1
% output =
case 2
output = input;
% disp(input);
% input = exp(input);
max_element = max(input);
min_element = min(input);
input = exp(input-max_element);
% disp(min_element);
% disp(max_element);
denominator = sum(input);
% disp(size(input))
for i = 1:size(input, 1)
output(i) = input(i)/denominator;
end
% We expect input to be array
% output =
% Softmax: For last layer, output probabilities. Exponential to
% widen the differences
end
end
function output = layerFunction(layer, input)
% call Convolve + Max Pooling Here
% Add third dimension equal to previous layer dimension
% Depending on dimension
size_input = size(input);
output = zeros(size_input(1), size_input(2), layer.numberOfFilters);
if layer.layerType == 2
output = zeros(size_input(1)/2, size_input(2)/2, layer.numberOfFilters);
end
if layer.layerType == 3
%disp(size(output))
output = zeros(layer.numberOfFilters, 1);
end
% disp(randomFilters(:, :, :, 1))
i = layer.numberOfFilters;
while (i~=0)
switch layer.layerType
case 1
% disp(size(layer.filters));
% disp(layer.filters(:, :, :, i))
output(:,:,i) = convolve(input, layer.filters(:, :, :, i));
% output(:,:,i) = output(:,:,i)
% output(:,:,i) = output(:,:,i)+ layer.bias(i);
%output(:,:,i) = conv2(input,rot90(layer.filters(:,:,:,i), 2), 'same');
case 2
[poolingRes, layer.winningIndex] = maxPooling(input, layer.filters(:, :, :, i));
output(:,:,i) = poolingRes;
%layer.winningIndex = zeros(size(output, 1),size(output, 2),layer.numberOfFilters);
% for l = 1:size(output, 1)
% for k =1:size(output, 2)
% layer.winningIndex(l, k) = maxpool_winnner(layer.filters(:, :, :, i), poolingRes(l, k, i));
% end
% end
% TODO: CASE 3
case 3
input = reshape(input', [1, size(input, 1) * size(input, 2) * size(input, 3)]);
% disp(size(input));
%disp(size(layer.filters(i, :)));
output = fullyConnected(layer.filters, input);
% output = output + layer.bias;
break;
% disp(size(output))
otherwise
disp("Please enter correct layer type.")
end
i = i - 1;
end
layer.layerInput = input;
% disp(layer.activation(10))
if(layer.layerType~=2)
if layer.activationFunction~=2
for i = 1:size(output, 1)
for j = 1:size(output, 2)
for k = 1:size(output, 3)
output(i, j, k) = layer.activation(output(i, j, k));
end
end
end
else
output = layer.activation(output);
end
end
layer.layerOutput = output;
end
function output = calculateError(layer, nextLayer, actualY, prevOutput) %Actual filters
alpha = 0.001;
clipBy = 100;
if(layer.layerType==3 && layer.activationFunction == 2)
% delF = layer.layerOutput * (1 - layer.layerOutput);
%delF = (1-layer.layerOutput) * -1;
% output = (output .* layer.input)) * nextError; % Store the input too
% output = (1./layer.layerOutput).*actualY * delF; % Check this
%output = repmat(delF, [1, size(layer.filters, 2)]).*repmat(prevOutput', [size(layer.filters, 1), 1]);
%output = repmat(actualY, [1,size(layer.filters, 2)]).*output;
layer.sigma = (layer.layerOutput - actualY);
output = layer.sigma;
output = repmat(output, [1, size(layer.filters, 2)]);
output = output .* repmat(prevOutput', [size(layer.filters, 1), 1]);
% output = clipValue(output, clipBy);
layer.filters = layer.filters - alpha*output; % Refactor to learning rate
% layer.sigma = -actualY.*(1 - layer.layerOutput);
layer.bias = layer.bias - alpha * layer.sigma;
layer.error = output;
end
if(layer.layerType==3 && layer.activationFunction == 0)
% dE/dwi = dE/dO * dO/dWi
% dO/dWi = f'(input) * wi - Take input
% dE/dOl = dE/dOl+1 * dOl+1/dOl
% dE/dOl = nextError * dOl+1/dOl
% dOl+1/dOl = summation(weights associated)
% Ith weight
output = zeros(size(layer.filters, 1), size(layer.filters, 2));
layer.sigma = zeros(size(layer.filters, 1),1);
layer.sigma = nextLayer.filters' * nextLayer.sigma;
layer.sigma = layer.sigma.*(layer.layerOutput~=0);
output = layer.sigma * layer.layerInput;
% for i = 1:size(layer.filters, 1)
% layer.sigma(i) = nextLayer.filters(:, i)' * nextLayer.sigma;
% for j = 1:size(layer.filters, 2)
% s = layer.sigma(i) * layer.layerInput(1, j);
% %sum(nextLayer.filters(:, i));
% output(i, j) = s;
% end
% end
% Multiply next error here
%output = output * nextLayer.error; % Check this
% output = clipValue(output, clipBy);
layer.filters = layer.filters - alpha * output; % Next layer's errors would be an array. What are we multpiplyi8ng by?
layer.error = output;
layer.bias = layer.bias - alpha * layer.sigma;
end
if(layer.layerType==2)
%Gradient will be routed to the right node.
% We assign gradients to the previous layer here, since the
% others would become zero
% Gradient of neuron vs gradient of weight.
for i = 1:size(layer.winningIndex, 1)
for j = 1:size(layer.winningIndex, 2)
x = layer.winningIndex(i, j, 1);
y = layer.winningIndex(i, j, 2);
z = layer.winningIndex(i, j, 3);
onedIndex = (i-1)*size(layer.winningIndex, 2)+j;
% disp("Coordinates: ");
% disp(x);
% disp(y);
% disp(z);
% disp(onedIndex);
s = sum(nextLayer.sigma.*nextLayer.filters(:,onedIndex));
output(x, y, z) = s; %Fix dimensionality
end
end
% What ever is connected to it will be routed
% The error w.r.t output for prev lay0er would be routed.
layer.error = output;
end
if(layer.layerType == 1)
% First calculate Sigma
% Sigma will be 2D, convolve it with the kth dimension of the
% previous output
output = zeros(layer.filterDimension,layer.filterDimension,layer.prevDimension,layer.numberOfFilters);
layer.error = output;
outputDimensionX = size(layer.layerOutput, 1);
outputDimensionY = size(layer.layerOutput, 2);
if(nextLayer.layerType == 2)
%CHeck if entering
sigma = zeros(outputDimensionX, outputDimensionY, layer.numberOfFilters);
for i = 1 : size(nextLayer.winningIndex, 1)
for j = 1:size(nextLayer.winningIndex, 2)
x = nextLayer.winningIndex(i, j, 1);
y = nextLayer.winningIndex(i, j, 2);
z = nextLayer.winningIndex(i, j, 3);
sigma(x, y, z) = nextLayer.error(x, y, z);
end
end
layer.sigma = sigma;
else
sigma = zeros(outputDimensionX, outputDimensionY, layer.numberOfFilters);
% alternateSigma = zeros(outputDimensionX, outputDimensionY, layhe
rotatedFilters = nextLayer.filters;
for k = 1:layer.numberOfFilters
rotatedFilters(:,:,k,:) = rot90(nextLayer.filters(:,:,k,:), 2);
end
for k = 1 : layer.numberOfFilters
l = 0;
rotatedFilter = rotatedFilters(:,:,k, :);
rotatedFilter = reshape(rotatedFilter, size(rotatedFilter, 1), size(rotatedFilter, 2), size(rotatedFilter, 4));
sigma(:,:,k) = convolve(nextLayer.sigma, rotatedFilter);
% for nextFilterI = 1:nextLayer.numberOfFilters
% l = l + convolve(nextLayer.sigma(:,:, nextFilterI), rot90(nextLayer.filters(:, :, k, nextFilterI), 2)); %Vectorize ths
% end
sigma(:, :, k) = l;
end
end
layer.sigma = sigma;
layer.sigma = layer.sigma.*(layer.layerOutput~=0);
for k = 1:layer.numberOfFilters
for channel = 1:layer.prevDimension
alternateError = zeros(size(layer.filters, 1), size(layer.filters, 2));
for mt = 1 : size(layer.filters, 1)
for nt = 1:size(layer.filters, 2)
s = 0;
p1 = prevOutput(:, :, channel);
p2 = sigma(:, :, k);
for it = 1:size(p2,1)
for jt = 1:size(p2,2)
i1 = it + mt;
j1 = jt + nt;
if i1 <= size(p1, 1) && j1 <= size(p1, 2)
s = s + p1(i1, j1) * p2(it, jt);
end
end
end
alternateError(mt, nt) = s;
end
end
layer.error(:, :, channel, k) = alternateError;
end
layer.bias = layer.bias - alpha*sum(sum(layer.sigma(:,:,k)));
end
% layer.error = clipValue(layer.error, clipBy);
layer.filters = layer.filters - alpha*layer.error;
end
end
end
end