1 package yawn.nn.appart;
2
3 import java.util.ArrayList;
4
5 import yawn.nn.Layer;
6 import yawn.util.Pattern;
7
8 /***
9 * A recognition (F2) layer for AppART neural networks.
10 *
11 * <p>$Id: RecognitionLayer.java,v 1.10 2005/05/09 11:04:55 supermarti Exp $</p>
12 *
13 * @author Luis Martí (luis dot marti at uc3m dot es)
14 * @version $Revision: 1.10 $
15 */
16
17 public class RecognitionLayer extends Layer {
18
19 /***
20 *
21 * @uml.property name="g"
22 * @uml.associationEnd multiplicity="(1 1)"
23 */
24 protected GainControlUnitOnMatching g;
25
26 /***
27 *
28 * @uml.property name="inputSize"
29 * @uml.associationEnd multiplicity="(0 -1)" elementType="yawn.nn.gasart.GasRecognitionLayer"
30 */
31 protected int inputSize;
32
33 /***
34 *
35 * @uml.property name="normalizedActivations"
36 * @uml.associationEnd multiplicity="(0 1)"
37 */
38 protected Pattern normalizedActivations;
39
40
41 protected boolean normDone;
42
43 public RecognitionLayer(Layer nextLayer, GainControlUnitOnMatching aGF2, int inputSize) {
44 super(nextLayer, inputSize);
45 g = aGF2;
46 this.inputSize = inputSize;
47 units = new ArrayList();
48 normalizedActivations = null;
49 normDone = false;
50 }
51
52 public void calculateNormalizedActivations() {
53
54 Pattern g = getActivations();
55 Pattern v = new Pattern(size());
56
57 double sum = 0;
58 for (int i = 0; i < size(); i++) {
59 sum += g.getComponent(i);
60 }
61
62 for (int i = 0; i < size(); i++) {
63 v.setComponent(g.getComponent(i) / sum, i);
64 }
65
66 normalizedActivations = v;
67 normDone = true;
68 }
69
70 /***
71 *
72 * @uml.property name="normalizedActivations"
73 */
74 public Pattern getNormalizedActivations() {
75 if (!normDone) {
76 calculateNormalizedActivations();
77 }
78 return normalizedActivations;
79 }
80
81 public void learn() {
82 if (isAdapting()) {
83 for (int i = 0; i < size(); i++) {
84 ((RadialBasisFunctionsNeuralNode) units.get(i)).learn(normalizedActivations
85 .getComponent(i));
86 }
87 }
88 }
89
90 public void makeNewCategory() {
91 units.add(new RadialBasisFunctionsNeuralNode(inputSize, g.getBaseVigilanceParameter(),
92 units.size() + 1));
93 }
94
95 public void matching() {
96 double oneOutput;
97
98
99
100 g.setVigilanceParameter(g.getVigilanceParameter());
101 g.setMatch(false);
102 for (int i = 0; i < size() && g.fires(); i++) {
103 oneOutput = ((RadialBasisFunctionsNeuralNode) units.get(i)).output();
104 if (oneOutput > 0) {
105 g.setMatch(true);
106 }
107 }
108 }
109
110 /***
111 * Propagates the <code>normalizedActivations</code> to a prediction
112 * layer.
113 */
114 public void propagateToNextLayer() {
115 nextLayer.setInput(getNormalizedActivations());
116 }
117
118 /***
119 * @see yawn.nn.Layer#reset()
120 */
121 public void reset() {
122
123 normDone = false;
124 super.reset();
125 }
126
127 public void setGF2(GainControlUnitOnMatching aGF2) {
128 g = aGF2;
129 }
130
131 /***
132 * @see yawn.nn.Layer#setInput(yawn.util.Pattern)
133 */
134 public void setInput(Pattern input) {
135
136 super.setInput(input);
137 normDone = false;
138 }
139
140 }