View Javadoc

1   /*
2    * MultiLayerPerceptronConfig.java
3    * Part of the yawn project
4    * Created on 06-oct-2004 by marti.
5    *
6    */
7   package yawn.nn.mlp;
8   
9   import java.util.ArrayList;
10  import java.util.List;
11  
12  import yawn.config.NeuralNetworkConfig;
13  import yawn.config.ValidationException;
14  import yawn.util.MiscUtils;
15  
16  /***
17   * A configuration class for the MultiLayerPerceptron class.
18   * 
19   * <p>$Id: MultiLayerPerceptronConfig.java,v 1.8 2005/05/09 11:04:54 supermarti Exp $</p>
20   * 
21   * @author Luis Mart&iacute; (luis dot marti at uc3m dot es)
22   * @version $Revision: 1.8 $
23   * @see yawn.nn.mlp.MultiLayerPerceptron
24   */
25  public class MultiLayerPerceptronConfig extends NeuralNetworkConfig {
26  
27  	/***
28  	 * 
29  	 */
30  	protected List layerConfigs = new ArrayList();
31  
32  	/***
33  	 * 
34  	 */
35  	protected double learningRate;
36  
37  	/***
38  	 * 
39  	 */
40  	protected double momentumRate;
41  
42  	/***
43  	 * 
44  	 */
45  	protected long maxEpochs;
46  
47  	/***
48  	 * 
49  	 */
50  	protected double predictionError;
51  
52  
53      /***
54       * 
55       */
56      public MultiLayerPerceptronConfig() {
57          super();
58      }
59  
60      public void addHiddenLayerConfig(LayerElement le) {
61          layerConfigs.add(le);
62      }
63  
64  	/***
65  	 * @return Returns the layerElements.
66  	 */
67  	public List getLayerConfigs() {
68  		return layerConfigs;
69  	}
70  
71  	/***
72  	 * @param layers
73  	 *            The layers to set.
74  	 */
75  	public void setLayerConfigs(List layers) {
76  		this.layerConfigs = layers;
77  	}
78  
79  
80      /***
81       * @see yawn.config.NeuralNetworkConfig#internalValidate()
82       */
83      protected void internalValidate() throws ValidationException {
84          if (layerConfigs.size() == 0) {
85              throw new ValidationException("no layers defined");
86          }
87  
88          if (getLearningRate() < 0) {
89              throw new ValidationException("learning rate must be a positive number");
90          }
91  
92          if (getMaxEpochs() < 0) {
93              throw new ValidationException("max epochs must be a positive number");
94          }
95  
96          if (getPredictionError() <= 0) {
97              throw new ValidationException("prediction error must be greater than zero");
98          }
99  
100         for (int i = 0; i < layerConfigs.size(); i++) {
101             LayerElement lc = (LayerElement) layerConfigs.get(i);
102 
103             // right dimension?
104             if (lc.getSize() < 1) {
105                 throw new ValidationException(lc.toString());
106             }
107 
108             // can load the node class?
109             Class nodeClass = null;
110             try {
111                 nodeClass = Class.forName(lc.getNodesClassName());
112             } catch (ClassNotFoundException e) {
113                 throw new ValidationException(lc.toString());
114             }
115 
116             if (!MiscUtils.isSuperClass(PerceptronNode.class, nodeClass)) {
117                 throw new ValidationException(lc.toString());
118             }
119         }
120 
121     }
122 
123 	/***
124 	 * @return Returns the learningRate.
125 	 * 
126 	 * @uml.property name="learningRate"
127 	 */
128 	public double getLearningRate() {
129 		return this.learningRate;
130 	}
131 
132 	/***
133 	 * @param learningRate
134 	 *            The learningRate to set.
135 	 * 
136 	 * @uml.property name="learningRate"
137 	 */
138 	public void setLearningRate(double learningRate) {
139 		this.learningRate = learningRate;
140 	}
141 
142 	/***
143 	 * @return Returns the maxEpochs.
144 	 * 
145 	 * @uml.property name="maxEpochs"
146 	 */
147 	public long getMaxEpochs() {
148 		return this.maxEpochs;
149 	}
150 
151 	/***
152 	 * @param maxEpochs
153 	 *            The maxEpochs to set.
154 	 * 
155 	 * @uml.property name="maxEpochs"
156 	 */
157 	public void setMaxEpochs(long maxEpochs) {
158 		this.maxEpochs = maxEpochs;
159 	}
160 
161 	/***
162 	 * @return Returns the predictionError.
163 	 * 
164 	 * @uml.property name="predictionError"
165 	 */
166 	public double getPredictionError() {
167 		return this.predictionError;
168 	}
169 
170 	/***
171 	 * @param predictionError
172 	 *            The predictionError to set.
173 	 * 
174 	 * @uml.property name="predictionError"
175 	 */
176 	public void setPredictionError(double predictionError) {
177 		this.predictionError = predictionError;
178 	}
179 
180 
181     /***
182      * @return <code>yawn.nn.mlp.MultiLayerPerceptron.class</code> by default
183      * @see yawn.config.NeuralNetworkConfig#getBindedNetworkClass()
184      */
185     public Class getBindedNetworkClass() {
186         return MultiLayerPerceptron.class;
187     }
188 
189 	/***
190 	 * @return Returns the momentumRate.
191 	 * 
192 	 * @uml.property name="momentumRate"
193 	 */
194 	public double getMomentumRate() {
195 		return this.momentumRate;
196 	}
197 
198 	/***
199 	 * @param momentumRate
200 	 *            The momentumRate to set.
201 	 * 
202 	 * @uml.property name="momentumRate"
203 	 */
204 	public void setMomentumRate(double momentumRate) {
205 		this.momentumRate = momentumRate;
206 	}
207 
208 	/* (non-Javadoc)
209 	 * @see yawn.config.NeuralNetworkConfig#createSampleInstance()
210 	 */
211 	public NeuralNetworkConfig createSampleInstance() {
212 		MultiLayerPerceptronConfig conf = new MultiLayerPerceptronConfig();
213 
214         conf.setMaxEpochs(500);
215         conf.setLearningRate(0.2);
216         conf.setMomentumRate(0.01);
217         conf.setPredictionError(0.05);
218 
219         LayerElement hiddenLayer = new LayerElement();
220         hiddenLayer.setNodesClassName("yawn.nn.mlp.SigmoidNode");
221         hiddenLayer.setSize(20);
222 
223         LayerElement outputLayer = new LayerElement();
224         outputLayer.setNodesClassName("yawn.nn.mlp.LinearNode");
225         outputLayer.setSize(8);
226 
227         ArrayList layers = new ArrayList();
228         layers.add(hiddenLayer);
229         layers.add(outputLayer);
230 
231         conf.setLayerConfigs(layers);
232 
233         return conf;
234 	}
235 
236 }