-
Notifications
You must be signed in to change notification settings - Fork 0
/
code3.m
158 lines (133 loc) · 6.05 KB
/
code3.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
clear all
close all
[cancerInputs, cancerTargets] = cancer_dataset;
x = cancerInputs;
t = cancerTargets;
% trainFcn = 'trainlm';
trainFcn = 'trainrp';
% trainFcn = 'trainrp';
% trainFcn = 'trainscg'; % Scaled conjugate gradient backpropagation.
nodes = [2 8 32];
epochs = [4 8 16 32 64];
% original test sets
avgs = zeros(length(epochs), length(nodes));
sigs = zeros(length(epochs), length(nodes));
% Recalculate training sets
trainNetwork = struct;
trainNetwork.avgs = zeros(length(epochs), length(nodes));
trainNetwork.sigs = zeros(length(epochs), length(nodes));
% trainNetwork.mses = zeros(length(epochs), length(nodes));
% Recaluculate test sets
testNetwork = struct;
testNetwork.avgs = zeros(length(epochs), length(nodes));
testNetwork.sigs = zeros(length(epochs), length(nodes));
% testNetwork.mses = zeros(length(epochs), length(nodes));
execution_times = zeros(length(epochs), length(nodes));
for kk = 1:1:length(epochs)
for k = 1:1:length(nodes)
hiddenLayerSize = nodes(k);
% hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize, trainFcn);
net.trainParam.epochs = epochs(kk);
net.input.processFcns = {'removeconstantrows','mapminmax'};
% Setup Division of Data for Training, Testing (No validation data)
net.divideFcn = 'dividerand'; % Divide data randomly
net.divideMode = 'sample'; % Divide up every sample
% train and test ratio 50% each
net.divideParam.trainRatio = 50/300;
net.divideParam.valRatio = 0/300;
net.divideParam.testRatio = 50/300;
net.performFcn = 'crossentropy'; % Cross-Entropy
net.plotFcns = {'plotperform','plottrainstate','ploterrhist', 'plotconfusion',
'plotroc'};
percentErrors = zeros(1, 30);
trainNetwork.errors = zeros(1, 30);
testNetwork.errors = zeros(1, 30);
% Train the Network
% 30 times
for i = 1:1:30
[net,tr] = train(net, x, t);
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y);
tind = vec2ind(t);
yind = vec2ind(y);
percentError = sum(tind ~= yind)/numel(tind);
% percentErrors(i) = percentError;
percentErrors(1, i) = percentError;
% training recalculate
trainNetwork.targets = t .* tr.trainMask{1};
trainNetwork.targets = trainNetwork.targets(~isnan(trainNetwork.targets))';
trainNetwork.targets = reshape(trainNetwork.targets, [2,
length(trainNetwork.targets)/2]);
trainNetwork.output = y.*tr.trainMask{1};
trainNetwork.output = trainNetwork.output(~isnan(trainNetwork.output))';
trainNetwork.output = reshape(trainNetwork.output, [2,
length(trainNetwork.output)/2]);
trainNetwork.tind = vec2ind(trainNetwork.targets);
trainNetwork.yind = vec2ind(trainNetwork.output);
trainNetwork.percentErrors = sum(trainNetwork.tind ~=
trainNetwork.yind)/numel(trainNetwork.tind);
trainNetwork.errors(1, i) = trainNetwork.percentErrors;
% test recalculate
testNetwork.targets = t .* tr.testMask{1};
testNetwork.targets = testNetwork.targets(~isnan(testNetwork.targets))';
testNetwork.targets = reshape(testNetwork.targets, [2,
length(testNetwork.targets)/2]);
testNetwork.output = y.*tr.testMask{1};
testNetwork.output = testNetwork.output(~isnan(testNetwork.output))';
testNetwork.output = reshape(testNetwork.output, [2,
length(testNetwork.output)/2]);
testNetwork.tind = vec2ind(testNetwork.targets);
testNetwork.yind = vec2ind(testNetwork.output);
testNetwork.percentErrors = sum(testNetwork.tind ~=
testNetwork.yind)/numel(testNetwork.tind);
testNetwork.errors(1, i) = testNetwork.percentErrors;
end
% avg : average / standard deviation
avgs(kk, k) = sum(percentErrors(1, :)) / 30;
sigs(kk, k) = sqrt(sum(((percentErrors - avgs(kk, k)).^2))/30);
trainNetwork.avgs(kk, k) = sum(trainNetwork.errors(1,:)) / 30;
trainNetwork.sigs(kk, k) = sqrt(sum(((trainNetwork.errors -
trainNetwork.avgs(kk, k)).^2)) / 30);
testNetwork.avgs(kk, k) = sum(testNetwork.errors(1,:)) / 30;
testNetwork.sigs(kk, k) = sqrt(sum(((testNetwork.errors - testNetwork.avgs(kk,
k)).^2)) / 30);
node_times(kk, k) = sum(tr.time);
end
end
figure(1)
plot(epochs, trainNetwork.avgs(:,1), 'r', epochs, trainNetwork.avgs(:, 2), 'b', epochs,
trainNetwork.avgs(:, 3), 'g');
legend('Performance of 2 Node', 'Performance of 8 node', 'Performance of 32 Node');
title('Error rates using training sets');
xlabel('Epochs');
ylabel('Mean Squared Error (MSE)');
figure(2)
plot(epochs, trainNetwork.sigs(:,1), 'r', epochs, trainNetwork.sigs(:, 2), 'b', epochs,
trainNetwork.sigs(:, 3), 'g');
legend('Performance of 2 Node', 'Performance of 8 node', 'Performance of 32 Node');
title('Standard deviation using training sets');
xlabel('Epochs');
ylabel('Standard deviation');
figure(3)
% subplot(2, 1, 1);
plot(epochs, testNetwork.avgs(:, 1), 'r', epochs, testNetwork.avgs(:, 2), 'b', epochs,
testNetwork.avgs(:, 3), 'g');
legend('Performance of 2 Node', 'Performance of 8 node', 'Performance of 32 Node');
title('Error rates using test sets');
xlabel('Epochs');
ylabel('Mean Squared Error (MSE)');
figure(4)
plot(epochs, testNetwork.sigs(:,1), 'r', epochs, testNetwork.sigs(:, 2), 'b', epochs,
testNetwork.sigs(:, 3), 'g');
legend('Performance of 2 Node', 'Performance of 8 node', 'Performance of 32 Node');
title('Standard deviation using test sets');
xlabel('Epochs');
ylabel('Standard deviation');
figure(5)
plot(epochs, node_times(:, 1), epochs, node_times(:, 2), epochs, node_times(:, 3));
legend('2 Node execution times', '8 Node execution times', '32 Node execution times');
xlabel('Epochs');
ylabel('Execution times');