-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestNNs.m
162 lines (124 loc) · 4.51 KB
/
testNNs.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
function [largeF1Folds] = testNNs(networks)
% Load the data in for testing
[x,y] = loaddata('noisydata_students.txt');
[x,y] = ANNdata(x,y);
% Cross validate the larger NN
[net] = networks{1};
% Set up variables to store the statistics for the larger NN
stats = cell(0);
largeNNCM = zeros(6,6);
largeRecall = zeros(1,6);
largePrecision = zeros(1,6);
largeF1Folds = zeros(10, 6);
foldIndices = cell(1,10);
totalNumber = size(x,2);
for i = 1:10
foldIndices{i} = [round(((i-1)*totalNumber/10)+1):round(i*totalNumber/10)];
end
for i = 1:10
trainExamples = x(:, [foldIndices{1:(i-1)}, foldIndices{i+1:end}]);
trainTargets = y(:, [foldIndices{1:(i-1)}, foldIndices{i+1:end}]);
testExamples = x(:, [foldIndices{i}], :);
testTargets = y(:, [foldIndices{i}]);
% Configure and train
[thisNetwork] = configure(net, trainExamples, trainTargets);
[thisNetwork] = train(thisNetwork, trainExamples, trainTargets);
% Simulate using the test data
[out] = sim(thisNetwork, testExamples);
% Transform the output
[out] = NNout2labels(findMax(out));
[t] = NNout2labels(testTargets);
% Build the confusion matrix
largeCM = buildCM(out, t);
[recallPerFold, precPerFold] = recall_precision(largeCM);
largeF1Folds(i, :) = f1measure(recallPerFold, precPerFold);
% Add the recall and precision for averaging later
largeNNCM = largeNNCM + largeCM;
end
[largeRecall, largePrecision] = recall_precision(largeNNCM);
largeNNCM
largeF1 = f1measure(largeRecall, largePrecision)
% Store the statistics to the stats variable for saving to file
stats{1} = largeNNCM; stats{2} = largeRecall; stats{3} = largePrecision; stats{4} = largeF1;
% Set up variables to store the statistics for the smaller NNs
smallNNsCM = zeros(6,6);
smallRecall = zeros(1,6);
smallPrecision = zeros(1,6);
foldIndices = cell(1,10);
totalNumber = size(x,2);
for i = 1:10
foldIndices{i} = [round(((i-1)*totalNumber/10)+1):round(i*totalNumber/10)];
end
for i = 1:10
trainExamples = x(:, [foldIndices{1:(i-1)}, foldIndices{i+1:end}]);
trainTargets = y(:, [foldIndices{1:(i-1)}, foldIndices{i+1:end}]);
testExamples = x(:, [foldIndices{i}], :);
testTargets = y(:, [foldIndices{i}]);
% Initialise a matrix to store the output of each NN
thisFold = zeros(6, length(testTargets));
% Configure and train the networks for this fold
for i = 1:6
% Strip out the expected values for this NN
[thisNetworkTrainTargets] = trainTargets(i,:);
[thisNetwork] = networks{i+1};
[thisNetwork] = configure(thisNetwork, trainExamples, thisNetworkTrainTargets);
[thisNetwork] = train(thisNetwork, trainExamples, thisNetworkTrainTargets);
[out] = sim(thisNetwork, testExamples);
% Transform the data
thisFold(i,:) = out > 0.5;
end
% Ensure only one emotion is selected for each example
thisFold = getOneEmotion(thisFold);
[thisFoldEmotions] = NNout2labels(thisFold);
[t] = NNout2labels(testTargets);
% Build the confusion matrix
smallCM = buildCM(thisFoldEmotions, t);
% Add the recall and precision for averaging later
smallNNsCM = smallNNsCM + smallCM;
end
[smallRecall, smallPrecision] = recall_precision(smallNNsCM);
smallF1 = f1measure(smallRecall, smallPrecision)
% Store the statistics to the stats variable for saving to file
stats{5} = smallNNsCM; stats{6} = smallRecall; stats{7} = smallPrecision; stats{8} = smallF1;
end
% Build a confusion matrix given the predictions and correct classifications
function [CM] = buildCM(predictions, testTargets)
CM = zeros(6,6);
for i = 1:length(predictions)
CM(predictions(i), testTargets(i)) = CM(predictions(i), testTargets(i)) + 1;
end
end
% Ensure only one emotion for eache example
function [fold] = getOneEmotion(foldData)
[m,n] = size(foldData);
sums = sum(foldData);
for i = 1:n
if sums(i) > 1
ones = find(foldData(:,i) == 1);
ind = ones(randi(1,length(ones)));
f = zeros(6,1);
f(ind) = 1;
foldData(:,i) = f;
elseif sums(i) == 0
foldData(randi([1,6]),i) = 1;
end
end
fold = foldData;
end
% Find the most likely classification for the 6-output NN
function[foldMaxes] = findMax(fold)
[m,n] = size(fold);
foldMaxes = zeros(m, n);
for i = 1:n
thisColumn = fold(:, i);
maxVal = thisColumn(1);
maxInd = 1;
for j = 2:m
if(thisColumn(j) > maxVal)
maxVal = thisColumn(j);
maxInd = j;
end
end
foldMaxes(maxInd, i) = 1;
end
end