-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLP.java
122 lines (99 loc) · 2.99 KB
/
MLP.java
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
public class MLP {
private double [][] wh;
private double [][] wo;
private int qtdIn, qtdH, qtdOut;
private double ni;
public MLP(int qtdIn, int qtdH, int qtdOut, double ni) {
this.qtdIn = qtdIn;
this.qtdH = qtdH;
this.qtdOut = qtdOut;
this.ni = ni;
this.wh = new double [this.qtdIn+1][this.qtdH];
this.wo = new double [this.qtdH+1][this.qtdOut];
setPesosAleatorios(this.wh);
setPesosAleatorios(this.wo);
}
private void setPesosAleatorios(double [][] w) {
for (int i = 0; i < w.length; i++) {
for (int j = 0; j < w[0].length; j++) {
w[i][j] = ((Math.random() * 0.3) - 0.3);
}
}
}
public double[] treinar(double [] xIn, double [] y) {
//Acrescentar o bias
double [] x = new double[xIn.length + 1];
for (int i = 0; i < xIn.length; i++) {
x[i] = xIn[i];
}
x[x.length-1] = 1;
//Calcular a saida da camada H
double [] H = new double[this.qtdH+1];
for (int j = 0; j < H.length-1; j++) {
double soma = 0;
for (int i = 0; i < x.length; i++) {
soma = soma + wh[i][j] * x[i];
}
H[j] = 1 / (1 + Math.exp(-soma));
}
H[H.length-1] = 1;
//Calcular a saida da camada out
double [] out = new double[this.qtdOut];
for (int j = 0; j < out.length; j++) {
double soma = 0;
for (int i = 0; i < H.length; i++) {
soma = soma + wo[i][j] * H[i];
}
out[j] = 1 / (1 + Math.exp(-soma));
}
//Calcular os deltas da camada out
double [] Do = new double[this.qtdOut];
for (int j = 0; j < Do.length; j++) {
//int sinal=1;
//if((y[j]-out[j])<0) sinal = -1;
//Do[j] = out[j] * (1 - out[j]) * (y[j] - out[j])*sinal;
Do[j] = out[j]*(1-out[j])*(y[j]-out[j]);
}
double [] Dh = new double[this.qtdH];
for (int i = 0; i < Dh.length; i++) {
double soma = 0;
for (int j = 0; j < Do.length; j++) {
soma = soma + Do[j] * wo[i][j];
}
Dh[i] = H[i] * (1 - H[i]) * soma;
}
for (int i = 0; i < H.length; i++)
for (int j = 0; j < out.length; j++)
wo[i][j] = wo[i][j] + ni * Do[j] * H[i];
for (int i = 0; i < x.length; i++)
for (int j = 0; j < H.length-1; j++)
wh[i][j] = wh[i][j] + ni * Dh[j] * x[i];
return out;
}
public double[] teste(double[] xIn, double[] y){
//Faz acrescimo do bias
double[] x = new double[xIn.length+1];
for (int i = 0; i < xIn.length; i++) {
x[i] = xIn[i];
}
x[x.length-1] = 1;
//Calcula a saída da camada h
double[] h = new double[this.qtdH+1];
for(int i=0; i<h.length-1; i++){
for (int j = 0; j < x.length; j++) {
h[i] += x[j] *wh[j][i];
}
h[i] = 1/(1+ Math.exp(-h[i]));
}
h[this.qtdH] = 1;
//Calcula a saída da camada out
double[] out = new double[this.qtdOut];
for (int i = 0; i < out.length; i++) {
for (int j = 0; j < h.length; j++) {
out[i] += h[j]*wo[j][i];
}
out[i] = 1/(1+ Math.exp(-out[i]));
}
return out;
}
}