-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackPropagation.java
109 lines (98 loc) · 3.73 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
/**
* Implementation of Back Propagation algorithm.
* @author berni
*
*/
public class BackPropagation {
static int Activation_TYPE = 1;
static int Derivative_TYPE = 2;
static double beta = 1;
static double learningFactor = 0.03; // 0.07
/**
* Select appropriate activation function.
* @param x input
* @return function value
*/
public static double activationFunction(final double x) {
return unipolarActivationFunction(x);
}
/**
* Select appropriate derivative function.
* @param x input
* @return function value
*/
public static double derivativeFunction(final double x) {
return unipolarDerivative(x);
}
public static double unipolarActivationFunction(final double x) {
double denominator = 1 + Math.exp(-1 * beta * x);
return 1 / denominator;
}
public static double bipolarActivationFunction(final double x){
return Math.tanh(beta * x);
}
public static double unipolarDerivative(final double x){
return beta * unipolarActivationFunction(x)
* (1 - unipolarActivationFunction(x));
}
public static double bipolarDerivative(final double x) {
return beta * (1 - Math.pow(bipolarActivationFunction(x), 2));
}
public static double targetFunction(){
return 0;
}
public static double newWeightHiddenLay(final double oldWeight,
final double prevLayComputedVal, final double thisLayDerivativeVal,
final Matrix thisLaySum4DerivativeVals, final Matrix nextLayWeights,
final Matrix computedOutputVal, final Matrix targetOutputVal) {
double newWeight = 0;
double grad = 0;
for (int i = 0; i < targetOutputVal.getHeigth(); i++) {
grad += (computedOutputVal.getValue(0, i) - targetOutputVal.getValue(0, i))
* derivativeFunction(thisLaySum4DerivativeVals.getValue(0, i))
* nextLayWeights.getValue(i + 1, 0) //+1 first is number one
* thisLayDerivativeVal * prevLayComputedVal;
}
newWeight = oldWeight - learningFactor * grad;
return newWeight;
}
public static void computeNewHiddenLayWeights(final Matrix thisLayWeights,
final Matrix outputComputedVals, final Matrix outputDestinationVals,
final Matrix nextLayNeuronSums, final Matrix thisLayNeuronSums,
final Matrix prevLayComputedVal, final Matrix nextLayWeights) {
double newval;
for (int i = 0; i < thisLayWeights.getHeigth(); i++) { // neurons
for (int a = 0; a < thisLayWeights.getWidth(); a++) { // weights
newval = newWeightHiddenLay(thisLayWeights.getValue(a, i),
prevLayComputedVal.getValue(0, a),
derivativeFunction(thisLayNeuronSums.getValue(0, i)),
nextLayNeuronSums, nextLayWeights,
outputComputedVals, outputDestinationVals);
thisLayWeights.setValue(a, i, newval);
}
}
}
public static double newWeightOutput(final double oldWeight,final double targetOutputVal,
final double computedOutputVal, final double sumVal,final double prevLayComputedVal) {
double newWeight = oldWeight - learningFactor
* (computedOutputVal - targetOutputVal)
* derivativeFunction(sumVal) * prevLayComputedVal;
return newWeight;
}
public static void computeNewOutputWeights(Matrix outputWeights,
Matrix outputVals,Matrix neuronResults,Matrix neuronSums, Matrix nuronPrevResults){
double newval ;
double neuronPrevRes = 0 ;
for (int i = 0; i < outputWeights.getHeigth(); i++) {
for (int a = 0; a < outputWeights.getWidth(); a++) {
if (a == 0) { neuronPrevRes = 1; } else {
neuronPrevRes = nuronPrevResults.getValue(0, a - 1);
}
newval = newWeightOutput(outputWeights.getValue(a, i),
outputVals.getValue(0, i), neuronResults.getValue(0, i),
neuronSums.getValue(0, i), neuronPrevRes);
outputWeights.setValue(a, i, newval);
}
}
}
}