title: Fail to use ANN_MLP in Opencv 3 in Java
body: <p>I tried using ANN_MLP in OpenCV 3.0 to train a model for a simple XOR operation (i.e. 00->0, 01->1, 10->1, 11->0). But it returned NaN when I called nn.predict. What's wrong with the code? Here is my Java code:</p>
  
  <pre><code>package jm.app;
  import org.opencv.core.Core;
  import org.opencv.core.CvType;
  import org.opencv.core.Mat;
  import org.opencv.core.TermCriteria;
  import org.opencv.ml.ANN_MLP;
  import org.opencv.ml.Ml;
  import org.opencv.ml.StatModel;
  
  public class Main {
      static{System.loadLibrary(Core.NATIVE_LIBRARY_NAME);}
  
      public static void main(String[] args) {        
          Mat trainData = new Mat(4, 2, CvType.CV_32FC1);
          trainData.put(0, 0, 0);
          trainData.put(0, 1, 0);
  
          trainData.put(1, 0, 0);
          trainData.put(1, 1, 1);
  
          trainData.put(2, 0, 1);
          trainData.put(2, 1, 0);
  
          trainData.put(3, 0, 1);
          trainData.put(3, 1, 1);
  
          Mat trainLabels = new Mat(4, 1, CvType.CV_32FC1);
          trainLabels.put(0, 0, 0);
          trainLabels.put(1, 0, 1);
          trainLabels.put(2, 0, 1);
          trainLabels.put(3, 0, 0);
  
          ANN_MLP nn = ANN_MLP.create();
          nn.setActivationFunction(ANN_MLP.SIGMOID_SYM);
          nn.setTrainMethod(ANN_MLP.BACKPROP);        
          nn.setBackpropMomentumScale(0.1);
          nn.setBackpropWeightScale(0.1);
          nn.setTermCriteria(new TermCriteria(TermCriteria.MAX_ITER, (int)100000, 0.000001));
  
          Mat layers = new Mat(1, 3, CvType.CV_32SC1);
          layers.put(0, 0, 2);
          layers.put(0, 1, 3);
          layers.put(0, 2, 1);
          nn.setLayerSizes(layers);
  
          nn.train(trainData, Ml.ROW_SAMPLE, trainLabels);
  
          Mat testData = new Mat(1, 2, CvType.CV_32FC1);
          testData.put(0, 0, 1);
          testData.put(0, 1, 1);
  
          Mat testLabels = new Mat(1, 1, CvType.CV_32FC1);
          float res = nn.predict(testData, testLabels, ANN_MLP.RAW_OUTPUT);
          Util.printMat(testLabels);
  
          Mat layer1 = nn.getWeights(0);
          Mat layer2 = nn.getWeights(1);
          Mat layer3 = nn.getWeights(2);
          Util.printMat(layer1);
          Util.printMat(layer2);
          Util.printMat(layer3);             
      }
  }
  
  package jm.app;
  import org.opencv.core.Mat;
  
  public class Util {
  
      public static void printMat(Mat mat){
          System.out.println();
          System.out.print(mat.rows() + " * " + mat.cols());
          for(int i = 0; i &lt; mat.rows(); i++){
              System.out.println();
              for(int j = 0; j &lt; mat.cols(); j++){
                  System.out.print(mat.get(i, j)[0] + " ");
              }
          }
          System.out.println();
      }
  }
  </code></pre>
  
  <p>And the output is:</p>
  
  <pre><code>1 * 1
  NaN 
  
  1 * 4
  2.0 -1.0 2.0 -1.0 
  
  3 * 3
  -0.417962425638773 -0.11805564491195578 0.7527567170648859 
  0.40930192249590086 -0.24876980957807385 -0.2929439299929529 
  0.6025307693048867 0.2936134607392147 -0.10605986687856579 
  
  4 * 1
  0.5558049015443158 
  0.4766362469511742 
  0.3713056187114578 
  -0.24058588929784652 
  </code></pre>
  
  <p>So I have two questions:
  1. Why the "testLabel" got a NaN value?
  2. Why the "layer1" is a 1*4 matrix? What did the "layer1" do here?</p>