-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backpropagation.java
executable file
·130 lines (106 loc) · 3.47 KB
/
Backpropagation.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
123
124
125
126
127
128
129
130
public class Backpropagation implements LearnAlg {
protected double[] Err;
protected double[][] ErrSig;
protected double ErrSum;
protected double ErrWeightSum;
protected double TotalErr=999;
protected double learnrate;
protected int cycle;
protected int maxCycle;
protected double meancycle=999;
protected int Fails=999;
protected double[] Output;
protected boolean success;
protected double weightval;
protected Network net;
protected FM file;
protected volatile boolean running = true;
public Backpropagation(double learnrateinp, Network network, int maximalCycle,String filename){
net=network;
Err=new double[net.GetOutput().length];
ErrSig=new double[net.GetLayerCount()][net.GetMaxNeurons()];
cycle=0;
maxCycle=maximalCycle;
learnrate=learnrateinp;
ErrSum=0;
file = new FM(filename,net.GetLayer(0).GetNeurons().length+net.GetLayer(net.GetLayerCount()-1).GetNeurons().length);
net.InitTrainingData(file);
}
public void run(){
System.out.println("Running backprop");
//ATTENTION SET INPUT OUTPUT length here!
while ((Fails > 0 || meancycle > 1) && running) {
TotalErr = 0;
int lc = file.getdatacount();
Fails = lc;
int i;
for (i = 0; i < lc; i++) {
int ran = (int) Math.round(Math.random() * (lc - 1));
net.SetTrainingData(ran);
learn(ran);
if (cycle != maxCycle) {
Fails--;
}
meancycle += cycle;
TotalErr += ErrSum;
}
TotalErr = TotalErr / (i + 1);
meancycle = meancycle / (i+1);
System.out.println("Mean of Cycles: " + meancycle);
System.out.println("Fails: " + Fails);
System.out.println("Training sets: " + file.getdatacount());
System.out.println("Total Error: " + TotalErr);
}
System.out.println("Backpropagation learning exiting.");
}
public void learn(int num){
success=false;
cycle=0;
double[] TrainOut=net.GetTrainOut(num);
while(!success){
ErrSum=0;
cycle++;
net.SetInput(num);
net.propagate();
Output=net.GetOutput();
for(int i=0;i<Output.length;i++){
Err[i]=TrainOut[i]-Output[i];
ErrSig[net.GetLayerCount()-1][i]=Output[i]*(1-Output[i])*Err[i];
ErrSum+=Math.pow(Err[i], 2);
net.GetLayer(net.GetLayerCount()-1).SetBias(i,(net.GetLayer(net.GetLayerCount()-1).GetBias(i)+learnrate*ErrSig[net.GetLayerCount()-1][i]));
}
for(int i=net.GetLayerCount()-2;i>=0;i--){
for(int k=0;k<net.GetLayer(i).nNeurons();k++){
ErrWeightSum=0;
weightval=0;
for(int m=0;m<net.GetLayer(i+1).nNeurons();m++){
ErrWeightSum+=net.GetLayer(i).GetWeight(k, m)*ErrSig[i+1][m];
}
ErrSig[i][k]=net.GetLayer(i).GetNeuron(k)*(1-net.GetLayer(i).GetNeuron(k))*ErrWeightSum;
for(int m=0;m<net.GetLayer(i+1).nNeurons();m++){
weightval=net.GetLayer(i).GetWeight(k,m)+learnrate*net.GetLayer(i).GetNeuron(k)*ErrSig[i+1][m];
net.GetLayer(i).SetWeight(k, m, weightval);
}
net.GetLayer(i).SetBias(k,(net.GetLayer(i).GetBias(k)+learnrate*ErrSig[i][k]));
}
}
ErrSum=Math.pow(ErrSum,1/2);
if(cycle==maxCycle){
success=true;
}
if(CheckResult()){
success=true;
}
}
}
protected boolean CheckResult(){
for(int i=0;i<Output.length;i++){
if(ErrSum>0.001) return false;
}
return true;
}
public void terminate(){
System.out.println("terminating...");
running=false;
}
}