Author: tommaso
Date: Tue Jan 24 17:10:49 2012
New Revision: 1235360

URL: http://svn.apache.org/viewvc?rev=1235360&view=rev
Log:
Introduced commons-math for easier matrix operations

Added:
    labs/yay/trunk/core/src/main/java/org/apache/yay/MatrixConverter.java
Modified:
    labs/yay/trunk/core/pom.xml
    labs/yay/trunk/core/src/main/java/org/apache/yay/NeuralNetworkFactory.java

Modified: labs/yay/trunk/core/pom.xml
URL: 
http://svn.apache.org/viewvc/labs/yay/trunk/core/pom.xml?rev=1235360&r1=1235359&r2=1235360&view=diff
==============================================================================
--- labs/yay/trunk/core/pom.xml (original)
+++ labs/yay/trunk/core/pom.xml Tue Jan 24 17:10:49 2012
@@ -18,5 +18,10 @@
       <version>1.9.0-rc1</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-math</artifactId>
+      <version>2.2</version>
+    </dependency>
   </dependencies>
 </project>
\ No newline at end of file

Added: labs/yay/trunk/core/src/main/java/org/apache/yay/MatrixConverter.java
URL: 
http://svn.apache.org/viewvc/labs/yay/trunk/core/src/main/java/org/apache/yay/MatrixConverter.java?rev=1235360&view=auto
==============================================================================
--- labs/yay/trunk/core/src/main/java/org/apache/yay/MatrixConverter.java 
(added)
+++ labs/yay/trunk/core/src/main/java/org/apache/yay/MatrixConverter.java Tue 
Jan 24 17:10:49 2012
@@ -0,0 +1,53 @@
+/*
+ * 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 org.apache.commons.math.linear.Array2DRowRealMatrix;
+import org.apache.commons.math.linear.RealMatrix;
+
+import java.util.Collection;
+
+/**
+ */
+public class MatrixConverter {
+
+  public RealMatrix toMatrix(Collection<TrainingExample<Double, Double>> 
trainingSet) {
+    double[][] matrixData = new double[trainingSet.size()][];
+
+    int i = 0;
+    for (TrainingExample<Double, Double> sample : trainingSet) {
+      double[] sampleRow = toDoubleArray(sample);
+      matrixData[i] = sampleRow;
+      i++;
+    }
+
+    return new Array2DRowRealMatrix(matrixData);
+  }
+
+  private double[] toDoubleArray(TrainingExample<Double, Double> sample) {
+    double[] ar = new double[sample.getFeatureVector().size() + 1];
+    int i = 0;
+    for (Feature<Double> f : sample.getFeatureVector()) {
+      ar[i] = f.getValue().doubleValue();
+      i++;
+    }
+    ar[i + 1] = sample.getOutput().getValue();
+    return ar;
+  }
+}

Modified: 
labs/yay/trunk/core/src/main/java/org/apache/yay/NeuralNetworkFactory.java
URL: 
http://svn.apache.org/viewvc/labs/yay/trunk/core/src/main/java/org/apache/yay/NeuralNetworkFactory.java?rev=1235360&r1=1235359&r2=1235360&view=diff
==============================================================================
--- labs/yay/trunk/core/src/main/java/org/apache/yay/NeuralNetworkFactory.java 
(original)
+++ labs/yay/trunk/core/src/main/java/org/apache/yay/NeuralNetworkFactory.java 
Tue Jan 24 17:10:49 2012
@@ -18,6 +18,7 @@
  */
 package org.apache.yay;
 
+import org.apache.commons.math.linear.RealMatrix;
 import org.apache.yay.bio.Signal;
 
 import java.util.Collection;
@@ -27,7 +28,11 @@ import java.util.Set;
  * Factory class for creating {@link NeuralNetwork}s
  */
 public class NeuralNetworkFactory {
-  public static NeuralNetwork create(Set<WeightsMatrix> weightsMatrixes, 
Collection<TrainingExample> trainingSet) throws InvalidWeightMatrixException {
+
+
+  public static NeuralNetwork create(final Set<RealMatrix> weightsMatrixes, 
final Collection<TrainingExample<Double, Double>> trainingSet) throws 
InvalidWeightMatrixException {
+
+    final MatrixConverter matrixConverter = new MatrixConverter();
 
     // ad bias units to each layer
 
@@ -47,8 +52,18 @@ public class NeuralNetworkFactory {
         [dummy, p] = max(h2, [], 2);
 
          */
+
+        RealMatrix x = matrixConverter.toMatrix(trainingSet);
+        for (RealMatrix weightsMatrix : weightsMatrixes) {
+          x = weightsMatrix.transpose().multiply(x);
+          // TODO : apply SigmoidFunction
+        }
+
+
         return null;
       }
+
+
     };
 
 



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

Reply via email to