Author: tommaso
Date: Fri Jan 15 11:44:14 2016
New Revision: 1724779

URL: http://svn.apache.org/viewvc?rev=1724779&view=rev
Log:
fixed bug in SFFNN, added boolean operator NNs tests

Modified:
    
labs/yay/trunk/core/src/main/java/org/apache/yay/ShallowFeedForwardNeuralNetwork.java
    
labs/yay/trunk/core/src/test/java/org/apache/yay/ShallowFeedForwardNeuralNetworkTest.java

Modified: 
labs/yay/trunk/core/src/main/java/org/apache/yay/ShallowFeedForwardNeuralNetwork.java
URL: 
http://svn.apache.org/viewvc/labs/yay/trunk/core/src/main/java/org/apache/yay/ShallowFeedForwardNeuralNetwork.java?rev=1724779&r1=1724778&r2=1724779&view=diff
==============================================================================
--- 
labs/yay/trunk/core/src/main/java/org/apache/yay/ShallowFeedForwardNeuralNetwork.java
 (original)
+++ 
labs/yay/trunk/core/src/main/java/org/apache/yay/ShallowFeedForwardNeuralNetwork.java
 Fri Jan 15 11:44:14 2016
@@ -18,7 +18,6 @@
  */
 package org.apache.yay;
 
-import org.apache.commons.lang3.SystemUtils;
 import org.apache.commons.math3.linear.ArrayRealVector;
 import org.apache.commons.math3.linear.MatrixUtils;
 import org.apache.commons.math3.linear.RealMatrix;
@@ -54,11 +53,12 @@ public class ShallowFeedForwardNeuralNet
 
   public ShallowFeedForwardNeuralNetwork(Configuration configuration) {
     this.configuration = configuration;
-    initialize();
+    this.weights = createRandomWeights();
   }
 
-  private void initialize() {
-    weights = createRandomWeights();
+  public ShallowFeedForwardNeuralNetwork(Configuration configuration, 
RealMatrix[] weights) {
+    this.configuration = configuration;
+    this.weights = weights;
   }
 
   private RealMatrix[] createRandomWeights() {
@@ -338,7 +338,6 @@ public class ShallowFeedForwardNeuralNet
 
       // apply the activation function to each element in the matrix
       x = 
configuration.activationFunctions[idx].applyMatrix(x.getRowMatrix(0));
-      x = new SigmoidFunction().applyMatrix(x.getRowMatrix(0));
 
       debugOutput[w] = x.getRowVector(0);
     }

Modified: 
labs/yay/trunk/core/src/test/java/org/apache/yay/ShallowFeedForwardNeuralNetworkTest.java
URL: 
http://svn.apache.org/viewvc/labs/yay/trunk/core/src/test/java/org/apache/yay/ShallowFeedForwardNeuralNetworkTest.java?rev=1724779&r1=1724778&r2=1724779&view=diff
==============================================================================
--- 
labs/yay/trunk/core/src/test/java/org/apache/yay/ShallowFeedForwardNeuralNetworkTest.java
 (original)
+++ 
labs/yay/trunk/core/src/test/java/org/apache/yay/ShallowFeedForwardNeuralNetworkTest.java
 Fri Jan 15 11:44:14 2016
@@ -18,6 +18,8 @@
  */
 package org.apache.yay;
 
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.RealMatrix;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -60,6 +62,89 @@ public class ShallowFeedForwardNeuralNet
     double[] doubles = neuralNetwork.predictOutput(new double[]{0.7d, 0.8d, 
0.9d});
     assertNotNull(doubles);
 
-    assertEquals(0.4d, doubles[0], 0.4d);
+    assertEquals(0.9d, doubles[0], 0.2d);
   }
