Source code: AI/NeuralNetworks/FeedForwardWeightedTrainingLayer.java
1 /* FeedForwardWeightedTrainingLayer.java */
2
3 package AI.NeuralNetworks;
4
5 import java.util.*;
6
7 /**
8 This class is used to represent the inner and output layers of a multilayer feed forward
9 neural network where the back error propabation algorithm is run
10 */
11 public class FeedForwardWeightedTrainingLayer extends FeedForwardWeightedLayer {
12 float[][] deltaWeights;
13 float[] error;
14 float[] outputError;
15
16 /**
17 Creates a new FeedForwardWeightedTrainingLayer instance form a FeedForwardWeightedLayer instance
18 */
19 public FeedForwardWeightedTrainingLayer (FeedForwardWeightedLayer layer){
20 super(layer);
21 error = new float[size()-1]; //the bias has no error
22 outputError = new float[size()-1]; //the bias has no error
23 deltaWeights = new float[weights.length][weights[0].length];
24 resetDeltaWeights();
25 }
26
27 /**
28 Sets the acumulated delta weights to 0
29 */
30 public void resetDeltaWeights(){
31 for(int row=0; row<deltaWeights.length; row++){
32 for(int col=0; col<deltaWeights[row].length; col++){
33 deltaWeights[row][col] = (float) 0.0;
34 }
35 }
36 }
37
38 /**
39 Calculates and adds the delta weights for this pattern
40 @param expectedOutput the output expected of course the bias is not included
41 */
42 public void adjustDeltaWeights(float[] expectedOutput, FeedForwardLayer previousLayer){
43 float[] previousActivation;
44
45 for(int pos=1; pos<activation.length; pos++){
46 outputError[pos-1] = (expectedOutput[pos-1]-activation[pos]);
47 error[pos-1]=outputError[pos-1]*activation[pos]*(1-activation[pos]);
48 }
49
50 previousActivation = previousLayer.getActivation();
51 for(int row=0; row<deltaWeights.length; row++){
52 for(int col=0; col<deltaWeights[row].length; col++){
53 deltaWeights[row][col] += error[row] * previousActivation[col];
54 }
55 }
56 }
57
58 /**
59 Calculates and adds the delta weights for this pattern
60 @param expectedOutput the output expected of course the bias is not included
61 */
62 public void adjustDeltaWeights(FeedForwardLayer previousLayer,
63 FeedForwardWeightedTrainingLayer nextLayer){
64 float[] previousActivation;
65 float[] nextError;
66
67 resetError();
68 nextError = nextLayer.getError();
69 for(int pos=0; pos<error.length; pos++){
70 for(int nextErr=0; nextErr<nextError.length; nextErr++){
71 error[pos]+=nextError[nextErr]*weights[nextErr][pos+1];
72 }
73 error[pos]= error[pos] * activation[pos+1]*(1-activation[pos+1]);
74 }
75
76 previousActivation = previousLayer.getActivation();
77 for(int row=0; row<deltaWeights.length; row++){
78 for(int col=0; col<deltaWeights[row].length; col++){
79 deltaWeights[row][col] += error[row] * previousActivation[col];
80 }
81 }
82 }
83
84 /**
85 Sets the error of all this layer to 0
86 */
87 public void resetError(){
88 float cero = (float) 0.0;
89
90 for(int pos=0; pos<error.length; pos++){
91 error[pos] = cero;
92 }
93 }
94
95 /**
96 returns the error of the layer
97 */
98 public float[] getError(){
99 return error;
100 }
101
102 public float[] getOutputError(){
103 return outputError;
104 }
105
106 /**
107 Adds the deltaWeights to the weights
108 @param learninRate is the constan by wich is multiplied the deltaWeight before it is added to the weights
109 */
110 public void addDeltaWeights(float learningRate){
111 /*debug*///AI.Test.TestBP.printMatrix(weights,"\t\tdeltaweights = ");
112
113 for(int row=0; row<deltaWeights.length; row++){
114 for(int col=0; col<deltaWeights[row].length; col++){
115 weights[row][col] += deltaWeights[row][col] * learningRate;
116 }
117 }
118 }
119
120
121 }