Author: tommaso
Date: Mon Nov 19 09:05:33 2012
New Revision: 1411106

URL: http://svn.apache.org/viewvc?rev=1411106&view=rev
Log:
refactored NNFactoryTest, added test for BasicPerceptron

Added:
    labs/yay/trunk/core/src/test/java/org/apache/yay/BasicPerceptronTest.java   
(with props)
Modified:
    labs/yay/trunk/core/src/main/java/org/apache/yay/BasicPerceptron.java
    
labs/yay/trunk/core/src/test/java/org/apache/yay/NeuralNetworkFactoryTest.java

Modified: labs/yay/trunk/core/src/main/java/org/apache/yay/BasicPerceptron.java
URL: 
http://svn.apache.org/viewvc/labs/yay/trunk/core/src/main/java/org/apache/yay/BasicPerceptron.java?rev=1411106&r1=1411105&r2=1411106&view=diff
==============================================================================
--- labs/yay/trunk/core/src/main/java/org/apache/yay/BasicPerceptron.java 
(original)
+++ labs/yay/trunk/core/src/main/java/org/apache/yay/BasicPerceptron.java Mon 
Nov 19 09:05:33 2012
@@ -38,7 +38,7 @@ public class BasicPerceptron implements 
      *
      * @param inputWeights the array of starting weights for the perceptron
      */