+
+  @Test
+  public void andNNCreationTest() throws Exception {
+    double[][] weights = {{-30d, 20d, 20d}};
+    RealMatrix singleAndLayerWeights = new Array2DRowRealMatrix(weights);
+    RealMatrix[] andRealMatrixSet = new RealMatrix[]{singleAndLayerWeights};
+
+    ShallowFeedForwardNeuralNetwork.Configuration configuration = new 
ShallowFeedForwardNeuralNetwork.Configuration();
+    configuration.alpha = 0.0001d;
+    configuration.layers = new int[]{2, 1};
+    configuration.maxIterations = 10000;
+    configuration.threshold = 0.004d;
+    configuration.activationFunctions = new ActivationFunction[]{new 
SigmoidFunction()};
+
+    ShallowFeedForwardNeuralNetwork and = new 
ShallowFeedForwardNeuralNetwork(configuration, andRealMatrixSet);
+
+    assertEquals(0L, Math.round(and.predictOutput(new double[]{1d, 0d})[0]));
+    assertEquals(0L, Math.round(and.predictOutput(new double[]{0d, 1d})[0]));
+    assertEquals(0L, Math.round(and.predictOutput(new double[]{0d, 0d})[0]));
+    assertEquals(1L, Math.round(and.predictOutput(new double[]{1d, 1d})[0]));
+  }
+
+  @Test
+  public void orNNCreationTest() throws Exception {
+    double[][] weights = {{-10d, 20d, 20d}};
+    RealMatrix singleOrLayerWeights = new Array2DRowRealMatrix(weights);
+    RealMatrix[] orRealMatrixSet = new RealMatrix[]{singleOrLayerWeights};
+
+    ShallowFeedForwardNeuralNetwork.Configuration configuration = new 
ShallowFeedForwardNeuralNetwork.Configuration();
+    configuration.alpha = 0.0001d;
+    configuration.layers = new int[]{2, 1};
+    configuration.maxIterations = 10000;
+    configuration.threshold = 0.004d;
+    configuration.activationFunctions = new ActivationFunction[]{new 
SigmoidFunction()};
+
+    ShallowFeedForwardNeuralNetwork or = new 
ShallowFeedForwardNeuralNetwork(configuration, orRealMatrixSet);
+
+    assertEquals(1L, Math.round(or.predictOutput(new double[]{1d, 0d})[0]));
+    assertEquals(1L, Math.round(or.predictOutput(new double[]{0d, 1d})[0]));
+    assertEquals(1L, Math.round(or.predictOutput(new double[]{1d, 1d})[0]));
+    assertEquals(0L, Math.round(or.predictOutput(new double[]{0d, 0d})[0]));
+  }
+
+  @Test
+  public void notNNCreationTest() throws Exception {
+    double[][] weights = {{10d, -20d}};
+    RealMatrix singleNotLayerWeights = new Array2DRowRealMatrix(weights);
+    RealMatrix[] notRealMatrixSet = new RealMatrix[]{singleNotLayerWeights};
+
+    ShallowFeedForwardNeuralNetwork.Configuration configuration = new 
ShallowFeedForwardNeuralNetwork.Configuration();
+    configuration.alpha = 0.0001d;
+    configuration.layers = new int[]{1, 1};
+    configuration.maxIterations = 10000;
+    configuration.threshold = 0.004d;
+    configuration.activationFunctions = new ActivationFunction[]{new 
SigmoidFunction()};
+
+    ShallowFeedForwardNeuralNetwork not = new 
ShallowFeedForwardNeuralNetwork(configuration, notRealMatrixSet);
+    assertEquals(1L, Math.round(not.predictOutput(new double[]{0d})[0]));
+    assertEquals(0L, Math.round(not.predictOutput(new double[]{1d})[0]));
+  }
+
+  @Test
+  public void norNNCreationTest() throws Exception {
+    RealMatrix firstNorLayerWeights = new Array2DRowRealMatrix(new 
double[][]{{0, 0, 0}, {-30d, 20d, 20d}, {10d, -20d, -20d}});
+    RealMatrix secondNorLayerWeights = new Array2DRowRealMatrix(new 
double[][]{{-10d, 20d, 20d}});
+    RealMatrix[] norRealMatrixSet = new RealMatrix[]{firstNorLayerWeights, 
secondNorLayerWeights};
+
+    ShallowFeedForwardNeuralNetwork.Configuration configuration = new 
ShallowFeedForwardNeuralNetwork.Configuration();
+    configuration.alpha = 0.0001d;
+    configuration.layers = new int[]{2, 2, 1};
+    configuration.maxIterations = 10000;
+    configuration.threshold = 0.004d;
+    configuration.activationFunctions = new ActivationFunction[]{new 
SigmoidFunction()};
+
+    ShallowFeedForwardNeuralNetwork nor = new 
ShallowFeedForwardNeuralNetwork(configuration, norRealMatrixSet);
+
+
+    assertEquals(0L, Math.round(nor.predictOutput(new double[]{1d, 0d})[0]));
+    assertEquals(0L, Math.round(nor.predictOutput(new double[]{0d, 1d})[0]));
+    assertEquals(1L, Math.round(nor.predictOutput(new double[]{0d, 0d})[0]));
+    assertEquals(1L, Math.round(nor.predictOutput(new double[]{1d, 1d})[0]));
+  }
+
 }
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@labs.apache.org
For additional commands, e-mail: commits-h...@labs.apache.org

Reply via email to