|
15 | 15 | import aima.learning.learners.AdaBoostLearner; |
16 | 16 | import aima.learning.learners.DecisionListLearner; |
17 | 17 | import aima.learning.learners.DecisionTreeLearner; |
18 | | -import aima.learning.learners.NeuralNetLearner; |
19 | 18 | import aima.learning.learners.StumpLearner; |
20 | | -import aima.learning.statistics.FeedForwardNetwork; |
21 | | -import aima.learning.statistics.IrisDataSetNumerizer; |
22 | | -import aima.learning.statistics.PerceptronLearning; |
23 | | -import aima.learning.statistics.StandardBackPropogation; |
24 | | -import aima.probability.JavaRandomizer; |
| 19 | +import aima.learning.neural.BackPropLearning; |
| 20 | +import aima.learning.neural.FeedForwardNeuralNetwork; |
| 21 | +import aima.learning.neural.IrisDataSetNumerizer; |
| 22 | +import aima.learning.neural.NNConfig; |
| 23 | +import aima.learning.neural.NNDataSet; |
| 24 | +import aima.learning.neural.Numerizer; |
| 25 | +import aima.learning.neural.Perceptron; |
| 26 | +import aima.test.learningtest.neural.IrisNNDataSet; |
25 | 27 | import aima.util.Util; |
26 | 28 |
|
27 | 29 | public class LearningDemo { |
@@ -127,64 +129,66 @@ private static void ensembleLearningDemo() { |
127 | 129 | } |
128 | 130 |
|
129 | 131 | private static void perceptronDemo() { |
130 | | - System.out.println(Util.ntimes("*", 100)); |
131 | | - System.out.println("Perceptron Demo (Neural Net)"); |
132 | | - System.out.println(Util.ntimes("*", 100)); |
133 | | - System.out |
134 | | - .println("Trying to run Perception Learning on the Iris DataSet"); |
135 | | - System.out |
136 | | - .println("The Network weights and biases are set up at random .So you may get a different result n some runs"); |
137 | 132 | try { |
138 | | - DataSet ds = DataSetFactory.getIrisDataSet(); |
139 | | - FeedForwardNetwork network = new FeedForwardNetwork(4, 3, |
140 | | - new JavaRandomizer()); |
141 | | - NeuralNetLearner learner = new NeuralNetLearner(network, |
142 | | - new IrisDataSetNumerizer(), new PerceptronLearning(), 10); |
143 | | - learner.train(ds); |
144 | | - int[] result = learner.test(ds); |
145 | | - |
| 133 | + System.out.println(Util.ntimes("*", 100)); |
146 | 134 | System.out |
147 | | - .println("\nThis Perceptron classifies the data set with " |
148 | | - + result[0] + " successes" + " and " + result[1] |
149 | | - + " failures"); |
150 | | - System.out.println("\n"); |
151 | | - } catch (Exception e) { |
| 135 | + .println("\n Perceptron Demo - Running Perceptron on Iris data Set with 10 epochs of learning "); |
| 136 | + System.out.println(Util.ntimes("*", 100)); |
| 137 | + DataSet irisDataSet = DataSetFactory.getIrisDataSet(); |
| 138 | + Numerizer numerizer = new IrisDataSetNumerizer(); |
| 139 | + NNDataSet innds = new IrisNNDataSet(); |
| 140 | + |
| 141 | + innds.createExamplesFromDataSet(irisDataSet, numerizer); |
| 142 | + |
| 143 | + Perceptron perc = new Perceptron(3, 4); |
| 144 | + |
| 145 | + perc.trainOn(innds, 10); |
152 | 146 |
|
| 147 | + innds.refreshDataset(); |
| 148 | + int[] result = perc.testOnDataSet(innds); |
| 149 | + System.out.println(result[0] + " right, " + result[1] + " wrong"); |
| 150 | + } catch (Exception e) { |
| 151 | + // TODO Auto-generated catch block |
| 152 | + e.printStackTrace(); |
153 | 153 | } |
154 | 154 |
|
155 | 155 | } |
156 | 156 |
|
157 | 157 | private static void backPropogationDemo() { |
158 | | - System.out.println(Util.ntimes("*", 100)); |
159 | | - System.out.println("BackPropogation (Neural Net)"); |
160 | | - System.out.println(Util.ntimes("*", 100)); |
161 | | - System.out |
162 | | - .println("Trying to run BackPropogation Learning on the Iris DataSet"); |
163 | | - System.out |
164 | | - .println("The Network weights and biases are set up at random .So you may get a different result on some runs"); |
165 | 158 | try { |
166 | | - DataSet ds = DataSetFactory.getIrisDataSet(); |
167 | | - FeedForwardNetwork network = new FeedForwardNetwork(4, 4, 3, |
168 | | - new JavaRandomizer()); |
169 | | - NeuralNetLearner learner = new NeuralNetLearner(network, |
170 | | - new IrisDataSetNumerizer(), new StandardBackPropogation(), |
171 | | - 1000); |
172 | | - learner.train(ds); |
173 | | - int[] result = learner.test(ds); |
174 | | - |
| 159 | + System.out.println(Util.ntimes("*", 100)); |
175 | 160 | System.out |
176 | | - .println("\nThis BackPropogation Network classifies the data set with " |
177 | | - + result[0] |
178 | | - + " successes" |
179 | | - + " and " |
180 | | - + result[1] |
181 | | - + " failures"); |
182 | | - System.out.println("\n"); |
| 161 | + .println("\n BackpropagationDemo - Running BackProp on Iris data Set with 10 epochs of learning "); |
| 162 | + System.out.println(Util.ntimes("*", 100)); |
| 163 | + |
| 164 | + DataSet irisDataSet = DataSetFactory.getIrisDataSet(); |
| 165 | + Numerizer numerizer = new IrisDataSetNumerizer(); |
| 166 | + NNDataSet innds = new IrisNNDataSet(); |
| 167 | + |
| 168 | + innds.createExamplesFromDataSet(irisDataSet, numerizer); |
| 169 | + |
| 170 | + NNConfig config = new NNConfig(); |
| 171 | + config.setConfig(FeedForwardNeuralNetwork.NUMBER_OF_INPUTS, 4); |
| 172 | + config.setConfig(FeedForwardNeuralNetwork.NUMBER_OF_OUTPUTS, 3); |
| 173 | + config.setConfig(FeedForwardNeuralNetwork.NUMBER_OF_HIDDEN_NEURONS, |
| 174 | + 6); |
| 175 | + config |
| 176 | + .setConfig(FeedForwardNeuralNetwork.LOWER_LIMIT_WEIGHTS, |
| 177 | + -2.0); |
| 178 | + config.setConfig(FeedForwardNeuralNetwork.UPPER_LIMIT_WEIGHTS, 2.0); |
| 179 | + |
| 180 | + FeedForwardNeuralNetwork ffnn = new FeedForwardNeuralNetwork(config); |
| 181 | + ffnn.setTrainingScheme(new BackPropLearning(0.1, 0.9)); |
| 182 | + |
| 183 | + ffnn.trainOn(innds, 10); |
| 184 | + |
| 185 | + innds.refreshDataset(); |
| 186 | + int[] result = ffnn.testOnDataSet(innds); |
| 187 | + System.out.println(result[0] + " right, " + result[1] + " wrong"); |
183 | 188 | } catch (Exception e) { |
184 | | - System.out.println("exception"); |
| 189 | + // TODO Auto-generated catch block |
185 | 190 | e.printStackTrace(); |
186 | 191 | } |
187 | | - |
188 | 192 | } |
189 | 193 |
|
190 | 194 | } |
0 commit comments