You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by to...@apache.org on 2012/11/18 16:41:14 UTC

svn commit: r1410903 - in /labs/yay/trunk/core/src/main/java/org/apache/yay: BasicPerceptron.java neuron/BinaryThresholdNeuron.java

Author: tommaso
Date: Sun Nov 18 15:41:13 2012
New Revision: 1410903

URL: http://svn.apache.org/viewvc?rev=1410903&view=rev
Log:
basic perceptron impl based on binary threshold neuron

Added:
    labs/yay/trunk/core/src/main/java/org/apache/yay/BasicPerceptron.java   (with props)
Modified:
    labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/BinaryThresholdNeuron.java

Added: 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=1410903&view=auto
==============================================================================
--- labs/yay/trunk/core/src/main/java/org/apache/yay/BasicPerceptron.java (added)
+++ labs/yay/trunk/core/src/main/java/org/apache/yay/BasicPerceptron.java Sun Nov 18 15:41:13 2012
@@ -0,0 +1,69 @@
+/*
+ * 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.apache.yay.neuron.BinaryThresholdNeuron;
+import org.apache.yay.utils.ConversionUtils;
+
+/**
+ * A perceptron {@link NeuralNetwork} implementation based on {@link org.apache.yay.neuron.BinaryThresholdNeuron}s
+ */
+public class BasicPerceptron implements NeuralNetwork<Double, Double> {
+
+    private BinaryThresholdNeuron perceptronNeuron;
+
+    private double[] currentWeights;
+
+    /**
+     * Create a perceptron given its input weights.
+     * Assume bias weight is given and all the input samples have a corresponding 1 input for that
+     *
+     * @param inputWeights the array of starting weights for the perceptron
+     */
+    public BasicPerceptron(double[] inputWeights) {
+        this.perceptronNeuron = new BinaryThresholdNeuron(0, inputWeights);
+        this.currentWeights = inputWeights;
+    }
+
+    @Override
+    public void learn(TrainingExample<Double, Double>... trainingExamples) throws LearningException {
+        for (TrainingExample<Double, Double> example : trainingExamples) {
+            Vector<Double> doubles = ConversionUtils.toVector(example.getFeatureVector());
+            Double[] inputs = doubles.toArray(new Double[doubles.size()]);
+            Double calculatedOutput = perceptronNeuron.elaborate(inputs);
+            int diff = calculatedOutput.compareTo(example.getOutput());
+            if (diff > 0) {
+                for (int i = 0; i < currentWeights.length; i++) {
+                    currentWeights[i] += inputs[i];
+                }
+            } else if (diff < 0) {
+                for (int i = 0; i < currentWeights.length; i++) {
+                    currentWeights[i] -= inputs[i];
+                }
+            }
+        }
+    }
+
+    @Override
+    public Double predict(Example<Double> input) throws PredictionException {
+        return perceptronNeuron.elaborate(ConversionUtils.toVector(input.getFeatureVector()).toArray(new Double[input.getFeatureVector().size()]));
+    }
+}

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

Modified: labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/BinaryThresholdNeuron.java
URL: http://svn.apache.org/viewvc/labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/BinaryThresholdNeuron.java?rev=1410903&r1=1410902&r2=1410903&view=diff
==============================================================================
--- labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/BinaryThresholdNeuron.java (original)
+++ labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/BinaryThresholdNeuron.java Sun Nov 18 15:41:13 2012
@@ -37,6 +37,10 @@ public class BinaryThresholdNeuron exten
         this.weights = weights;
     }
 
+    public void updateWeights(double... weights) {
+        this.weights = weights;
+    }
+
     @Override
     protected Double combine(Double... inputs) {
         Double res = 0d;



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