View Javadoc

1   package yawn.nn.appart;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   
6   import yawn.nn.NeuralNode;
7   import yawn.nn.OutputLayer;
8   import yawn.util.Pattern;
9   
10  /***
11   * A prediction (P) layer for AppART neural networks.
12   * 
13   * <p>$Id: PredictionLayer.java,v 1.10 2005/05/09 11:04:55 supermarti Exp $</p>
14   * 
15   * @author Luis Mart&iacute; (luis dot marti at uc3m dot es)
16   * @version $Revision: 1.10 $
17   */
18  public class PredictionLayer extends yawn.nn.Layer {
19  
20  	/***
21  	 * 
22  	 * @uml.property name="a"
23  	 * @uml.associationEnd multiplicity="(0 -1)" elementType="yawn.nn.appart.PredictionNeuralNode"
24  	 */
25  	protected ArrayList a;
26  
27  	/***
28  	 * 
29  	 * @uml.property name="b"
30  	 * @uml.associationEnd multiplicity="(1 1)"
31  	 */
32  	protected PredictionNeuralNode b;
33  
34  
35      /***
36       * 
37       * @param size
38       *            number of nodes in layer
39       * @param e
40       *            eta
41       * @param l
42       *            associated output layer
43       */
44      public PredictionLayer(int size, double e, OutputLayer l) {
45          super();
46          nextLayer = l;
47          a = new ArrayList();
48  
49          for (int i = 0; i < size; i++)
50              a.add(new PredictionNeuralNode(0, e));
51  
52          b = new PredictionNeuralNode(0, e);
53      }
54  
55      public PredictionNeuralNode[] getAUnits() {
56          return (PredictionNeuralNode[]) a.toArray(new PredictionNeuralNode[1]);
57      }
58  
59  	/***
60  	 * 
61  	 * @uml.property name="b"
62  	 */
63  	public PredictionNeuralNode getBUnit() {
64  		return b;
65  	}
66  
67      public NeuralNode[] getNodes() {
68          throw new RuntimeException(
69                  "This method lost its meaning in this class (you can say its deprecated if you prefer).");
70      }
71  
72      public void learn(Pattern y) {
73          int m = a.size();
74          if (!isAdapting())
75              return;
76  
77          for (int i = 0; i <= m; i++)
78              if (i == m)
79                  b.learn(1);
80              // beta(t+1)=beta(t)+e*V_j*1
81              else
82                  ((PredictionNeuralNode) a.get(i)).learn(y.getComponent(i));
83          // alfa(t+1)=alfa(t)+e*V_j*Y_k
84      }
85  
86      public void propagateToNextLayer() {
87          PredictionNeuralNode[] aNodes = getAUnits();
88          Pattern o = new Pattern(aNodes.length);
89  
90          for (int i = 0; i < aNodes.length; i++) {
91              o.setComponent(aNodes[i].output() / getBUnit().output(), i);
92          }
93          nextLayer.setInput(o);
94      }
95  
96      public void reset() {
97          for (Iterator i = a.iterator(); i.hasNext();)
98              ((PredictionNeuralNode) i.next()).reset();
99          b.reset();
100     }
101 
102     public void setInput(Pattern input) {
103         PredictionNeuralNode[] aNodes = getAUnits();
104         for (int i = 0; i < aNodes.length; i++) {
105             aNodes[i].setInput(input);
106         }
107         getBUnit().setInput(input);
108     }
109 
110     public void setLearningRate(double lcte) {
111         int large = a.size();
112         for (int i = 0; i < large; i++)
113             ((PredictionNeuralNode) a.get(i)).e = lcte;
114         b.e = lcte;
115     }
116 
117     public int size() {
118         return a.size();
119     }
120 
121     public void updateWeightsStructure(Pattern y) {
122         for (int i = 0; i < size(); i++) {
123             // ((PredictionNeuralNode) a.elementAt(i)).lastIn.addElement(new
124             // Double(0.0));
125             if (y == null) {
126                 ((PredictionNeuralNode) a.get(i)).updateStructure(0, 0);
127             } else {
128                 ((PredictionNeuralNode) a.get(i)).updateStructure(0, y.getComponent(i));
129             }
130         }
131         if (y == null)
132             b.updateStructure(0, 0);
133         else
134             b.updateStructure(0, 1);
135     }
136 }