Repository: deeplearning4j/deeplearning4j
Issue: RegressionEvaluation bug

```
    @Test
    public void testRegressionEvalTimeSeriesSplit(){

        INDArray out1 = Nd4j.rand(new int[]{3, 5, 20});
        INDArray outSub1 = out1.get(all(), all(), interval(0,10));
        INDArray outSub2 = out1.get(all(), all(), interval(10, 20));

        INDArray label1 = Nd4j.rand(new int[]{3, 5, 20});
        INDArray labelSub1 = label1.get(all(), all(), interval(0,10));
        INDArray labelSub2 = label1.get(all(), all(), interval(10, 20));

        RegressionEvaluation e1 = new RegressionEvaluation();
        RegressionEvaluation e2 = new RegressionEvaluation();

        e1.eval(label1, out1);

        e2.eval(labelSub1, outSub1);
        e2.eval(labelSub2, outSub2);

        assertEquals(e1, e2);
    }
```

Following test fails due to ```sumSquaredLabelDeviations``` array only.

Changed files:
deeplearning4j-core/src/test/java/org/deeplearning4j/eval/RegressionEvalTest.java
deeplearning4j-nn/src/main/java/org/deeplearning4j/eval/RegressionEvaluation.java
deeplearning4j-core/src/test/java/org/deeplearning4j/eval/EvalTest.java

diff --git a/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/EvalTest.java b/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/EvalTest.java
index 599a5e53f9..2de65032de 100644
--- a/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/EvalTest.java
+++ b/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/EvalTest.java
@@ -53,6 +53,8 @@ import org.nd4j.linalg.util.FeatureUtil;
 import java.util.*;
 
 import static org.junit.Assert.*;
