Repository: deeplearning4j/deeplearning4j
Issue: "Expected no workspace active" exception during evaluation

Training a simple MLP on MNIST with an EvaluativeListener, I get the following workspace-related exception: `Exception in thread "main" org.nd4j.linalg.workspace.ND4JWorkspaceException: Expected no workspace active in outputOfLayerDetached - Open/active workspaces: [WS_ALL_LAYERS_ACT]`

It happens on the first call to `doEvaluation(...)` inside of the listener, so it likely has to do with the inference workspace mode...?

Here's a full stack trace for the exception:
```
Exception in thread "main" org.nd4j.linalg.workspace.ND4JWorkspaceException: Expected no workspace active in outputOfLayerDetached - Open/active workspaces: [WS_ALL_LAYERS_ACT]
	at org.nd4j.linalg.workspace.WorkspaceUtils.assertNoWorkspacesOpen(WorkspaceUtils.java:38)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.outputOfLayerDetached(MultiLayerNetwork.java:1030)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.doEvaluation(MultiLayerNetwork.java:3052)
	at org.deeplearning4j.optimize.listeners.EvaluativeListener.invokeListener(EvaluativeListener.java:211)
	at org.deeplearning4j.optimize.listeners.EvaluativeListener.onBackwardPass(EvaluativeListener.java:188)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.computeGradientAndScore(MultiLayerNetwork.java:2460)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.computeGradientAndScore(MultiLayerNetwork.java:2395)
	at org.deeplearning4j.optimize.solvers.BaseOptimizer.gradientAndScore(BaseOptimizer.java:178)
	at org.deeplearning4j.optimize.solvers.StochasticGradientDescent.optimize(StochasticGradientDescent.java:60)
	at org.deeplearning4j.optimize.Solver.optimize(Solver.java:54)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fit(MultiLayerNetwork.java:1459)
	at org.deeplearning4j.distributed.BasicTrainingExperimentDriver.entryPoint(BasicTrainingExperimentDriver.java:127)
	at org.deeplearning4j.distributed.BasicTrainingExperimentDriver.main(BasicTrainingExperimentDriver.java:58)
```

Changed files:
deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/EvalTest.java
deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/optimize/listeners/EvaluativeListener.java

diff --git a/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/EvalTest.java b/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/EvalTest.java
index c71b5ab115..bf36fe97db 100644
--- a/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/EvalTest.java
+++ b/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/eval/EvalTest.java
@@ -43,6 +43,7 @@ import org.deeplearning4j.nn.conf.layers.RnnOutputLayer;
 import org.deeplearning4j.nn.graph.ComputationGraph;
 import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
 import org.deeplearning4j.nn.weights.WeightInit;
+import org.deeplearning4j.optimize.listeners.EvaluativeListener;
 import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
 import org.junit.Test;
 import org.nd4j.linalg.activations.Activation;
@@ -1249,6 +1250,40 @@ public class EvalTest extends BaseDL4JTest {
 
         System.out.println("\\n\\n\\n\\n");
         System.out.println(e.stats(false, true));
+    }
+
+
+
+    @Test
+    public void testEvaluativeListenerSimple(){
+        //Sanity check: https://github.com/deeplearning4j/deeplearning4j/issues/5351
+
+        // Network config
+        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
+
+                .optimizationAlgo(OptimizationAlgorithm.LINE_GRADIENT_DESCENT).seed(42)
+                .updater(new Sgd(1e-6)).list()
+                .layer(0, new DenseLayer.Builder().nIn(4).nOut(2).activation(Activation.TANH)
+                        .weightInit(WeightInit.XAVIER).build())
+                .layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
+                        LossFunctions.LossFunction.MCXENT).nIn(2).nOut(3).weightInit(WeightInit.XAVIER)
+                        .activation(Activation.SOFTMAX).build())
+                .build();
+
+        // Instantiate model
+        MultiLayerNetwork net = new MultiLayerNetwork(conf);
+        net.init();
+
+        // Train-test split
+        DataSetIterator iter = new IrisDataSetIterator(30, 150);
+        DataSetIterator iterTest = new IrisDataSetIterator(30, 150);
+
+        net.setListeners(new EvaluativeListener(iterTest, 3));
+
+        for( int i=0; i<10; i++ ){
+            net.fit(iter);
+        }
+
 
     }
 }
diff --git a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/optimize/listeners/EvaluativeListener.java b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/optimize/listeners/EvaluativeListener.java
index d8168c0b12..82e6d2aad3 100644
--- a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/optimize/listeners/EvaluativeListener.java
+++ b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/optimize/listeners/EvaluativeListener.java
@@ -152,7 +152,8 @@ public class EvaluativeListener extends BaseTrainingListener {
      */
     @Override
     public void iterationDone(Model model, int iteration, int epoch) {
-        // no-op
+        if (invocationType == InvocationType.ITERATION_END)
+            invokeListener(model);
     }
 
     @Override
@@ -167,27 +168,6 @@ public class EvaluativeListener extends BaseTrainingListener {
             invokeListener(model);
     }
 
-    @Override
-    public void onForwardPass(Model model, List<INDArray> activations) {
-        // no-op
-    }
-
-    @Override
-    public void onForwardPass(Model model, Map<String, INDArray> activations) {
-        // no-op
-    }
-
-    @Override
-    public void onGradientCalculation(Model model) {
-        // no-op
-    }
-
-    @Override
-    public void onBackwardPass(Model model) {
-        if (invocationType == InvocationType.ITERATION_END)
-            invokeListener(model);
-    }
-
     protected void invokeListener(Model model) {
         if (iterationCount.get() == null)
             iterationCount.set(new AtomicLong(0));