-    public BasicPerceptron(double[] inputWeights) {
+    public BasicPerceptron(double... inputWeights) {
         this.perceptronNeuron = new BinaryThresholdNeuron(0, inputWeights);
         this.currentWeights = inputWeights;
     }

Added: labs/yay/trunk/core/src/test/java/org/apache/yay/BasicPerceptronTest.java
URL: 
http://svn.apache.org/viewvc/labs/yay/trunk/core/src/test/java/org/apache/yay/BasicPerceptronTest.java?rev=1411106&view=auto
==============================================================================
--- labs/yay/trunk/core/src/test/java/org/apache/yay/BasicPerceptronTest.java 
(added)
+++ labs/yay/trunk/core/src/test/java/org/apache/yay/BasicPerceptronTest.java 
Mon Nov 19 09:05:33 2012
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.yay;
+
+import java.util.Vector;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Testcase for {@link BasicPerceptron}
+ */
+public class BasicPerceptronTest {
+
+    @Test
+    public void testLearnPhase() throws Exception {
+        BasicPerceptron basicPerceptron = new BasicPerceptron(1, 2, 3, 4);
+        basicPerceptron.learn(createTrainingExample(1d, 4d, 5d, 6d),
+                createTrainingExample(1d, 5d, 6d, 0.5d),
+                createTrainingExample(0.1d, 9d, 4d, 1.9d),
+                createTrainingExample(0.11d, 4d, 2.6d, 9.5d));
+    }
+
+    @Test
+    public void testPredictionPhase() throws Exception {
+        BasicPerceptron basicPerceptron = new BasicPerceptron(1, 2, 3, 4);
+        basicPerceptron.learn(createTrainingExample(1d, 4d, 5d, 6d),
+                createTrainingExample(1d, 5d, 6d, 0.5d),
+                createTrainingExample(0.1d, 9d, 4d, 1.9d),
+                createTrainingExample(0.11d, 4d, 2.6d, 9.5d));
+        Double output = basicPerceptron.predict(createTrainingExample(0d, 1d, 
6d, 0.4d));
+        assertEquals(Double.valueOf(1d), output);
+    }
+
+    private TrainingExample<Double, Double> createTrainingExample(final Double 
output, final Double... params) {
+        return new TrainingExample<Double, Double>() {
+            @Override
+            public Vector<Feature<Double>> getFeatureVector() {
+                Vector<Feature<Double>> features = new 
Vector<Feature<Double>>();
+                Feature<Double> byasFeature = new Feature<Double>();
+                byasFeature.setValue(1d);
+                features.add(byasFeature);
+                for (Double d : params) {
+                    Feature<Double> feature = new Feature<Double>();
+                    feature.setValue(d);
+                    features.add(feature);
+                }
+                return features;
+            }
+
+            @Override
+            public Double getOutput() {
+                return output;
+            }
+        };
+    }
+}

Propchange: 
labs/yay/trunk/core/src/test/java/org/apache/yay/BasicPerceptronTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
labs/yay/trunk/core/src/test/java/org/apache/yay/NeuralNetworkFactoryTest.java
URL: 
http://svn.apache.org/viewvc/labs/yay/trunk/core/src/test/java/org/apache/yay/NeuralNetworkFactoryTest.java?rev=1411106&r1=1411105&r2=1411106&view=diff
==============================================================================
--- 
labs/yay/trunk/core/src/test/java/org/apache/yay/NeuralNetworkFactoryTest.java 
(original)
+++ 
labs/yay/trunk/core/src/test/java/org/apache/yay/NeuralNetworkFactoryTest.java 
Mon Nov 19 09:05:33 2012
@@ -35,7 +35,7 @@ public class NeuralNetworkFactoryTest {
     double[][] weights = {{-30d, 20d, 20d}};
     WeightsMatrix singleAndLayerWeights = new WeightsMatrix(weights);
     WeightsMatrix[] andWeightsMatrixSet = new 
WeightsMatrix[]{singleAndLayerWeights};
-    NeuralNetwork<Double,Double> andNN = NeuralNetworkFactory.create(new 
LinkedList<TrainingExample<Double, Double>>(), andWeightsMatrixSet, new 
VoidLearningStrategy<Double, Double>(), new FeedForwardStrategy(new 
SigmoidFunction()));
+    NeuralNetwork<Double,Double> andNN = createFFNN(andWeightsMatrixSet);
     assertEquals(0l, Math.round(andNN.predict(createSample(1d, 0d))));
     assertEquals(0l, Math.round(andNN.predict(createSample(0d, 1d))));
     assertEquals(0l, Math.round(andNN.predict(createSample(0d, 0d))));
@@ -47,7 +47,7 @@ public class NeuralNetworkFactoryTest {
     double[][] weights = {{-10d, 20d, 20d}};
     WeightsMatrix singleOrLayerWeights = new WeightsMatrix(weights);
     WeightsMatrix[] orWeightsMatrixSet = new 
WeightsMatrix[]{singleOrLayerWeights};
-    NeuralNetwork<Double,Double> orNN = NeuralNetworkFactory.create(new 
LinkedList<TrainingExample<Double, Double>>(), orWeightsMatrixSet, new 
VoidLearningStrategy<Double, Double>(), new FeedForwardStrategy(new 
SigmoidFunction()));
+    NeuralNetwork<Double,Double> orNN = createFFNN(orWeightsMatrixSet);
     assertEquals(1l, Math.round(orNN.predict(createSample(1d, 0d))));
     assertEquals(1l, Math.round(orNN.predict(createSample(0d, 1d))));
     assertEquals(0l, Math.round(orNN.predict(createSample(0d, 0d))));
@@ -59,7 +59,7 @@ public class NeuralNetworkFactoryTest {
     double[][] weights = {{10d, -20d}};
     WeightsMatrix singleNotLayerWeights = new WeightsMatrix(weights);
     WeightsMatrix[] notWeightsMatrixSet = new 
WeightsMatrix[]{singleNotLayerWeights};
-    NeuralNetwork<Double,Double> orNN = NeuralNetworkFactory.create(new 
LinkedList<TrainingExample<Double, Double>>(), notWeightsMatrixSet, new 
VoidLearningStrategy<Double, Double>(), new FeedForwardStrategy(new 
SigmoidFunction()));
+    NeuralNetwork<Double,Double> orNN = createFFNN(notWeightsMatrixSet);
     assertEquals(1l, Math.round(orNN.predict(createSample(0d))));
     assertEquals(0l, Math.round(orNN.predict(createSample(1d))));
   }
@@ -69,13 +69,20 @@ public class NeuralNetworkFactoryTest {
     WeightsMatrix firstNorLayerWeights = new WeightsMatrix(new double[][]{{0, 
0, 0},{-30d, 20d, 20d}, {10d, -20d, -20d}});
     WeightsMatrix secondNorLayerWeights = new WeightsMatrix(new 
double[][]{{-10d, 20d, 20d}});
     WeightsMatrix[] norWeightsMatrixSet = new 
WeightsMatrix[]{firstNorLayerWeights,secondNorLayerWeights};
-    NeuralNetwork<Double,Double> norNN = NeuralNetworkFactory.create(new 
LinkedList<TrainingExample<Double, Double>>(), norWeightsMatrixSet, new 
VoidLearningStrategy<Double, Double>(), new FeedForwardStrategy(new 
SigmoidFunction()));
+    NeuralNetwork<Double,Double> norNN = createFFNN(norWeightsMatrixSet);
     assertEquals(0l, Math.round(norNN.predict(createSample(1d, 0d))));
     assertEquals(0l, Math.round(norNN.predict(createSample(0d, 1d))));
     assertEquals(1l, Math.round(norNN.predict(createSample(0d, 0d))));
     assertEquals(1l, Math.round(norNN.predict(createSample(1d, 1d))));
   }
 
+  private NeuralNetwork<Double, Double> createFFNN(WeightsMatrix[] 
andWeightsMatrixSet)
+          throws CreationException {
+    return NeuralNetworkFactory.create(new LinkedList<TrainingExample<Double, 
Double>>(), 
+            andWeightsMatrixSet, new VoidLearningStrategy<Double, Double>(), 
+            new FeedForwardStrategy(new SigmoidFunction()));
+  }
+
   private Example<Double> createSample(final Double... params) {
     return new Example<Double>() {
       @Override



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to