-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestNNsNoisy.m
143 lines (102 loc) · 3.11 KB
/
testNNsNoisy.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
function [] = testNNsNoisy(networks)
% Load the data in for testing
[cleanX,cleanY] = loaddata('cleandata_students.txt');
[noisyX,noisyY] = loaddata('noisydata_students.txt');
[cleanX,cleanY] = ANNdata(cleanX,cleanY);
[noisyX,noisyY] = ANNdata(noisyX,noisyY);
[net] = networks{1};
% Set up variables to store the statistics for the larger NN
stats = cell(0);
largeCM = zeros(6,6);
% Configure and train the larger NN
[thisNetwork] = configure(net, cleanX, cleanY);
[thisNetwork] = train(thisNetwork, cleanX, cleanY);
% Simulate using the noisy data
[out] = sim(thisNetwork, noisyX);
% Transform the output
[out] = NNout2labels(findMax(out));
[t] = NNout2labels(noisyY);
% Build the confusion matrix
largeCM = buildCM(out, t)
% Calculate the recall and precision
[largeRecall, largePrecision] = recall_precision(largeCM);
largeRecall
largePrecision
largeF1 = f1measure(largeRecall, largePrecision)
largeF1avg = sum(largeF1)/6
% SMALL NNS
% Initialise a matrix to store the output of each NN
pred = zeros(6, length(noisyY));
% Configure and train the networks for this fold
for i = 1:6
% Strip out the expected values for this NN
[thisNetworkTrainTargets] = cleanY(i,:);
[thisNetwork] = networks{i+1};
[thisNetwork] = configure(thisNetwork, cleanX, thisNetworkTrainTargets);
[thisNetwork] = train(thisNetwork, cleanX, thisNetworkTrainTargets);
[out] = sim(thisNetwork, noisyX);
% Transform the data
pred(i,:) = out > 0.5;
end
% Ensure only one emotion is selected for each example
pred = getOneEmotion(pred);
[thisFoldEmotions] = NNout2labels(pred);
[t] = NNout2labels(noisyY);
% Build the confusion matrix
smallCM = buildCM(thisFoldEmotions, t)
[smallRecall, smallPrecision] = recall_precision(smallCM);
smallRecall
smallPrecision
smallF1 = f1measure(smallRecall, smallPrecision)
smallF1avg = sum(smallF1)/6
stats{1} = largeCM;
stats{2} = largeRecall;
stats{3} = largePrecision;
stats{4} = largeF1;
stats{5} = smallCM;
stats{6} = smallRecall;
stats{7} = smallPrecision;
stats{8} = smallF1;
save('tests/NNstatsNoisy.mat', 'stats');
end
% Build a confusion matrix given the predictions and correct classifications
function [CM] = buildCM(predictions, noisyY)
CM = zeros(6,6);
for i = 1:length(predictions)
CM(predictions(i), noisyY(i)) = CM(predictions(i), noisyY(i)) + 1;
end
end
% Ensure only one emotion for each 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