+import static org.nd4j.linalg.indexing.NDArrayIndex.all;
+import static org.nd4j.linalg.indexing.NDArrayIndex.interval;
 
 /**
  * Created by agibsonccc on 12/22/14.
@@ -254,7 +256,7 @@ public class EvalTest {
             for (int j = 0; j < tsLength; j++) {
                 INDArray rand = Nd4j.rand(1, nOut);
                 rand.divi(rand.sumNumber());
-                predicted.put(new INDArrayIndex[] {NDArrayIndex.point(i), NDArrayIndex.all(), NDArrayIndex.point(j)},
+                predicted.put(new INDArrayIndex[] {NDArrayIndex.point(i), all(), NDArrayIndex.point(j)},
                                 rand);
                 int idx = r.nextInt(nOut);
                 labels.putScalar(new int[] {i, idx, j}, 1.0);
@@ -264,11 +266,11 @@ public class EvalTest {
         //Create a longer labels/predicted with mask for first and last time step
         //Expect masked evaluation to be identical to original evaluation
         INDArray labels2 = Nd4j.zeros(miniBatch, nOut, tsLength + 2);
-        labels2.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all(),
-                        NDArrayIndex.interval(1, tsLength + 1)}, labels);
+        labels2.put(new INDArrayIndex[] {all(), all(),
+                        interval(1, tsLength + 1)}, labels);
         INDArray predicted2 = Nd4j.zeros(miniBatch, nOut, tsLength + 2);
-        predicted2.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all(),
-                        NDArrayIndex.interval(1, tsLength + 1)}, predicted);
+        predicted2.put(new INDArrayIndex[] {all(), all(),
+                        interval(1, tsLength + 1)}, predicted);
 
         INDArray labelsMask = Nd4j.ones(miniBatch, tsLength + 2);
         for (int i = 0; i < miniBatch; i++) {
@@ -323,7 +325,7 @@ public class EvalTest {
             INDArray rand = Nd4j.rand(1, numClasses);
             rand.put(0, winner, rand.sumNumber());
             rand.divi(rand.sumNumber());
-            predicted.put(new INDArrayIndex[] {NDArrayIndex.point(i), NDArrayIndex.all()}, rand);
+            predicted.put(new INDArrayIndex[] {NDArrayIndex.point(i), all()}, rand);
             //Generating random label
             int label = r.nextInt(numClasses);
             labels.putScalar(new int[] {i, label}, 1.0);
@@ -359,16 +361,16 @@ public class EvalTest {
 
         //Now: split into 3 separate evaluation objects -> expect identical values after merging
         Evaluation eval1 = new Evaluation();
-        eval1.eval(actual.get(NDArrayIndex.interval(0, 5), NDArrayIndex.all()),
-                        predicted.get(NDArrayIndex.interval(0, 5), NDArrayIndex.all()));
+        eval1.eval(actual.get(interval(0, 5), all()),
+                        predicted.get(interval(0, 5), all()));
 
         Evaluation eval2 = new Evaluation();
-        eval2.eval(actual.get(NDArrayIndex.interval(5, 10), NDArrayIndex.all()),
-                        predicted.get(NDArrayIndex.interval(5, 10), NDArrayIndex.all()));
+        eval2.eval(actual.get(interval(5, 10), all()),
+                        predicted.get(interval(5, 10), all()));
 
         Evaluation eval3 = new Evaluation();
-        eval3.eval(actual.get(NDArrayIndex.interval(10, nRows), NDArrayIndex.all()),
-                        predicted.get(NDArrayIndex.interval(10, nRows), NDArrayIndex.all()));
+        eval3.eval(actual.get(interval(10, nRows), all()),
+                        predicted.get(interval(10, nRows), all()));
 
         eval1.merge(eval2);
         eval1.merge(eval3);
@@ -378,8 +380,8 @@ public class EvalTest {
 
         //Next: check evaluation merging with empty, and empty merging with non-empty
         eval1 = new Evaluation();
-        eval1.eval(actual.get(NDArrayIndex.interval(0, 5), NDArrayIndex.all()),
-                        predicted.get(NDArrayIndex.interval(0, 5), NDArrayIndex.all()));
+        eval1.eval(actual.get(interval(0, 5), all()),
+                        predicted.get(interval(0, 5), all()));
 
         Evaluation evalInitiallyEmpty = new Evaluation();
         evalInitiallyEmpty.merge(eval1);
diff --git a/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/RegressionEvalTest.java b/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/RegressionEvalTest.java
index dcc42cb9f1..ff2523abfc 100644
--- a/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/RegressionEvalTest.java
+++ b/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/RegressionEvalTest.java
@@ -20,6 +20,8 @@ import java.util.Collections;
 import java.util.List;
 
 import static org.junit.Assert.assertEquals;
+import static org.nd4j.linalg.indexing.NDArrayIndex.all;
+import static org.nd4j.linalg.indexing.NDArrayIndex.interval;
 
 /**
  * @author Alex Black
@@ -204,4 +206,26 @@ public class RegressionEvalTest {
             assertEquals(rmse[i], re.rootMeanSquaredError(i), 1e-6);
         }
     }
+
+    @Test
+    public void testRegressionEvalTimeSeriesSplit(){
+
+        INDArray out1 = Nd4j.rand(new int[]{3, 5, 20});
+        INDArray outSub1 = out1.get(all(), all(), interval(0,10));
+        INDArray outSub2 = out1.get(all(), all(), interval(10, 20));
+
+        INDArray label1 = Nd4j.rand(new int[]{3, 5, 20});
+        INDArray labelSub1 = label1.get(all(), all(), interval(0,10));
+        INDArray labelSub2 = label1.get(all(), all(), interval(10, 20));
+
+        RegressionEvaluation e1 = new RegressionEvaluation();
+        RegressionEvaluation e2 = new RegressionEvaluation();
+
+        e1.eval(label1, out1);
+
+        e2.eval(labelSub1, outSub1);
+        e2.eval(labelSub2, outSub2);
+
+        assertEquals(e1, e2);
+    }
 }
diff --git a/deeplearning4j-nn/src/main/java/org/deeplearning4j/eval/RegressionEvaluation.java b/deeplearning4j-nn/src/main/java/org/deeplearning4j/eval/RegressionEvaluation.java
index 9ef34ca53c..de701ca49c 100644
--- a/deeplearning4j-nn/src/main/java/org/deeplearning4j/eval/RegressionEvaluation.java
+++ b/deeplearning4j-nn/src/main/java/org/deeplearning4j/eval/RegressionEvaluation.java
@@ -66,7 +66,7 @@ public class RegressionEvaluation extends BaseEvaluation<RegressionEvaluation> {
     private INDArray sumSquaredPredicted;
     @JsonSerialize(using = RowVectorSerializer.class)
     @JsonDeserialize(using = RowVectorDeserializer.class)
-    private INDArray sumSquaredLabelDeviations;
+    private INDArray sumLabels;
 
     public RegressionEvaluation() {
         this(null, DEFAULT_PRECISION);
@@ -135,7 +135,7 @@ public class RegressionEvaluation extends BaseEvaluation<RegressionEvaluation> {
         sumOfProducts = Nd4j.zeros(n);
         sumSquaredLabels = Nd4j.zeros(n);
         sumSquaredPredicted = Nd4j.zeros(n);
-        sumSquaredLabelDeviations = Nd4j.zeros(n);
+        sumLabels = Nd4j.zeros(n);
 
         initialized = true;
     }
@@ -217,8 +217,7 @@ public class RegressionEvaluation extends BaseEvaluation<RegressionEvaluation> {
                         .divi(newExampleCountPerColumn);
         exampleCountPerColumn = newExampleCountPerColumn;
 
-        final INDArray n = labels.dup().subRowVector(currentMean);
-        sumSquaredLabelDeviations.addi(n.muli(n).sum(0));
+        sumLabels.addi(labels.sum(0));
     }
 
     @Override
@@ -384,8 +383,14 @@ public class RegressionEvaluation extends BaseEvaluation<RegressionEvaluation> {
      * @see <a href="https://en.wikipedia.org/wiki/Coefficient_of_determination">Wikipedia</a>
      */
     public double rSquared(int column) {
-        final double sstot = sumSquaredLabelDeviations.getDouble(column);
-        final double ssres = sumSquaredErrorsPerColumn.getDouble(column);
+        //ss_tot = sum_i (label_i - mean(labels))^2
+        //       = (sum_i label_i^2) + mean(labels) * (n * mean(labels) - 2 * sum_i label_i)
+        double sumLabelSquared = sumSquaredLabels.getDouble(column);
+        double meanLabel = currentMean.getDouble(column);
+        double sumLabel = sumLabels.getDouble(column);
+        double n = exampleCountPerColumn.getDouble(column);
+        double sstot = sumLabelSquared + meanLabel * (n * meanLabel - 2 * sumLabel);
+        double ssres = sumSquaredErrorsPerColumn.getDouble(column);
         return (sstot - ssres) / sstot;
     }