You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2015/07/19 00:11:20 UTC

[2/2] [math] New private method for factoring out some common code.

New private method for factoring out some common code.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/7a8a7783
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/7a8a7783
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/7a8a7783

Branch: refs/heads/master
Commit: 7a8a7783315917b56b6a71c4f28cec4ed1d91a7f
Parents: a7fe613
Author: Gilles <er...@apache.org>
Authored: Sun Jul 19 00:07:03 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Sun Jul 19 00:07:03 2015 +0200

----------------------------------------------------------------------
 .../ml/neuralnet/sofm/KohonenUpdateAction.java  | 32 ++++++++++++++------
 1 file changed, 22 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/7a8a7783/src/main/java/org/apache/commons/math4/ml/neuralnet/sofm/KohonenUpdateAction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/ml/neuralnet/sofm/KohonenUpdateAction.java b/src/main/java/org/apache/commons/math4/ml/neuralnet/sofm/KohonenUpdateAction.java
index ffe6943..b937291 100644
--- a/src/main/java/org/apache/commons/math4/ml/neuralnet/sofm/KohonenUpdateAction.java
+++ b/src/main/java/org/apache/commons/math4/ml/neuralnet/sofm/KohonenUpdateAction.java
@@ -146,6 +146,26 @@ public class KohonenUpdateAction implements UpdateAction {
     }
 
     /**
+     * Tries to update a neuron.
+     *
+     * @param n Neuron to be updated.
+     * @param features Training data.
+     * @param learningRate Learning factor.
+     * @return {@code true} if the update succeeded, {@code true} if a
+     * concurrent update has been detected.
+     */
+    private boolean attemptNeuronUpdate(Neuron n,
+                                        double[] features,
+                                        double learningRate) {
+        final double[] expect = n.getFeatures();
+        final double[] update = computeFeatures(expect,
+                                                features,
+                                                learningRate);
+
+        return n.compareAndSetFeatures(expect, update);
+    }
+
+    /**
      * Atomically updates the given neuron.
      *
      * @param n Neuron to be updated.
@@ -156,11 +176,7 @@ public class KohonenUpdateAction implements UpdateAction {
                                           double[] features,
                                           double learningRate) {
         while (true) {
-            final double[] expect = n.getFeatures();
-            final double[] update = computeFeatures(expect,
-                                                    features,
-                                                    learningRate);
-            if (n.compareAndSetFeatures(expect, update)) {
+            if (attemptNeuronUpdate(n, features, learningRate)) {
                 break;
             }
         }
@@ -181,11 +197,7 @@ public class KohonenUpdateAction implements UpdateAction {
         while (true) {
             final Neuron best = MapUtils.findBest(features, net, distance);
 
-            final double[] expect = best.getFeatures();
-            final double[] update = computeFeatures(expect,
-                                                    features,
-                                                    learningRate);
-            if (best.compareAndSetFeatures(expect, update)) {
+            if (attemptNeuronUpdate(best, features, learningRate)) {
                 return best;
             }