-
Notifications
You must be signed in to change notification settings - Fork 1
/
nn_construction.m
104 lines (76 loc) · 3.09 KB
/
nn_construction.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
function [rede,error] = nn_construction(IN,OUT,NEURONS,TRP,VP,TTP,iterations)
% INPUTS:
% IN: amostras de treinamento
% OUT: verdade de campo
% NEURONS: numero de neuros na camada oculta da rede
% TRP: porcentagem das amostras que devem ser utilizadas para treinamento
% VP: porcentagem das amostras que devem ser utilizadas para validacao
% TTP: porcentagem das amostras que devem ser utilizadas para teste
% iterations: numero de iterações que o sistema deve fazer
% OUTPUTS:
% rede: rede treinada
% error: erro (%E) da rede treinada
error = 999999999;
error_old = 999999999;
net_old = network;
IN = double(IN);
OUT = double(OUT);
inputs = IN;
targets = OUT;
count = 0;
while count < iterations
% Create a Pattern Recognition Network
hiddenLayerSize = NEURONS;
net = patternnet(hiddenLayerSize);
% Choose Input and Output Pre/Post-Processing Functions
% For a list of all processing functions type: help nnprocess
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
net.outputs{2}.processFcns = {'removeconstantrows','mapminmax'};
% Setup Division of Data for Training, Validation, Testing
% For a list of all data division functions type: help nndivide
net.divideFcn = 'dividerand'; % Divide data randomly
net.divideMode = 'sample'; % Divide up every sample
net.divideParam.trainRatio = TRP/100;
net.divideParam.valRatio = VP/100;
net.divideParam.testRatio = TTP/100;
% For help on training function 'trainlm' type: help trainlm
% For a list of all training functions type: help nntrain
net.trainFcn = 'trainscg'; %'trainlm'; % Levenberg-Marquardt
% Train the Network
[net,tr] = train(net,inputs,targets);
outputs = net(inputs);
% Recalculate Training, Validation and Test Performance
trainTargets = targets .* tr.trainMask{1};
valTargets = targets .* tr.valMask{1};
testTargets = targets .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,outputs);
valPerformance = perform(net,valTargets,outputs);
testPerformance = perform(net,testTargets,outputs);
C = [trainPerformance valPerformance testPerformance];
error = max(C);
C = [error error_old]
[min_error,I] = min(C);
if I == 1
net_old = net;
error_old = error;
end;
if I == 2
net_old = net_old;
error_old = error_old;
end;
count = count + 1
C = [];
end;
if count == iterations
C = [error error_old];
[min_error,I] = min(C);
if I == 1
rede = net;
error = error;
end;
if I == 2
rede = net_old;
error = error_old;
end;
end;
end