Repository navigation
Expand file tree
/
Copy pathMainMLP.java
More file actions
188 lines (156 loc) · 4.58 KB
/
Copy pathMainMLP.java
File metadata and controls
188 lines (156 loc) · 4.58 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.util.Arrays;
import java.util.Random;
public class MainMLP {
public static void main(String[] args) {
Reader r = new Reader();
Amostra[] database = r.readDataSet();
int var = -2;
MLP rna = new MLP(database[0].input.length,
(int) ((database[0].input.length + database[0].output.length) / 2)+var,
database[0].output.length, 0.01);
randomizarBase(database);
for (int e = 0; e < 100000; e++) {
double erroApEpocaTreino = 0;
double erroClEpocaTreino = 0;
double erroApEpocaTeste = 0;
double erroClEpocaTeste = 0;
for (int a = 0; a < database.length; a++) {
double[] x = database[a].input;
double[] y = database[a].output;
/*
100 treino 43 teste cp || 54 treino 23 teste im || 35 treino 17 teste pp
*/
if(a > 100 && a < 143 || a > 197 && a < 220 || a > 255){
double[] out = rna.teste(x, y);
double erroApAmostra = 0;
for (int j = 0; j < out.length; j++) {
erroApAmostra += Math.abs(y[j] - out[j]);
}
double erroClAmostra = 0;
double[] ot = MainMLP.getOutThreshold(out);
double soma = 0;
for (int j = 0; j < out.length; j++) {
soma += Math.abs(y[j] - ot[j]);
}
if (soma > 0){
erroClAmostra = 1;
}
erroApEpocaTeste += erroApAmostra;
erroClEpocaTeste += erroClAmostra;
}else{
double[] out = rna.treinar(x, y);
double erroApAmostra = 0;
for (int j = 0; j < out.length; j++) {
erroApAmostra += Math.abs(y[j] - out[j]);
}
double erroClAmostra = 0;
double[] ot = MainMLP.getOutThreshold(out);
double soma = 0;
for (int j = 0; j < out.length; j++) {
soma += Math.abs(y[j] - ot[j]);
}
if (soma > 0){
erroClAmostra = 1;
}
erroApEpocaTreino += erroApAmostra;
erroClEpocaTreino += erroClAmostra;
}
}
System.out.printf("Epoca: %6d - erroApEpocaTreino: %.10f - erroClEpocaTreino: %d"+
" - erroApEpocaTeste: %.10f - erroClEpocaTeste: %d \n", e, erroApEpocaTreino,
(int) erroClEpocaTreino, erroApEpocaTeste, (int) erroClEpocaTeste);
}
}
private static void randomizarBase(Amostra[] baseTreino) {
Random random = new Random();
int[][] ordenacp = new int[143][2];
int[][] ordenaim = new int[77][2];
int[][] ordenapp = new int[52][2];
Amostra cp[] = Arrays.copyOfRange(baseTreino, 0, 143);
Amostra im[] = Arrays.copyOfRange(baseTreino, 143, 220);
Amostra pp[] = Arrays.copyOfRange(baseTreino, 220, 272);
for(int i=0; i<ordenacp.length; i++){
ordenacp[i][0] = i;
ordenacp[i][1] = random.nextInt(300);
}
for(int i=0; i<ordenaim.length; i++){
ordenaim[i][0] = i;
ordenaim[i][1] = random.nextInt(300);
}
for(int i=0; i<ordenapp.length; i++){
ordenapp[i][0] = i;
ordenapp[i][1] = random.nextInt(300);
}
for(int i=0; i<ordenacp.length; i++){
for(int j=i; j<ordenacp.length-1; j++){
if(ordenacp[j][1] <= ordenacp[j+1][1]){
int aux0 = ordenacp[j][0];
int aux1 = ordenacp[j][1];
ordenacp[j][0] = ordenacp[j+1][0];
ordenacp[j][1] = ordenacp[j+1][1];
ordenacp[j+1][0] = aux0;
ordenacp[j+1][1] = aux1;
}
}
}
for(int i=0; i<ordenaim.length; i++){
for(int j=i; j<ordenaim.length-1; j++){
if(ordenaim[j][1] <= ordenaim[j+1][1]){
int aux0 = ordenaim[j][0];
int aux1 = ordenaim[j][1];
ordenaim[j][0] = ordenaim[j+1][0];
ordenaim[j][1] = ordenaim[j+1][1];
ordenaim[j+1][0] = aux0;
ordenaim[j+1][1] = aux1;
}
}
}
for(int i=0; i<ordenapp.length; i++){
for(int j=i; j<ordenapp.length-1; j++){
if(ordenapp[j][1] <= ordenapp[j+1][1]){
int aux0 = ordenapp[j][0];
int aux1 = ordenapp[j][1];
ordenapp[j][0] = ordenapp[j+1][0];
ordenapp[j][1] = ordenapp[j+1][1];
ordenapp[j+1][0] = aux0;
ordenapp[j+1][1] = aux1;
}
}
}
for(int i=0; i<cp.length; i++){
Amostra aux = cp[i];
cp[i] = cp[ordenacp[i][0]];
cp[ordenacp[i][0]] = aux;
}
for(int i=0; i<im.length; i++){
Amostra aux = im[i];
im[i] = im[ordenaim[i][0]];
im[ordenaim[i][0]] = aux;
}
for(int i=0; i<pp.length; i++){
Amostra aux = pp[i];
pp[i] = pp[ordenapp[i][0]];
pp[ordenapp[i][0]] = aux;
}
for(int i=0; i<cp.length; i++){
baseTreino[i] = cp[i];
}
for(int i=0; i<im.length; i++){
baseTreino[i+143] = im[i];
}
for(int i=0; i<pp.length; i++){
baseTreino[i+220] = pp[i];
}
}
private static double[] getOutThreshold(double[] out) {
double[] ret = new double[out.length];
for (int i = 0; i < ret.length; i++) {
if (out[i] >= 0.5) {
ret[i] = 0.995;
} else {
ret[i] = 0.005;
}
}
return ret;
}
}