-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
165 lines (130 loc) · 4.4 KB
/
main.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
function main(input, output, verbose)
% Prevent calls without args
if ~exist('input', 'var') || ~exist('output', 'var')
return
end
% Log for analysis performance
if exist('verbose', 'var') && verbose == true
log();
end
[path, data, expecOutput, nh, nkFold] = readInput(input);
% First, try run aplication with loaded data
% The weights (A,B) will be loaded
try
newPath = path;
newExpecOutput = expecOutput;
load(data);
if ~strcmp(newPath, path) || ~strcmp(newExpecOutput, expecOutput)
error('Input file has changed.');
end
catch
% Construct X and expectedOutput (Yd) and your alias for report
[X, Yd, map] = expectedOutput(expecOutput, path);
end
% Run the principal loop
if exist('A', 'var') && exist('B', 'var')
[Y, A, B, Cvet, orderVet,vetErTrain,vetErVal,vetErTst] = runMLP(X, Yd, nh, nkFold, map, A, B);
else
[Y, A, B, Cvet, orderVet,vetErTrain,vetErVal,vetErTst] = runMLP(X, Yd, nh, nkFold, map);
end
saveOutput(output, Y, Cvet, orderVet, vetErTrain, vetErVal, vetErTst);
if exist('verbose', 'var') && verbose == true
log(true);
end
clear verbose input output nh nkFold newPath newExpecOutput;
save(data);
end
function [Y, A, B, cVet, orderVet,vetErTrain,vetErVal,vetErTst] = runMLP(X, Yd, nh, nkFold, map, ALoad, BLoad)
Y = cell(1, nkFold);
n = size(X,1);
Indices = 1:n;
grupoPorFold = floor(n/nkFold);
vetErTst = [];
vetErTrain = [];
vetErVal = [];
cVet = cell(1, nkFold);
orderVet = cell(1, nkFold);
i = 1;
while i <= nkFold
c = randperm(size(Indices,2), grupoPorFold);
[trainX, trainYd, testX, testYd] = kfold(X, Yd, Indices(c));
% Training and validation group
if exist('ALoad', 'var') && exist('BLoad', 'var')
[A,B,erroTest,erroVal] = mlp(trainX, trainYd, nh, grupoPorFold, ALoad, BLoad);
else
[A,B,erroTest,erroVal] = mlp(trainX, trainYd, nh, grupoPorFold);
end
vetErTrain = [vetErTrain;erroTest];
vetErVal = [vetErVal;erroVal];
[Ntr,~] = size(testX);
% Test group
[Yr,~] = feed_foward([ones(Ntr,1),testX], A, B);
erroTest = (Yr - testYd);
erroTest = sum(sum(erroTest.*erroTest))/Ntr;
vetErTst = [vetErTst;erroTest];
% Save mean output
Y{i} = acerto(Yr, testYd);
% Generate confusion matrix per fold
answerYd = mapping(testYd, map);
answerYr = mapping(mat2int(Yr), map);
[C, order] = confusionmat(answerYd, answerYr);
cVet{i} = C;
orderVet{i} = order;
% Reset for the next group
Indices(c) = [];
i = i+1;
end
Y = cell2mat(Y);
Y = mean(Y);
end
function [path, data, expectedOutput, nh, nkFold] = readInput(input)
fid = fopen(input, 'r');
tline = fgetl(fid);
while ischar(tline)
aux = strsplit(tline, " = ");
switch aux{1}
case 'path'
path = aux{2};
case 'data'
data = aux{2};
case 'expectedOutput'
expectedOutput = aux{2};
case 'nh'
nh = str2num(aux{2});
case 'nkFold'
nkFold = str2num(aux{2});
end
tline = fgetl(fid);
end
fclose(fid);
end
function saveOutput(output, Y, Cvet, orderVet, vetErTrain, vetErVal, vetErTst)
fid = fopen(output,'a');
fprintf(fid, strcat("Generated at\t", datestr(now, 'yyyy-mm-dd HH:MM:SS'), "\n\n"));
fprintf(fid,'Average percentage of correct answers: %g\t', Y);
fprintf(fid,'\n\n');
fclose(fid);
saveMSEperFold(output, vetErTrain, vetErVal, vetErTst);
saveCMperFold(output, Cvet, orderVet);
savePlot(output, vetErTrain, vetErVal, vetErTst);
end
function txAcerto = acerto(Yr,testYd)
Yr = round(Yr);
Total = size(Yr,1);
cont =0;
for i =1: Total
mi = testYd(i,:);
if mi == Yr(i,:)
cont = cont+1;
end
end
txAcerto = (cont*100)/Total;
end
function log(finished)
if ~exist('finished', 'var') || ~finished
tic();
disp('Running mlp...');
else
fprintf("Finished in %s\n", num2str(toc()));
end
end