-
Notifications
You must be signed in to change notification settings - Fork 0
/
BP_ORL.m
348 lines (252 loc) · 9.48 KB
/
BP_ORL.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
340
341
342
343
344
345
346
347
348
function [Train_Percent, trainACCList, trainNonACCList, Test_Percent, testACCList, testNonACCList] = BP_ORL(prototypeFACE)
% version : 7
divideNum = 1;
% only need to train and test total 200 records
% prototypeFACE = prototypeFACE(1:length(prototypeFACE) / 2, :);
% size(prototypeFACE) = 400 20
prototypeFACE = prototypeFACE(1:length(prototypeFACE) / divideNum, :);
%最低分80分softmax就有90分以上
% value-min/max-min in all vlaue prototypeFACE (正規化)
minNum = inf; maxNum = -inf;
for i = 1:length(prototypeFACE)
for j = 1:length(prototypeFACE(i))
if minNum > prototypeFACE(i, j)
minNum = prototypeFACE(i, j);
end
if maxNum < prototypeFACE(i, j)
maxNum = prototypeFACE(i, j);
end
end
end
for i = 1:length(prototypeFACE)
for j = 1:length(prototypeFACE(i))
prototypeFACE(i, j) = (prototypeFACE(i, j) - minNum) / (maxNum - minNum);
end
end
input = prototypeFACE(1:2:end, :);
% size(input) = 10 20
targetNum = ceil(40 / divideNum); % tag num = 40 (個分類)
tmpNUM = 175; % node num 40 is enough
length_i = length(input(:, 1));
length_j = 20; % data 寬度 = ldanum
% epoch : acc = train, test with node num 40 and total data set
% 500 : acc = 67.5, 54.5
% 1000 : acc = 63.0, 53.0
% 1500 : acc = 65.0, 55.5
% 2000 : acc = 75.5, 64.0
% 2500 : acc = 74.5, 57.0
% 3000 : acc = 71.5, 65.0
% 3500 : acc = 73.0, 65.0
% 4000 : acc = 72.5, 55.0
% 4500 : acc = 65.2, 54.0
% 5000 : acc = 79.0, 65.5
% epoch : acc = train, test with node num 60 and total data set
% 2000 : acc = 78.0, 67.0
% epoch : acc = train, test with node num 100 and total data set
% 2000 : acc = 89.5, 79.0
% epoch : acc = train, test with node num 125 and total data set
% 2000 : acc = 90.5, 77.5
% epoch : acc = train, test with node num 150 and total data set
% 2000 : acc = 91.5, 80.5
% 3000 : acc = 88.5, 82.0
% 5000 : acc = 91.0, 82.5
% epoch : acc = train, test with node num 175 and total data set
% 2000 : acc = 91.0, 81.0
% 3000 : acc = 92.0, 84.0
%- 3000 : acc = 93.5, 81.5 with 2 *
%- 3000 : acc = 92.5, 84.5 with 4 *
%- 3000 : acc = 94.5, 85.5 with 5 *
%- 3000 : acc = 93.5, 83.5 with 6 *
%- 3000 : acc = 89.0, 77.0 with 8 *
% 5000 : acc = 91.0, 80.5
% epoch : acc = train, test with node num 150 and total data set
% 2000 : acc = 90.0, 77.5
epochMax = 3000; % 500 - 5000
learningRate = 0.65;
% tag of the dataset
target = [];
% 40 * 5 = 200
for i = 1:targetNum
for j = 1:5
temp = zeros(targetNum, 1);
temp(i) = 1;
target = cat(3, target, temp);
end
end
% size(target) = 40 1 200
% initialize the weight matrix
outputmatrix = [];
for k = 1:1:targetNum
tempM = zeros(tmpNUM, 1);
for i = 1:1:tmpNUM
for j = 1:1:1
tempM(i, j) = rand;
end
end
outputmatrix = cat(3, outputmatrix, tempM);
end
% size(outputmatrix) = 30 1 40
hiddenmatrix = zeros(length_j, tmpNUM);
for i = 1:1:length_j
for j = 1:1:tmpNUM
hiddenmatrix(i, j) = rand;
end
end
% size(hiddenmatrix) = 20 30
RMSE = zeros(epochMax, 1);
% Training
for epoch = 1:1:epochMax
t = [];
for iter = 1:length_i
% 前傳部分
% size(input(iter, :)) = 1 20
% size(hiddenmatrix) = 20 30
hiddensigma = input(iter, :) * hiddenmatrix;
hiddennet = logsig(hiddensigma);
% size(hiddensigma) = 1 30
outputsigma = [];
outputnet = [];
for i = 1:targetNum
tempNum1 = hiddennet * outputmatrix(:, :, i);
% size(tempNum1) = 1 1
outputsigma = [outputsigma; tempNum1];
% outputnet = [outputnet; purelin(tempNum1)];
outputnet = [outputnet; tansig(tempNum1)];
end
% size(outputsigma) = 40 1
% size(outputnet) = 40 1
% 倒傳部分
% 輸出層的 delta
% doutputnet = [];
deltaoutput = [];
tempError = 0; %error
for i = 1:targetNum
tempNum2 = dpurelin(outputsigma(i, :));
% tempNum1 = hiddennet * outputmatrix(:, :, i);
% tempNum2 = dtansig(tempNum1, tansig(tempNum1));
% doutputnet = [doutputnet; tempNum2];
temp_ = target(:, :, iter) - outputnet(i, :);
deltaoutput = [deltaoutput; temp_ * tempNum2];
tempError = tempError + temp_;
end
tempError = tempError / targetNum;
t = [t; tempError.^2];
% 隱藏層的 delta
index = ceil(iter / 5);
tempdelta = 5 * deltaoutput(index, :) * outputmatrix(:, :, index);
% tempdelta = zeros(tmpNUM, 1);
for i = 1:targetNum
% tempdelta = [tempdelta; deltaoutput(i, :) * outputmatrix(:, :, i)];
tempdelta = tempdelta + deltaoutput(i, :) * outputmatrix(:, :, i);
end
% tempdelta = tempdelta / targetNum;
% size(tempdelta) = 30 1
transfer = dlogsig(hiddensigma, logsig(hiddensigma));
% transfer = dtansig(hiddensigma, tansig(hiddensigma));
deltahidden = [];
for i = 1:1:tmpNUM
deltahidden = [deltahidden; tempdelta(i) * transfer(i)];
end
% 輸出層權重更新
for i = 1:targetNum
outputmatrix(:, :, i) = outputmatrix(:, :, i) + learningRate * (deltaoutput(i, :) * hiddennet)';
end
% 隱藏層權重更新
newhiddenmatrix = hiddenmatrix;
for i = 1:1:tmpNUM
for j = 1:1:length_j
newhiddenmatrix(j, i) = hiddenmatrix(j, i) + learningRate * deltahidden(i, :) * input(iter, j);
% maybe can change the weight of deltahidden
end
end
if learningRate > 0.01
learningRate = learningRate - 0.01;
end
hiddenmatrix = newhiddenmatrix;
end
RMSE(epoch) = sqrt(sum(t) / length_i);
fprintf('epoch %.0f: RMSE = %.3f\n', epoch, RMSE(epoch));
end
fprintf('\nTotal number of epochs: %g\n', epoch);
fprintf('Final RMSE: %g\n', RMSE(epoch));
plot(1:epoch, RMSE(1:epoch));
legend('Training');
ylabel('RMSE'); xlabel('Epoch');
% train
Train_Correct = 0;
trainACCList = [];
trainNonACCList = [];
for i = 1:length_i
hiddensigma = input(i, :) * hiddenmatrix;
hiddennet = logsig(hiddensigma);
outputsigma = [];
outputnet = [];
maxNum = -inf; maxNumIdx = 0;
for j = 1:targetNum
outputsigma = [outputsigma, hiddennet * outputmatrix(:, :, j)];
outputnet = [outputnet, purelin(outputsigma(j))];
end
for j = 1:targetNum
if maxNum < outputnet(j)
maxNum = outputnet(j);
maxNumIdx = j;
end
end
maxNum = -inf; correctIdx = 0;
for j = 1:targetNum
if maxNum < target(j, :, i)
maxNum = target(j, :, i);
correctIdx = j;
end
end
if maxNumIdx == correctIdx
Train_Correct = Train_Correct + 1;
ansList = [i, correctIdx];
trainACCList = [trainACCList; ansList];
else
ansList = [i, maxNumIdx, correctIdx];
trainNonACCList = [trainNonACCList; ansList];
end
end
Train_Percent = Train_Correct / length_i;
% test
input = prototypeFACE(2:2:end, :);
length_i = length(input(:, 1))
Test_Correct = 0;
testACCList = [];
testNonACCList = [];
for i = 1:length_i
hiddensigma = input(i, :) * hiddenmatrix;
hiddennet = logsig(hiddensigma);
outputsigma = [];
outputnet = [];
maxNum = -inf; maxNumIdx = 0;
for j = 1:targetNum
outputsigma = [outputsigma, hiddennet * outputmatrix(:, :, j)];
outputnet = [outputnet, purelin(outputsigma(j))];
end
for j = 1:targetNum
if maxNum < outputnet(j)
maxNum = outputnet(j);
maxNumIdx = j;
end
end
maxNum = -inf; correctIdx = 0;
for j = 1:targetNum
if maxNum < target(j, :, i)
maxNum = target(j, :, i);
correctIdx = j;
end
end
if maxNumIdx == correctIdx
Test_Correct = Test_Correct + 1;
ansList = [i, correctIdx];
testACCList = [testACCList; ansList];
else
ansList = [i, maxNumIdx, correctIdx];
testNonACCList = [testNonACCList; ansList];
end
end
Test_Percent = Test_Correct / length_i;
% acc > 0.9
end