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/10/09 13:53:15 UTC
svn commit: r1395980 - in /labs/yay/trunk:
bio/src/main/java/org/apache/yay/bio/ core/src/main/java/org/apache/yay/
core/src/main/java/org/apache/yay/neuron/
Author: tommaso
Date: Tue Oct 9 11:53:15 2012
New Revision: 1395980
URL: http://svn.apache.org/viewvc?rev=1395980&view=rev
Log:
adding a neuron package to core for basic neuron implementations, minor fixes
Added:
labs/yay/trunk/core/src/main/java/org/apache/yay/StepActivationFunction.java (with props)
labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/
labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/LinearNeuron.java (with props)
Modified:
labs/yay/trunk/bio/src/main/java/org/apache/yay/bio/Nucleus.java
labs/yay/trunk/core/src/main/java/org/apache/yay/BackPropagationLearningStrategy.java
labs/yay/trunk/core/src/main/java/org/apache/yay/BasicElaborationUnit.java
Modified: labs/yay/trunk/bio/src/main/java/org/apache/yay/bio/Nucleus.java
URL: http://svn.apache.org/viewvc/labs/yay/trunk/bio/src/main/java/org/apache/yay/bio/Nucleus.java?rev=1395980&r1=1395979&r2=1395980&view=diff
==============================================================================
--- labs/yay/trunk/bio/src/main/java/org/apache/yay/bio/Nucleus.java (original)
+++ labs/yay/trunk/bio/src/main/java/org/apache/yay/bio/Nucleus.java Tue Oct 9 11:53:15 2012
@@ -23,6 +23,6 @@ package org.apache.yay.bio;
*/
public interface Nucleus<T> {
- public Signal<T> elaborate(Signal<T> signal);
+ public Signal<T> elaborate(Signal<T>... signal);
}
Modified: labs/yay/trunk/core/src/main/java/org/apache/yay/BackPropagationLearningStrategy.java
URL: http://svn.apache.org/viewvc/labs/yay/trunk/core/src/main/java/org/apache/yay/BackPropagationLearningStrategy.java?rev=1395980&r1=1395979&r2=1395980&view=diff
==============================================================================
--- labs/yay/trunk/core/src/main/java/org/apache/yay/BackPropagationLearningStrategy.java (original)
+++ labs/yay/trunk/core/src/main/java/org/apache/yay/BackPropagationLearningStrategy.java Tue Oct 9 11:53:15 2012
@@ -46,13 +46,14 @@ public class BackPropagationLearningStra
try {
// contains activation errors for the current training example
RealMatrix[] activationErrors = new RealMatrix[weightsMatrixSet.length - 1];
-
+
+ // feed forward propagation
RealMatrix output = predictionStrategy.debugOutput(ConversionUtils.toVector(trainingExample.getFeatureVector()), weightsMatrixSet);
- Double[] learnedOutput = trainingExample.getOutput();
- RealVector predictedOutputVector = new ArrayRealVector(output.getColumn(output.getColumnDimension() - 1));
- RealVector learnedOutputRealVector = new ArrayRealVector(learnedOutput);
+ Double[] learnedOutput = trainingExample.getOutput(); // training example output
+ RealVector predictedOutputVector = new ArrayRealVector(output.getColumn(output.getColumnDimension() - 1)); // turn output to vector
+ RealVector learnedOutputRealVector = new ArrayRealVector(learnedOutput); // turn example output to a vector
- RealVector error = predictedOutputVector.subtract(learnedOutputRealVector); // final layer error
+ RealVector error = predictedOutputVector.subtract(learnedOutputRealVector); // final layer error vector
activationErrors[activationErrors.length - 1] = new Array2DRowRealMatrix(error.toArray());
RealVector nextLayerDelta = error;
@@ -61,7 +62,7 @@ public class BackPropagationLearningStra
// TODO : remove the byas term from the error calculations
for (int l = weightsMatrixSet.length - 2; l > 0; l--) {
WeightsMatrix currentMatrix = weightsMatrixSet[l];
- ArrayRealVector realVector = new ArrayRealVector(output.getColumn(l));
+ ArrayRealVector realVector = new ArrayRealVector(output.getColumn(l)); // get l-th nn layer activations
ArrayRealVector identity = new ArrayRealVector(realVector.getDimension(), 1d);
RealVector gz = realVector.ebeMultiply(identity.subtract(realVector)); // = a^l .* (1-a^l)
RealVector resultingDeltaVector = currentMatrix.transpose().preMultiply(nextLayerDelta).ebeMultiply(gz);
Modified: labs/yay/trunk/core/src/main/java/org/apache/yay/BasicElaborationUnit.java
URL: http://svn.apache.org/viewvc/labs/yay/trunk/core/src/main/java/org/apache/yay/BasicElaborationUnit.java?rev=1395980&r1=1395979&r2=1395980&view=diff
==============================================================================
--- labs/yay/trunk/core/src/main/java/org/apache/yay/BasicElaborationUnit.java (original)
+++ labs/yay/trunk/core/src/main/java/org/apache/yay/BasicElaborationUnit.java Tue Oct 9 11:53:15 2012
@@ -23,7 +23,11 @@ package org.apache.yay;
*/
public abstract class BasicElaborationUnit<T> {
- private ActivationFunction<T> activationFunction;
+ protected ActivationFunction<T> activationFunction;
+
+ public BasicElaborationUnit() {
+ this.activationFunction = new IdentityActivationFunction<T>();
+ }
public BasicElaborationUnit(ActivationFunction<T> activationFunction) {
this.activationFunction = activationFunction;
@@ -33,5 +37,5 @@ public abstract class BasicElaborationUn
return activationFunction.apply(combine(inputs));
}
- public abstract T combine(T... inputs);
+ protected abstract T combine(T... inputs);
}
Added: labs/yay/trunk/core/src/main/java/org/apache/yay/StepActivationFunction.java
URL: http://svn.apache.org/viewvc/labs/yay/trunk/core/src/main/java/org/apache/yay/StepActivationFunction.java?rev=1395980&view=auto
==============================================================================
--- labs/yay/trunk/core/src/main/java/org/apache/yay/StepActivationFunction.java (added)
+++ labs/yay/trunk/core/src/main/java/org/apache/yay/StepActivationFunction.java Tue Oct 9 11:53:15 2012
@@ -0,0 +1,37 @@
+/*
+ * 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;
+
+/**
+ * An {@link ActivationFunction} implementing a step function of the input
+ */
+public class StepActivationFunction implements ActivationFunction<Double> {
+
+ private double center;
+
+ public StepActivationFunction(double center) {
+ this.center = center;
+ }
+
+ @Override
+ public Double apply(Double signal) {
+ return signal >= center ? 1d : 0d;
+ }
+
+}
Propchange: labs/yay/trunk/core/src/main/java/org/apache/yay/StepActivationFunction.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/LinearNeuron.java
URL: http://svn.apache.org/viewvc/labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/LinearNeuron.java?rev=1395980&view=auto
==============================================================================
--- labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/LinearNeuron.java (added)
+++ labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/LinearNeuron.java Tue Oct 9 11:53:15 2012
@@ -0,0 +1,48 @@
+/*
+ * 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.neuron;
+
+import org.apache.yay.BasicElaborationUnit;
+import org.apache.yay.IdentityActivationFunction;
+
+/**
+ * A neuron doing linear combination of inputs
+ */
+public class LinearNeuron extends BasicElaborationUnit<Double> {
+
+ private final double[] weights;
+ private Double bias;
+
+ public LinearNeuron(Double bias, double... weights) {
+ this.activationFunction = new IdentityActivationFunction<Double>();
+ this.bias = bias;
+ this.weights = weights;
+ }
+
+ @Override
+ protected Double combine(Double... inputs) {
+ Double res = bias;
+ for (int i = 0; i < inputs.length; i++) {
+ res += inputs[i] * weights[i];
+ }
+ return res;
+ }
+
+
+}
Propchange: labs/yay/trunk/core/src/main/java/org/apache/yay/neuron/LinearNeuron.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org