You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@horn.apache.org by ed...@apache.org on 2016/04/26 05:46:23 UTC

[2/4] incubator-horn git commit: Code refactoring

http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/ac8eaf8e/src/main/java/org/apache/horn/examples/NeuralNetwork.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/horn/examples/NeuralNetwork.java b/src/main/java/org/apache/horn/examples/NeuralNetwork.java
deleted file mode 100644
index 5c0afdf..0000000
--- a/src/main/java/org/apache/horn/examples/NeuralNetwork.java
+++ /dev/null
@@ -1,217 +0,0 @@
-/**
- * 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.horn.examples;
-
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.net.URI;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.hadoop.fs.FileSystem;
-import org.apache.hadoop.fs.Path;
-import org.apache.hama.HamaConfiguration;
-import org.apache.hama.commons.math.DenseDoubleVector;
-import org.apache.hama.commons.math.DoubleVector;
-import org.apache.horn.bsp.SmallLayeredNeuralNetwork;
-import org.apache.horn.funcs.FunctionFactory;
-
-/**
- * The example of using {@link SmallLayeredNeuralNetwork}, including the
- * training phase and labeling phase.
- */
-public class NeuralNetwork {
-
-  public static void main(String[] args) throws Exception {
-    if (args.length < 3) {
-      printUsage();
-      return;
-    }
-    HamaConfiguration conf = new HamaConfiguration();
-    String mode = args[0];
-    
-    if (mode.equalsIgnoreCase("label")) {
-      if (args.length < 4) {
-        printUsage();
-        return;
-      }
-
-      String featureDataPath = args[1];
-      String resultDataPath = args[2];
-      String modelPath = args[3];
-
-      SmallLayeredNeuralNetwork ann = new SmallLayeredNeuralNetwork(conf, modelPath);
-
-      // process data in streaming approach
-      FileSystem fs = FileSystem.get(new URI(featureDataPath), conf);
-      BufferedReader br = new BufferedReader(new InputStreamReader(
-          fs.open(new Path(featureDataPath))));
-      Path outputPath = new Path(resultDataPath);
-      if (fs.exists(outputPath)) {
-        fs.delete(outputPath, true);
-      }
-      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
-          fs.create(outputPath)));
-
-      String line = null;
-
-      while ((line = br.readLine()) != null) {
-        if (line.trim().length() == 0) {
-          continue;
-        }
-        String[] tokens = line.trim().split(",");
-        double[] vals = new double[tokens.length];
-        for (int i = 0; i < tokens.length; ++i) {
-          vals[i] = Double.parseDouble(tokens[i]);
-        }
-        DoubleVector instance = new DenseDoubleVector(vals);
-        DoubleVector result = ann.getOutput(instance);
-        double[] arrResult = result.toArray();
-        StringBuilder sb = new StringBuilder();
-        for (int i = 0; i < arrResult.length; ++i) {
-          sb.append(arrResult[i]);
-          if (i != arrResult.length - 1) {
-            sb.append(",");
-          } else {
-            sb.append("\n");
-          }
-        }
-        bw.write(sb.toString());
-      }
-
-      br.close();
-      bw.close();
-    } else if (mode.equals("train")) {
-      if (args.length < 5) {
-        printUsage();
-        return;
-      }
-
-      String trainingDataPath = args[1];
-      String trainedModelPath = args[2];
-
-      int featureDimension = Integer.parseInt(args[3]);
-      int labelDimension = Integer.parseInt(args[4]);
-
-      int iteration = 1000;
-      double learningRate = 0.4;
-      double momemtumWeight = 0.2;
-      double regularizationWeight = 0.01;
-
-      // parse parameters
-      if (args.length >= 6) {
-        try {
-          iteration = Integer.parseInt(args[5]);
-          System.out.printf("Iteration: %d\n", iteration);
-        } catch (NumberFormatException e) {
-          System.err
-              .println("MAX_ITERATION format invalid. It should be a positive number.");
-          return;
-        }
-      }
-      if (args.length >= 7) {
-        try {
-          learningRate = Double.parseDouble(args[6]);
-          System.out.printf("Learning rate: %f\n", learningRate);
-        } catch (NumberFormatException e) {
-          System.err
-              .println("LEARNING_RATE format invalid. It should be a positive double in range (0, 1.0)");
-          return;
-        }
-      }
-      if (args.length >= 8) {
-        try {
-          momemtumWeight = Double.parseDouble(args[7]);
-          System.out.printf("Momemtum weight: %f\n", momemtumWeight);
-        } catch (NumberFormatException e) {
-          System.err
-              .println("MOMEMTUM_WEIGHT format invalid. It should be a positive double in range (0, 1.0)");
-          return;
-        }
-      }
-      if (args.length >= 9) {
-        try {
-          regularizationWeight = Double.parseDouble(args[8]);
-          System.out
-              .printf("Regularization weight: %f\n", regularizationWeight);
-        } catch (NumberFormatException e) {
-          System.err
-              .println("REGULARIZATION_WEIGHT format invalid. It should be a positive double in range (0, 1.0)");
-          return;
-        }
-      }
-
-      // train the model
-      SmallLayeredNeuralNetwork ann = new SmallLayeredNeuralNetwork();
-      // ann.setLearningRate(learningRate);
-      // ann.setMomemtumWeight(momemtumWeight);
-      // ann.setRegularizationWeight(regularizationWeight);
-      ann.addLayer(featureDimension, false,
-          FunctionFactory.createDoubleFunction("Sigmoid"));
-      ann.addLayer(featureDimension, false,
-          FunctionFactory.createDoubleFunction("Sigmoid"));
-      ann.addLayer(labelDimension, true,
-          FunctionFactory.createDoubleFunction("Sigmoid"));
-      ann.setCostFunction(FunctionFactory
-          .createDoubleDoubleFunction("CrossEntropy"));
-      ann.setModelPath(trainedModelPath);
-
-      Map<String, String> trainingParameters = new HashMap<String, String>();
-      trainingParameters.put("tasks", "2");
-      trainingParameters.put("training.max.iterations", "" + iteration);
-      trainingParameters.put("training.batch.size", "300");
-      trainingParameters.put("convergence.check.interval", "1000");
-      // ann.train(conf, new Path(trainingDataPath), trainingParameters);
-    }
-
-  }
-
-  private static void printUsage() {
-    System.out
-        .println("USAGE: <MODE> <INPUT_PATH> <OUTPUT_PATH> <MODEL_PATH>|<FEATURE_DIMENSION> <LABEL_DIMENSION> [<MAX_ITERATION> <LEARNING_RATE> <MOMEMTUM_WEIGHT> <REGULARIZATION_WEIGHT>]");
-    System.out
-        .println("\tMODE\t- train: train the model with given training data.");
-    System.out
-        .println("\t\t- label: obtain the result by feeding the features to the neural network.");
-    System.out
-        .println("\tINPUT_PATH\tin 'train' mode, it is the path of the training data; in 'label' mode, it is the path of the to be evaluated data that lacks the label.");
-    System.out
-        .println("\tOUTPUT_PATH\tin 'train' mode, it is where the trained model is stored; in 'label' mode, it is where the labeled data is stored.");
-    System.out.println("\n\tConditional Parameters:");
-    System.out
-        .println("\tMODEL_PATH\tonly required in 'label' mode. It specifies where to load the trained neural network model.");
-    System.out
-        .println("\tMAX_ITERATION\tonly used in 'train' mode. It specifies how many iterations for the neural network to run. Default is 0.01.");
-    System.out
-        .println("\tLEARNING_RATE\tonly used to 'train' mode. It specifies the degree of aggregation for learning, usually in range (0, 1.0). Default is 0.1.");
-    System.out
-        .println("\tMOMEMTUM_WEIGHT\tonly used to 'train' mode. It specifies the weight of momemtum. Default is 0.");
-    System.out
-        .println("\tREGULARIZATION_WEIGHT\tonly required in 'train' model. It specifies the weight of reqularization.");
-    System.out.println("\nExample:");
-    System.out
-        .println("Train a neural network with with feature dimension 8, label dimension 1 and default setting:\n\tneuralnets train hdfs://localhost:30002/training_data hdfs://localhost:30002/model 8 1");
-    System.out
-        .println("Train a neural network with with feature dimension 8, label dimension 1 and specify learning rate as 0.1, momemtum rate as 0.2, and regularization weight as 0.01:\n\tneuralnets.train hdfs://localhost:30002/training_data hdfs://localhost:30002/model 8 1 0.1 0.2 0.01");
-    System.out
-        .println("Label the data with trained model:\n\tneuralnets evaluate hdfs://localhost:30002/unlabeled_data hdfs://localhost:30002/result hdfs://localhost:30002/model");
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/ac8eaf8e/src/test/java/org/apache/horn/bsp/MLTestBase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/horn/bsp/MLTestBase.java b/src/test/java/org/apache/horn/bsp/MLTestBase.java
deleted file mode 100644
index 8001bcf..0000000
--- a/src/test/java/org/apache/horn/bsp/MLTestBase.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/**
- * 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.horn.bsp;
-
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * The common methods for testing machine learning algorithms
- *
- */
-public abstract class MLTestBase {
-
-  /**
-   * Conduct the 0-1 normalization.
-   * 
-   * @param instances
-   */
-  protected static void zeroOneNormalization(List<double[]> instanceList,
-      int len) {
-    int dimension = len;
-
-    double[] mins = new double[dimension];
-    double[] maxs = new double[dimension];
-    Arrays.fill(mins, Double.MAX_VALUE);
-    Arrays.fill(maxs, Double.MIN_VALUE);
-
-    for (double[] instance : instanceList) {
-      for (int i = 0; i < len; ++i) {
-        if (mins[i] > instance[i]) {
-          mins[i] = instance[i];
-        }
-        if (maxs[i] < instance[i]) {
-          maxs[i] = instance[i];
-        }
-      }
-    }
-
-    for (double[] instance : instanceList) {
-      for (int i = 0; i < len; ++i) {
-        double range = maxs[i] - mins[i];
-        if (range != 0) {
-          instance[i] = (instance[i] - mins[i]) / range;
-        }
-      }
-    }
-
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/ac8eaf8e/src/test/java/org/apache/horn/bsp/TestAutoEncoder.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/horn/bsp/TestAutoEncoder.java b/src/test/java/org/apache/horn/bsp/TestAutoEncoder.java
deleted file mode 100644
index a42fd72..0000000
--- a/src/test/java/org/apache/horn/bsp/TestAutoEncoder.java
+++ /dev/null
@@ -1,202 +0,0 @@
-/**
- * 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.horn.bsp;
-
-import static org.junit.Assert.assertEquals;
-
-import java.io.BufferedReader;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Random;
-
-import org.apache.hadoop.fs.FileSystem;
-import org.apache.hadoop.fs.Path;
-import org.apache.hadoop.io.LongWritable;
-import org.apache.hadoop.io.SequenceFile;
-import org.apache.hama.HamaConfiguration;
-import org.apache.hama.commons.io.VectorWritable;
-import org.apache.hama.commons.math.DenseDoubleVector;
-import org.apache.hama.commons.math.DoubleVector;
-import org.junit.Test;
-import org.mortbay.log.Log;
-
-/**
- * Test the functionality of {@link AutoEncoder}.
- * 
- */
-public class TestAutoEncoder extends MLTestBase {
-
-  @Test
-  public void testAutoEncoderSimple() {
-    double[][] instances = { { 0, 0, 0, 1 }, { 0, 0, 1, 0 }, { 0, 1, 0, 0 },
-        { 0, 0, 0, 0 } };
-    AutoEncoder encoder = new AutoEncoder(4, 2);
-    // TODO use the configuration
-
-    // encoder.setLearningRate(0.5);
-    // encoder.setMomemtumWeight(0.2);
-
-    int maxIteration = 2000;
-    Random rnd = new Random();
-    for (int iteration = 0; iteration < maxIteration; ++iteration) {
-      for (int i = 0; i < instances.length; ++i) {
-        encoder.trainOnline(new DenseDoubleVector(instances[rnd
-            .nextInt(instances.length)]));
-      }
-    }
-
-    for (int i = 0; i < instances.length; ++i) {
-      DoubleVector encodeVec = encoder.encode(new DenseDoubleVector(
-          instances[i]));
-      DoubleVector decodeVec = encoder.decode(encodeVec);
-      for (int d = 0; d < instances[i].length; ++d) {
-        assertEquals(instances[i][d], decodeVec.get(d), 0.1);
-      }
-    }
-
-  }
-
-  @Test
-  public void testAutoEncoderSwissRollDataset() {
-    List<double[]> instanceList = new ArrayList<double[]>();
-    try {
-      BufferedReader br = new BufferedReader(new FileReader(
-          "src/test/resources/dimensional_reduction.txt"));
-      String line = null;
-      while ((line = br.readLine()) != null) {
-        String[] tokens = line.split("\t");
-        double[] instance = new double[tokens.length];
-        for (int i = 0; i < instance.length; ++i) {
-          instance[i] = Double.parseDouble(tokens[i]);
-        }
-        instanceList.add(instance);
-      }
-      br.close();
-      // normalize instances
-      zeroOneNormalization(instanceList, instanceList.get(0).length);
-    } catch (FileNotFoundException e) {
-      e.printStackTrace();
-    } catch (NumberFormatException e) {
-      e.printStackTrace();
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-
-    List<DoubleVector> vecInstanceList = new ArrayList<DoubleVector>();
-    for (double[] instance : instanceList) {
-      vecInstanceList.add(new DenseDoubleVector(instance));
-    }
-    AutoEncoder encoder = new AutoEncoder(3, 2);
-    // encoder.setLearningRate(0.05);
-    // encoder.setMomemtumWeight(0.1);
-    int maxIteration = 2000;
-    for (int iteration = 0; iteration < maxIteration; ++iteration) {
-      for (DoubleVector vector : vecInstanceList) {
-        encoder.trainOnline(vector);
-      }
-    }
-
-    double errorInstance = 0;
-    for (DoubleVector vector : vecInstanceList) {
-      DoubleVector decoded = encoder.getOutput(vector);
-      DoubleVector diff = vector.subtract(decoded);
-      double error = diff.dot(diff);
-      if (error > 0.1) {
-        ++errorInstance;
-      }
-    }
-    Log.info(String.format("Autoecoder error rate: %f%%\n", errorInstance * 100
-        / vecInstanceList.size()));
-
-  }
-
-  @Test
-  public void testAutoEncoderSwissRollDatasetDistributed() {
-    HamaConfiguration conf = new HamaConfiguration();
-    String strDataPath = "/tmp/dimensional_reduction.txt";
-    Path path = new Path(strDataPath);
-    List<double[]> instanceList = new ArrayList<double[]>();
-    try {
-      FileSystem fs = FileSystem.get(new URI(strDataPath), conf);
-      if (fs.exists(path)) {
-        fs.delete(path, true);
-      }
-
-      String line = null;
-      BufferedReader br = new BufferedReader(new FileReader(
-          "src/test/resources/dimensional_reduction.txt"));
-      while ((line = br.readLine()) != null) {
-        String[] tokens = line.split("\t");
-        double[] instance = new double[tokens.length];
-        for (int i = 0; i < instance.length; ++i) {
-          instance[i] = Double.parseDouble(tokens[i]);
-        }
-        instanceList.add(instance);
-      }
-      br.close();
-      // normalize instances
-      zeroOneNormalization(instanceList, instanceList.get(0).length);
-
-      SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, path,
-          LongWritable.class, VectorWritable.class);
-      for (int i = 0; i < instanceList.size(); ++i) {
-        DoubleVector vector = new DenseDoubleVector(instanceList.get(i));
-        writer.append(new LongWritable(i), new VectorWritable(vector));
-      }
-
-      writer.close();
-    } catch (FileNotFoundException e) {
-      e.printStackTrace();
-    } catch (IOException e) {
-      e.printStackTrace();
-    } catch (URISyntaxException e) {
-      e.printStackTrace();
-    }
-
-    AutoEncoder encoder = new AutoEncoder(3, 2);
-    String modelPath = "/tmp/autoencoder-modelpath";
-    encoder.setModelPath(modelPath);
-    Map<String, String> trainingParams = new HashMap<String, String>();
-    // encoder.setLearningRate(0.5);
-    trainingParams.put("tasks", "5");
-    trainingParams.put("training.max.iterations", "3000");
-    trainingParams.put("training.batch.size", "200");
-    // encoder.train(conf, path, trainingParams);
-
-    double errorInstance = 0;
-    for (double[] instance : instanceList) {
-      DoubleVector vector = new DenseDoubleVector(instance);
-      DoubleVector decoded = encoder.getOutput(vector);
-      DoubleVector diff = vector.subtract(decoded);
-      double error = diff.dot(diff);
-      if (error > 0.1) {
-        ++errorInstance;
-      }
-    }
-    Log.info(String.format("Autoecoder error rate: %f%%\n", errorInstance * 100
-        / instanceList.size()));
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/ac8eaf8e/src/test/java/org/apache/horn/bsp/TestSmallLayeredNeuralNetwork.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/horn/bsp/TestSmallLayeredNeuralNetwork.java b/src/test/java/org/apache/horn/bsp/TestSmallLayeredNeuralNetwork.java
deleted file mode 100644
index ee48136..0000000
--- a/src/test/java/org/apache/horn/bsp/TestSmallLayeredNeuralNetwork.java
+++ /dev/null
@@ -1,642 +0,0 @@
-/**
- * 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.horn.bsp;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-
-import java.io.BufferedReader;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.fs.FileSystem;
-import org.apache.hadoop.fs.Path;
-import org.apache.hadoop.io.LongWritable;
-import org.apache.hadoop.io.SequenceFile;
-import org.apache.hama.HamaConfiguration;
-import org.apache.hama.commons.io.VectorWritable;
-import org.apache.hama.commons.math.DenseDoubleMatrix;
-import org.apache.hama.commons.math.DenseDoubleVector;
-import org.apache.hama.commons.math.DoubleMatrix;
-import org.apache.hama.commons.math.DoubleVector;
-import org.apache.hama.ml.util.DefaultFeatureTransformer;
-import org.apache.hama.ml.util.FeatureTransformer;
-import org.apache.horn.bsp.AbstractLayeredNeuralNetwork.LearningStyle;
-import org.apache.horn.bsp.AbstractLayeredNeuralNetwork.TrainingMethod;
-import org.apache.horn.funcs.FunctionFactory;
-import org.junit.Test;
-import org.mortbay.log.Log;
-
-/**
- * Test the functionality of SmallLayeredNeuralNetwork.
- * 
- */
-public class TestSmallLayeredNeuralNetwork extends MLTestBase {
-
-  @Test
-  public void testReadWrite() {
-    SmallLayeredNeuralNetwork ann = new SmallLayeredNeuralNetwork();
-    ann.addLayer(2, false,
-        FunctionFactory.createDoubleFunction("IdentityFunction"));
-    ann.addLayer(5, false,
-        FunctionFactory.createDoubleFunction("IdentityFunction"));
-    ann.addLayer(1, true,
-        FunctionFactory.createDoubleFunction("IdentityFunction"));
-    ann.setCostFunction(FunctionFactory
-        .createDoubleDoubleFunction("SquaredError"));
-    double learningRate = 0.2;
-    // ann.setLearningRate(learningRate);
-    double momentumWeight = 0.5;
-    // ann.setMomemtumWeight(momentumWeight);
-    double regularizationWeight = 0.05;
-    //ann.setRegularizationWeight(regularizationWeight);
-    // intentionally initialize all weights to 0.5
-    DoubleMatrix[] matrices = new DenseDoubleMatrix[2];
-    matrices[0] = new DenseDoubleMatrix(5, 3, 0.2);
-    matrices[1] = new DenseDoubleMatrix(1, 6, 0.8);
-    ann.setWeightMatrices(matrices);
-    ann.setLearningStyle(LearningStyle.UNSUPERVISED);
-    
-    FeatureTransformer defaultFeatureTransformer = new DefaultFeatureTransformer();
-    ann.setFeatureTransformer(defaultFeatureTransformer);
-    
-
-    // write to file
-    String modelPath = "/tmp/testSmallLayeredNeuralNetworkReadWrite";
-    ann.setModelPath(modelPath);
-    try {
-      ann.writeModelToFile();
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-
-    // read from file
-    SmallLayeredNeuralNetwork annCopy = new SmallLayeredNeuralNetwork(new HamaConfiguration(), modelPath);
-    assertEquals(annCopy.getClass().getSimpleName(), annCopy.getModelType());
-    assertEquals(modelPath, annCopy.getModelPath());
-    // assertEquals(learningRate, annCopy.getLearningRate(), 0.000001);
-    // assertEquals(momentumWeight, annCopy.getMomemtumWeight(), 0.000001);
-    //assertEquals(regularizationWeight, annCopy.getRegularizationWeight(),
-    //    0.000001);
-    assertEquals(TrainingMethod.GRADIENT_DESCENT, annCopy.getTrainingMethod());
-    assertEquals(LearningStyle.UNSUPERVISED, annCopy.getLearningStyle());
-
-    // compare weights
-    DoubleMatrix[] weightsMatrices = annCopy.getWeightMatrices();
-    for (int i = 0; i < weightsMatrices.length; ++i) {
-      DoubleMatrix expectMat = matrices[i];
-      DoubleMatrix actualMat = weightsMatrices[i];
-      for (int j = 0; j < expectMat.getRowCount(); ++j) {
-        for (int k = 0; k < expectMat.getColumnCount(); ++k) {
-          assertEquals(expectMat.get(j, k), actualMat.get(j, k), 0.000001);
-        }
-      }
-    }
-    
-    FeatureTransformer copyTransformer = annCopy.getFeatureTransformer();
-    assertEquals(defaultFeatureTransformer.getClass().getName(), copyTransformer.getClass().getName());
-  }
-
-  @Test
-  /**
-   * Test the forward functionality.
-   */
-  public void testOutput() {
-    // first network
-    SmallLayeredNeuralNetwork ann = new SmallLayeredNeuralNetwork();
-    ann.addLayer(2, false,
-        FunctionFactory.createDoubleFunction("IdentityFunction"));
-    ann.addLayer(5, false,
-        FunctionFactory.createDoubleFunction("IdentityFunction"));
-    ann.addLayer(1, true,
-        FunctionFactory.createDoubleFunction("IdentityFunction"));
-    ann.setCostFunction(FunctionFactory
-        .createDoubleDoubleFunction("SquaredError"));
-    // ann.setLearningRate(0.1);
-    // intentionally initialize all weights to 0.5
-    DoubleMatrix[] matrices = new DenseDoubleMatrix[2];
-    matrices[0] = new DenseDoubleMatrix(5, 3, 0.5);
-    matrices[1] = new DenseDoubleMatrix(1, 6, 0.5);
-    ann.setWeightMatrices(matrices);
-
-    double[] arr = new double[] { 0, 1 };
-    DoubleVector training = new DenseDoubleVector(arr);
-    DoubleVector result = ann.getOutput(training);
-    assertEquals(1, result.getDimension());
-    // assertEquals(3, result.get(0), 0.000001);
-
-    // second network
-    SmallLayeredNeuralNetwork ann2 = new SmallLayeredNeuralNetwork();
-    ann2.addLayer(2, false, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann2.addLayer(3, false, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann2.addLayer(1, true, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann2.setCostFunction(FunctionFactory
-        .createDoubleDoubleFunction("SquaredError"));
-    // ann2.setLearningRate(0.3);
-    // intentionally initialize all weights to 0.5
-    DoubleMatrix[] matrices2 = new DenseDoubleMatrix[2];
-    matrices2[0] = new DenseDoubleMatrix(3, 3, 0.5);
-    matrices2[1] = new DenseDoubleMatrix(1, 4, 0.5);
-    ann2.setWeightMatrices(matrices2);
-
-    double[] test = { 0, 0 };
-    double[] result2 = { 0.807476 };
-
-    DoubleVector vec = ann2.getOutput(new DenseDoubleVector(test));
-    assertArrayEquals(result2, vec.toArray(), 0.000001);
-
-    SmallLayeredNeuralNetwork ann3 = new SmallLayeredNeuralNetwork();
-    ann3.addLayer(2, false, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann3.addLayer(3, false, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann3.addLayer(1, true, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann3.setCostFunction(FunctionFactory
-        .createDoubleDoubleFunction("SquaredError"));
-    // ann3.setLearningRate(0.3);
-    // intentionally initialize all weights to 0.5
-    DoubleMatrix[] initMatrices = new DenseDoubleMatrix[2];
-    initMatrices[0] = new DenseDoubleMatrix(3, 3, 0.5);
-    initMatrices[1] = new DenseDoubleMatrix(1, 4, 0.5);
-    ann3.setWeightMatrices(initMatrices);
-
-    double[] instance = { 0, 1 };
-    DoubleVector output = ann3.getOutput(new DenseDoubleVector(instance));
-    assertEquals(0.8315410, output.get(0), 0.000001);
-  }
-
-  @Test
-  public void testXORlocal() {
-    SmallLayeredNeuralNetwork ann = new SmallLayeredNeuralNetwork();
-    ann.addLayer(2, false, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(3, false, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(1, true, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.setCostFunction(FunctionFactory
-        .createDoubleDoubleFunction("SquaredError"));
-    // ann.setLearningRate(0.5);
-    // ann.setMomemtumWeight(0.0);
-
-    int iterations = 50000; // iteration should be set to a very large number
-    double[][] instances = { { 0, 1, 1 }, { 0, 0, 0 }, { 1, 0, 1 }, { 1, 1, 0 } };
-    for (int i = 0; i < iterations; ++i) {
-      DoubleMatrix[] matrices = null;
-      for (int j = 0; j < instances.length; ++j) {
-        matrices = ann.trainByInstance(new DenseDoubleVector(instances[j
-            % instances.length]));
-        ann.updateWeightMatrices(matrices);
-      }
-    }
-
-    for (int i = 0; i < instances.length; ++i) {
-      DoubleVector input = new DenseDoubleVector(instances[i]).slice(2);
-      // the expected output is the last element in array
-      double result = instances[i][2];
-      double actual = ann.getOutput(input).get(0);
-      if (result < 0.5 && actual >= 0.5 || result >= 0.5 && actual < 0.5) {
-        Log.info("Neural network failes to lear the XOR.");
-      }
-    }
-
-    // write model into file and read out
-    String modelPath = "/tmp/testSmallLayeredNeuralNetworkXORLocal";
-    ann.setModelPath(modelPath);
-    try {
-      ann.writeModelToFile();
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-    SmallLayeredNeuralNetwork annCopy = new SmallLayeredNeuralNetwork(new HamaConfiguration(), modelPath);
-    // test on instances
-    for (int i = 0; i < instances.length; ++i) {
-      DoubleVector input = new DenseDoubleVector(instances[i]).slice(2);
-      // the expected output is the last element in array
-      double result = instances[i][2];
-      double actual = annCopy.getOutput(input).get(0);
-      if (result < 0.5 && actual >= 0.5 || result >= 0.5 && actual < 0.5) {
-        Log.info("Neural network failes to lear the XOR.");
-      }
-    }
-  }
-
-  @Test
-  public void testXORWithMomentum() {
-    SmallLayeredNeuralNetwork ann = new SmallLayeredNeuralNetwork();
-    ann.addLayer(2, false, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(3, false, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(1, true, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.setCostFunction(FunctionFactory
-        .createDoubleDoubleFunction("SquaredError"));
-    // ann.setLearningRate(0.6);
-    // ann.setMomemtumWeight(0.3);
-
-    int iterations = 2000; // iteration should be set to a very large number
-    double[][] instances = { { 0, 1, 1 }, { 0, 0, 0 }, { 1, 0, 1 }, { 1, 1, 0 } };
-    for (int i = 0; i < iterations; ++i) {
-      for (int j = 0; j < instances.length; ++j) {
-        ann.trainOnline(new DenseDoubleVector(instances[j % instances.length]));
-      }
-    }
-
-    for (int i = 0; i < instances.length; ++i) {
-      DoubleVector input = new DenseDoubleVector(instances[i]).slice(2);
-      // the expected output is the last element in array
-      double result = instances[i][2];
-      double actual = ann.getOutput(input).get(0);
-      if (result < 0.5 && actual >= 0.5 || result >= 0.5 && actual < 0.5) {
-        Log.info("Neural network failes to lear the XOR.");
-      }
-    }
-
-    // write model into file and read out
-    String modelPath = "/tmp/testSmallLayeredNeuralNetworkXORLocalWithMomentum";
-    ann.setModelPath(modelPath);
-    try {
-      ann.writeModelToFile();
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-    SmallLayeredNeuralNetwork annCopy = new SmallLayeredNeuralNetwork(new HamaConfiguration(), modelPath);
-    // test on instances
-    for (int i = 0; i < instances.length; ++i) {
-      DoubleVector input = new DenseDoubleVector(instances[i]).slice(2);
-      // the expected output is the last element in array
-      double result = instances[i][2];
-      double actual = annCopy.getOutput(input).get(0);
-      if (result < 0.5 && actual >= 0.5 || result >= 0.5 && actual < 0.5) {
-        Log.info("Neural network failes to lear the XOR.");
-      }
-    }
-  }
-
-  @Test
-  public void testXORLocalWithRegularization() {
-    SmallLayeredNeuralNetwork ann = new SmallLayeredNeuralNetwork();
-    ann.addLayer(2, false, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(3, false, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(1, true, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.setCostFunction(FunctionFactory
-        .createDoubleDoubleFunction("SquaredError"));
-    // ann.setLearningRate(0.7);
-    // ann.setMomemtumWeight(0.5);
-    //ann.setRegularizationWeight(0.002);
-
-    int iterations = 5000; // iteration should be set to a very large number
-    double[][] instances = { { 0, 1, 1 }, { 0, 0, 0 }, { 1, 0, 1 }, { 1, 1, 0 } };
-    for (int i = 0; i < iterations; ++i) {
-      for (int j = 0; j < instances.length; ++j) {
-        ann.trainOnline(new DenseDoubleVector(instances[j % instances.length]));
-      }
-    }
-
-    for (int i = 0; i < instances.length; ++i) {
-      DoubleVector input = new DenseDoubleVector(instances[i]).slice(2);
-      // the expected output is the last element in array
-      double result = instances[i][2];
-      double actual = ann.getOutput(input).get(0);
-      if (result < 0.5 && actual >= 0.5 || result >= 0.5 && actual < 0.5) {
-        Log.info("Neural network failes to lear the XOR.");
-      }
-    }
-
-    // write model into file and read out
-    String modelPath = "/tmp/testSmallLayeredNeuralNetworkXORLocalWithRegularization";
-    ann.setModelPath(modelPath);
-    try {
-      ann.writeModelToFile();
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-    SmallLayeredNeuralNetwork annCopy = new SmallLayeredNeuralNetwork(new HamaConfiguration(), modelPath);
-    // test on instances
-    for (int i = 0; i < instances.length; ++i) {
-      DoubleVector input = new DenseDoubleVector(instances[i]).slice(2);
-      // the expected output is the last element in array
-      double result = instances[i][2];
-      double actual = annCopy.getOutput(input).get(0);
-      if (result < 0.5 && actual >= 0.5 || result >= 0.5 && actual < 0.5) {
-        Log.info("Neural network failes to lear the XOR.");
-      }
-    }
-  }
-
-  @Test
-  public void testTwoClassClassification() {
-    // use logistic regression data
-    String filepath = "src/test/resources/logistic_regression_data.txt";
-    List<double[]> instanceList = new ArrayList<double[]>();
-
-    try {
-      BufferedReader br = new BufferedReader(new FileReader(filepath));
-      String line = null;
-      while ((line = br.readLine()) != null) {
-        String[] tokens = line.trim().split(",");
-        double[] instance = new double[tokens.length];
-        for (int i = 0; i < tokens.length; ++i) {
-          instance[i] = Double.parseDouble(tokens[i]);
-        }
-        instanceList.add(instance);
-      }
-      br.close();
-    } catch (FileNotFoundException e) {
-      e.printStackTrace();
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-
-    zeroOneNormalization(instanceList, instanceList.get(0).length - 1);
-    
-    int dimension = instanceList.get(0).length - 1;
-
-    // divide dataset into training and testing
-    List<double[]> testInstances = new ArrayList<double[]>();
-    testInstances.addAll(instanceList.subList(instanceList.size() - 100,
-        instanceList.size()));
-    List<double[]> trainingInstances = instanceList.subList(0,
-        instanceList.size() - 100);
-
-    SmallLayeredNeuralNetwork ann = new SmallLayeredNeuralNetwork();
-    // ann.setLearningRate(0.001);
-    // ann.setMomemtumWeight(0.1);
-    //ann.setRegularizationWeight(0.01);
-    ann.addLayer(dimension, false,
-        FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(dimension, false,
-        FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(dimension, false,
-        FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(1, true, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.setCostFunction(FunctionFactory
-        .createDoubleDoubleFunction("CrossEntropy"));
-
-    long start = new Date().getTime();
-    int iterations = 1000;
-    for (int i = 0; i < iterations; ++i) {
-      for (double[] trainingInstance : trainingInstances) {
-        ann.trainOnline(new DenseDoubleVector(trainingInstance));
-      }
-    }
-    long end = new Date().getTime();
-    Log.info(String.format("Training time: %fs\n",
-        (double) (end - start) / 1000));
-
-    double errorRate = 0;
-    // calculate the error on test instance
-    for (double[] testInstance : testInstances) {
-      DoubleVector instance = new DenseDoubleVector(testInstance);
-      double expected = instance.get(instance.getDimension() - 1);
-      instance = instance.slice(instance.getDimension() - 1);
-      double actual = ann.getOutput(instance).get(0);
-      if (actual < 0.5 && expected >= 0.5 || actual >= 0.5 && expected < 0.5) {
-        ++errorRate;
-      }
-    }
-    errorRate /= testInstances.size();
-
-    Log.info(String.format("Relative error: %f%%\n", errorRate * 100));
-  }
-  
-  @Test
-  public void testLogisticRegression() {
-    this.testLogisticRegressionDistributedVersion();
-    this.testLogisticRegressionDistributedVersionWithFeatureTransformer();
-  }
-
-  public void testLogisticRegressionDistributedVersion() {
-    // write data into a sequence file
-    String tmpStrDatasetPath = "/tmp/logistic_regression_data";
-    Path tmpDatasetPath = new Path(tmpStrDatasetPath);
-    String strDataPath = "src/test/resources/logistic_regression_data.txt";
-    String modelPath = "/tmp/logistic-regression-distributed-model";
-
-    Configuration conf = new Configuration();
-    List<double[]> instanceList = new ArrayList<double[]>();
-    List<double[]> trainingInstances = null;
-    List<double[]> testInstances = null;
-
-    try {
-      FileSystem fs = FileSystem.get(new URI(tmpStrDatasetPath), conf);
-      fs.delete(tmpDatasetPath, true);
-      if (fs.exists(tmpDatasetPath)) {
-        fs.createNewFile(tmpDatasetPath);
-      }
-
-      BufferedReader br = new BufferedReader(new FileReader(strDataPath));
-      String line = null;
-      int count = 0;
-      while ((line = br.readLine()) != null) {
-        String[] tokens = line.trim().split(",");
-        double[] instance = new double[tokens.length];
-        for (int i = 0; i < tokens.length; ++i) {
-          instance[i] = Double.parseDouble(tokens[i]);
-        }
-        instanceList.add(instance);
-      }
-      br.close();
-      
-      zeroOneNormalization(instanceList, instanceList.get(0).length - 1);
-      
-      // write training data to temporal sequence file
-      SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
-          tmpDatasetPath, LongWritable.class, VectorWritable.class);
-      int testSize = 150;
-
-      Collections.shuffle(instanceList);
-      testInstances = new ArrayList<double[]>();
-      testInstances.addAll(instanceList.subList(instanceList.size() - testSize,
-          instanceList.size()));
-      trainingInstances = instanceList.subList(0, instanceList.size()
-          - testSize);
-
-      for (double[] instance : trainingInstances) {
-        DoubleVector vec = new DenseDoubleVector(instance);
-        writer.append(new LongWritable(count++), new VectorWritable(vec));
-      }
-      writer.close();
-    } catch (FileNotFoundException e) {
-      e.printStackTrace();
-    } catch (IOException e) {
-      e.printStackTrace();
-    } catch (URISyntaxException e) {
-      e.printStackTrace();
-    }
-
-    // create model
-    int dimension = 8;
-    SmallLayeredNeuralNetwork ann = new SmallLayeredNeuralNetwork();
-    // ann.setLearningRate(0.7);
-    // ann.setMomemtumWeight(0.5);
-    //ann.setRegularizationWeight(0.1);
-    ann.addLayer(dimension, false,
-        FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(dimension, false,
-        FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(dimension, false,
-        FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(1, true, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.setCostFunction(FunctionFactory
-        .createDoubleDoubleFunction("CrossEntropy"));
-    ann.setModelPath(modelPath);
-
-    long start = new Date().getTime();
-    Map<String, String> trainingParameters = new HashMap<String, String>();
-    trainingParameters.put("tasks", "5");
-    trainingParameters.put("training.max.iterations", "2000");
-    trainingParameters.put("training.batch.size", "300");
-    trainingParameters.put("convergence.check.interval", "1000");
-    //ann.train(new HamaConfiguration(), tmpDatasetPath, trainingParameters);
-
-    long end = new Date().getTime();
-
-    // validate results
-    double errorRate = 0;
-    // calculate the error on test instance
-    for (double[] testInstance : testInstances) {
-      DoubleVector instance = new DenseDoubleVector(testInstance);
-      double expected = instance.get(instance.getDimension() - 1);
-      instance = instance.slice(instance.getDimension() - 1);
-      double actual = ann.getOutput(instance).get(0);
-      if (actual < 0.5 && expected >= 0.5 || actual >= 0.5 && expected < 0.5) {
-        ++errorRate;
-      }
-    }
-    errorRate /= testInstances.size();
-
-    Log.info(String.format("Training time: %fs\n",
-        (double) (end - start) / 1000));
-    Log.info(String.format("Relative error: %f%%\n", errorRate * 100));
-  }
-
-  public void testLogisticRegressionDistributedVersionWithFeatureTransformer() {
-    // write data into a sequence file
-    String tmpStrDatasetPath = "/tmp/logistic_regression_data_feature_transformer";
-    Path tmpDatasetPath = new Path(tmpStrDatasetPath);
-    String strDataPath = "src/test/resources/logistic_regression_data.txt";
-    String modelPath = "/tmp/logistic-regression-distributed-model-feature-transformer";
-
-    Configuration conf = new Configuration();
-    List<double[]> instanceList = new ArrayList<double[]>();
-    List<double[]> trainingInstances = null;
-    List<double[]> testInstances = null;
-
-    try {
-      FileSystem fs = FileSystem.get(new URI(tmpStrDatasetPath), conf);
-      fs.delete(tmpDatasetPath, true);
-      if (fs.exists(tmpDatasetPath)) {
-        fs.createNewFile(tmpDatasetPath);
-      }
-
-      BufferedReader br = new BufferedReader(new FileReader(strDataPath));
-      String line = null;
-      int count = 0;
-      while ((line = br.readLine()) != null) {
-        String[] tokens = line.trim().split(",");
-        double[] instance = new double[tokens.length];
-        for (int i = 0; i < tokens.length; ++i) {
-          instance[i] = Double.parseDouble(tokens[i]);
-        }
-        instanceList.add(instance);
-      }
-      br.close();
-      
-      zeroOneNormalization(instanceList, instanceList.get(0).length - 1);
-      
-      // write training data to temporal sequence file
-      SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
-          tmpDatasetPath, LongWritable.class, VectorWritable.class);
-      int testSize = 150;
-
-      Collections.shuffle(instanceList);
-      testInstances = new ArrayList<double[]>();
-      testInstances.addAll(instanceList.subList(instanceList.size() - testSize,
-          instanceList.size()));
-      trainingInstances = instanceList.subList(0, instanceList.size()
-          - testSize);
-
-      for (double[] instance : trainingInstances) {
-        DoubleVector vec = new DenseDoubleVector(instance);
-        writer.append(new LongWritable(count++), new VectorWritable(vec));
-      }
-      writer.close();
-    } catch (FileNotFoundException e) {
-      e.printStackTrace();
-    } catch (IOException e) {
-      e.printStackTrace();
-    } catch (URISyntaxException e) {
-      e.printStackTrace();
-    }
-
-    // create model
-    int dimension = 8;
-    SmallLayeredNeuralNetwork ann = new SmallLayeredNeuralNetwork();
-    // ann.setLearningRate(0.7);
-    // ann.setMomemtumWeight(0.5);
-    //ann.setRegularizationWeight(0.1);
-    ann.addLayer(dimension, false,
-        FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(dimension, false,
-        FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(dimension, false,
-        FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.addLayer(1, true, FunctionFactory.createDoubleFunction("Sigmoid"));
-    ann.setCostFunction(FunctionFactory
-        .createDoubleDoubleFunction("CrossEntropy"));
-    ann.setModelPath(modelPath);
-    
-    FeatureTransformer featureTransformer = new DefaultFeatureTransformer();
-    
-    ann.setFeatureTransformer(featureTransformer);
-
-    long start = new Date().getTime();
-    Map<String, String> trainingParameters = new HashMap<String, String>();
-    trainingParameters.put("tasks", "5");
-    trainingParameters.put("training.max.iterations", "2000");
-    trainingParameters.put("training.batch.size", "300");
-    trainingParameters.put("convergence.check.interval", "1000");
-    //ann.train(new HamaConfiguration(), tmpDatasetPath, trainingParameters);
-
-    long end = new Date().getTime();
-
-    // validate results
-    double errorRate = 0;
-    // calculate the error on test instance
-    for (double[] testInstance : testInstances) {
-      DoubleVector instance = new DenseDoubleVector(testInstance);
-      double expected = instance.get(instance.getDimension() - 1);
-      instance = instance.slice(instance.getDimension() - 1);
-      instance = featureTransformer.transform(instance);
-      double actual = ann.getOutput(instance).get(0);
-      if (actual < 0.5 && expected >= 0.5 || actual >= 0.5 && expected < 0.5) {
-        ++errorRate;
-      }
-    }
-    errorRate /= testInstances.size();
-
-    Log.info(String.format("Training time: %fs\n",
-        (double) (end - start) / 1000));
-    Log.info(String.format("Relative error: %f%%\n", errorRate * 100));
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/ac8eaf8e/src/test/java/org/apache/horn/bsp/TestSmallLayeredNeuralNetworkMessage.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/horn/bsp/TestSmallLayeredNeuralNetworkMessage.java b/src/test/java/org/apache/horn/bsp/TestSmallLayeredNeuralNetworkMessage.java
deleted file mode 100644
index e422d95..0000000
--- a/src/test/java/org/apache/horn/bsp/TestSmallLayeredNeuralNetworkMessage.java
+++ /dev/null
@@ -1,172 +0,0 @@
-/**
- * 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.horn.bsp;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
-
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.fs.FSDataInputStream;
-import org.apache.hadoop.fs.FSDataOutputStream;
-import org.apache.hadoop.fs.FileSystem;
-import org.apache.hadoop.fs.Path;
-import org.apache.hama.commons.math.DenseDoubleMatrix;
-import org.apache.hama.commons.math.DoubleMatrix;
-import org.junit.Test;
-
-/**
- * Test the functionalities of SmallLayeredNeuralNetworkMessage.
- * 
- */
-public class TestSmallLayeredNeuralNetworkMessage {
-
-  @Test
-  public void testReadWriteWithoutPrev() {
-    double error = 0.22;
-    double[][] matrix1 = new double[][] { { 0.1, 0.2, 0.8, 0.5 },
-        { 0.3, 0.4, 0.6, 0.2 }, { 0.5, 0.6, 0.1, 0.5 } };
-    double[][] matrix2 = new double[][] { { 0.8, 1.2, 0.5 } };
-    DoubleMatrix[] matrices = new DoubleMatrix[2];
-    matrices[0] = new DenseDoubleMatrix(matrix1);
-    matrices[1] = new DenseDoubleMatrix(matrix2);
-
-    boolean isConverge = false;
-
-    SmallLayeredNeuralNetworkMessage message = new SmallLayeredNeuralNetworkMessage(
-        error, isConverge, matrices, null);
-    Configuration conf = new Configuration();
-    String strPath = "/tmp/testReadWriteSmallLayeredNeuralNetworkMessage";
-    Path path = new Path(strPath);
-    try {
-      FileSystem fs = FileSystem.get(new URI(strPath), conf);
-      FSDataOutputStream out = fs.create(path);
-      message.write(out);
-      out.close();
-
-      FSDataInputStream in = fs.open(path);
-      SmallLayeredNeuralNetworkMessage readMessage = new SmallLayeredNeuralNetworkMessage(
-          0, isConverge, null, null);
-      readMessage.readFields(in);
-      in.close();
-      assertEquals(error, readMessage.getTrainingError(), 0.000001);
-      assertFalse(readMessage.isConverge());
-      DoubleMatrix[] readMatrices = readMessage.getCurMatrices();
-      assertEquals(2, readMatrices.length);
-      for (int i = 0; i < readMatrices.length; ++i) {
-        double[][] doubleMatrices = ((DenseDoubleMatrix) readMatrices[i])
-            .getValues();
-        double[][] doubleExpected = ((DenseDoubleMatrix) matrices[i])
-            .getValues();
-        for (int r = 0; r < doubleMatrices.length; ++r) {
-          assertArrayEquals(doubleExpected[r], doubleMatrices[r], 0.000001);
-        }
-      }
-
-      DoubleMatrix[] readPrevMatrices = readMessage.getPrevMatrices();
-      assertNull(readPrevMatrices);
-
-      // delete
-      fs.delete(path, true);
-    } catch (IOException e) {
-      e.printStackTrace();
-    } catch (URISyntaxException e) {
-      e.printStackTrace();
-    }
-  }
-
-  @Test
-  public void testReadWriteWithPrev() {
-    double error = 0.22;
-    boolean isConverge = true;
-
-    double[][] matrix1 = new double[][] { { 0.1, 0.2, 0.8, 0.5 },
-        { 0.3, 0.4, 0.6, 0.2 }, { 0.5, 0.6, 0.1, 0.5 } };
-    double[][] matrix2 = new double[][] { { 0.8, 1.2, 0.5 } };
-    DoubleMatrix[] matrices = new DoubleMatrix[2];
-    matrices[0] = new DenseDoubleMatrix(matrix1);
-    matrices[1] = new DenseDoubleMatrix(matrix2);
-
-    double[][] prevMatrix1 = new double[][] { { 0.1, 0.1, 0.2, 0.3 },
-        { 0.2, 0.4, 0.1, 0.5 }, { 0.5, 0.1, 0.5, 0.2 } };
-    double[][] prevMatrix2 = new double[][] { { 0.1, 0.2, 0.5, 0.9 },
-        { 0.3, 0.5, 0.2, 0.6 }, { 0.6, 0.8, 0.7, 0.5 } };
-
-    DoubleMatrix[] prevMatrices = new DoubleMatrix[2];
-    prevMatrices[0] = new DenseDoubleMatrix(prevMatrix1);
-    prevMatrices[1] = new DenseDoubleMatrix(prevMatrix2);
-
-    SmallLayeredNeuralNetworkMessage message = new SmallLayeredNeuralNetworkMessage(
-        error, isConverge, matrices, prevMatrices);
-    Configuration conf = new Configuration();
-    String strPath = "/tmp/testReadWriteSmallLayeredNeuralNetworkMessageWithPrev";
-    Path path = new Path(strPath);
-    try {
-      FileSystem fs = FileSystem.get(new URI(strPath), conf);
-      FSDataOutputStream out = fs.create(path);
-      message.write(out);
-      out.close();
-
-      FSDataInputStream in = fs.open(path);
-      SmallLayeredNeuralNetworkMessage readMessage = new SmallLayeredNeuralNetworkMessage(
-          0, isConverge, null, null);
-      readMessage.readFields(in);
-      in.close();
-
-      assertTrue(readMessage.isConverge());
-
-      DoubleMatrix[] readMatrices = readMessage.getCurMatrices();
-      assertEquals(2, readMatrices.length);
-      for (int i = 0; i < readMatrices.length; ++i) {
-        double[][] doubleMatrices = ((DenseDoubleMatrix) readMatrices[i])
-            .getValues();
-        double[][] doubleExpected = ((DenseDoubleMatrix) matrices[i])
-            .getValues();
-        for (int r = 0; r < doubleMatrices.length; ++r) {
-          assertArrayEquals(doubleExpected[r], doubleMatrices[r], 0.000001);
-        }
-      }
-
-      DoubleMatrix[] readPrevMatrices = readMessage.getPrevMatrices();
-      assertEquals(2, readPrevMatrices.length);
-      for (int i = 0; i < readPrevMatrices.length; ++i) {
-        double[][] doubleMatrices = ((DenseDoubleMatrix) readPrevMatrices[i])
-            .getValues();
-        double[][] doubleExpected = ((DenseDoubleMatrix) prevMatrices[i])
-            .getValues();
-        for (int r = 0; r < doubleMatrices.length; ++r) {
-          assertArrayEquals(doubleExpected[r], doubleMatrices[r], 0.000001);
-        }
-      }
-
-      // delete
-      fs.delete(path, true);
-    } catch (IOException e) {
-      e.printStackTrace();
-    } catch (URISyntaxException e) {
-      e.printStackTrace();
-    }
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/ac8eaf8e/src/test/java/org/apache/horn/core/MLTestBase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/horn/core/MLTestBase.java b/src/test/java/org/apache/horn/core/MLTestBase.java
new file mode 100644
index 0000000..3f02600
--- /dev/null
+++ b/src/test/java/org/apache/horn/core/MLTestBase.java
@@ -0,0 +1,64 @@
+/**
+ * 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.horn.core;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * The common methods for testing machine learning algorithms
+ *
+ */
+public abstract class MLTestBase {
+
+  /**
+   * Conduct the 0-1 normalization.
+   * 
+   * @param instances
+   */
+  protected static void zeroOneNormalization(List<double[]> instanceList,
+      int len) {
+    int dimension = len;
+
+    double[] mins = new double[dimension];
+    double[] maxs = new double[dimension];
+    Arrays.fill(mins, Double.MAX_VALUE);
+    Arrays.fill(maxs, Double.MIN_VALUE);
+
+    for (double[] instance : instanceList) {
+      for (int i = 0; i < len; ++i) {
+        if (mins[i] > instance[i]) {
+          mins[i] = instance[i];
+        }
+        if (maxs[i] < instance[i]) {
+          maxs[i] = instance[i];
+        }
+      }
+    }
+
+    for (double[] instance : instanceList) {
+      for (int i = 0; i < len; ++i) {
+        double range = maxs[i] - mins[i];
+        if (range != 0) {
+          instance[i] = (instance[i] - mins[i]) / range;
+        }
+      }
+    }
+
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/ac8eaf8e/src/test/java/org/apache/horn/core/TestAutoEncoder.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/horn/core/TestAutoEncoder.java b/src/test/java/org/apache/horn/core/TestAutoEncoder.java
new file mode 100644
index 0000000..10ae738
--- /dev/null
+++ b/src/test/java/org/apache/horn/core/TestAutoEncoder.java
@@ -0,0 +1,203 @@
+/**
+ * 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.horn.core;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.BufferedReader;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.io.LongWritable;
+import org.apache.hadoop.io.SequenceFile;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.commons.io.VectorWritable;
+import org.apache.hama.commons.math.DenseDoubleVector;
+import org.apache.hama.commons.math.DoubleVector;
+import org.apache.horn.core.AutoEncoder;
+import org.junit.Test;
+import org.mortbay.log.Log;
+
+/**
+ * Test the functionality of {@link AutoEncoder}.
+ * 
+ */
+public class TestAutoEncoder extends MLTestBase {
+
+  @Test
+  public void testAutoEncoderSimple() {
+    double[][] instances = { { 0, 0, 0, 1 }, { 0, 0, 1, 0 }, { 0, 1, 0, 0 },
+        { 0, 0, 0, 0 } };
+    AutoEncoder encoder = new AutoEncoder(4, 2);
+    // TODO use the configuration
+
+    // encoder.setLearningRate(0.5);
+    // encoder.setMomemtumWeight(0.2);
+
+    int maxIteration = 2000;
+    Random rnd = new Random();
+    for (int iteration = 0; iteration < maxIteration; ++iteration) {
+      for (int i = 0; i < instances.length; ++i) {
+        encoder.trainOnline(new DenseDoubleVector(instances[rnd
+            .nextInt(instances.length)]));
+      }
+    }
+
+    for (int i = 0; i < instances.length; ++i) {
+      DoubleVector encodeVec = encoder.encode(new DenseDoubleVector(
+          instances[i]));
+      DoubleVector decodeVec = encoder.decode(encodeVec);
+      for (int d = 0; d < instances[i].length; ++d) {
+        assertEquals(instances[i][d], decodeVec.get(d), 0.1);
+      }
+    }
+
+  }
+
+  @Test
+  public void testAutoEncoderSwissRollDataset() {
+    List<double[]> instanceList = new ArrayList<double[]>();
+    try {
+      BufferedReader br = new BufferedReader(new FileReader(
+          "src/test/resources/dimensional_reduction.txt"));
+      String line = null;
+      while ((line = br.readLine()) != null) {
+        String[] tokens = line.split("\t");
+        double[] instance = new double[tokens.length];
+        for (int i = 0; i < instance.length; ++i) {
+          instance[i] = Double.parseDouble(tokens[i]);
+        }
+        instanceList.add(instance);
+      }
+      br.close();
+      // normalize instances
+      zeroOneNormalization(instanceList, instanceList.get(0).length);
+    } catch (FileNotFoundException e) {
+      e.printStackTrace();
+    } catch (NumberFormatException e) {
+      e.printStackTrace();
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
+
+    List<DoubleVector> vecInstanceList = new ArrayList<DoubleVector>();
+    for (double[] instance : instanceList) {
+      vecInstanceList.add(new DenseDoubleVector(instance));
+    }
+    AutoEncoder encoder = new AutoEncoder(3, 2);
+    // encoder.setLearningRate(0.05);
+    // encoder.setMomemtumWeight(0.1);
+    int maxIteration = 2000;
+    for (int iteration = 0; iteration < maxIteration; ++iteration) {
+      for (DoubleVector vector : vecInstanceList) {
+        encoder.trainOnline(vector);
+      }
+    }
+
+    double errorInstance = 0;
+    for (DoubleVector vector : vecInstanceList) {
+      DoubleVector decoded = encoder.getOutput(vector);
+      DoubleVector diff = vector.subtract(decoded);
+      double error = diff.dot(diff);
+      if (error > 0.1) {
+        ++errorInstance;
+      }
+    }
+    Log.info(String.format("Autoecoder error rate: %f%%\n", errorInstance * 100
+        / vecInstanceList.size()));
+
+  }
+
+  @Test
+  public void testAutoEncoderSwissRollDatasetDistributed() {
+    HamaConfiguration conf = new HamaConfiguration();
+    String strDataPath = "/tmp/dimensional_reduction.txt";
+    Path path = new Path(strDataPath);
+    List<double[]> instanceList = new ArrayList<double[]>();
+    try {
+      FileSystem fs = FileSystem.get(new URI(strDataPath), conf);
+      if (fs.exists(path)) {
+        fs.delete(path, true);
+      }
+
+      String line = null;
+      BufferedReader br = new BufferedReader(new FileReader(
+          "src/test/resources/dimensional_reduction.txt"));
+      while ((line = br.readLine()) != null) {
+        String[] tokens = line.split("\t");
+        double[] instance = new double[tokens.length];
+        for (int i = 0; i < instance.length; ++i) {
+          instance[i] = Double.parseDouble(tokens[i]);
+        }
+        instanceList.add(instance);
+      }
+      br.close();
+      // normalize instances
+      zeroOneNormalization(instanceList, instanceList.get(0).length);
+
+      SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, path,
+          LongWritable.class, VectorWritable.class);
+      for (int i = 0; i < instanceList.size(); ++i) {
+        DoubleVector vector = new DenseDoubleVector(instanceList.get(i));
+        writer.append(new LongWritable(i), new VectorWritable(vector));
+      }
+
+      writer.close();
+    } catch (FileNotFoundException e) {
+      e.printStackTrace();
+    } catch (IOException e) {
+      e.printStackTrace();
+    } catch (URISyntaxException e) {
+      e.printStackTrace();
+    }
+
+    AutoEncoder encoder = new AutoEncoder(3, 2);
+    String modelPath = "/tmp/autoencoder-modelpath";
+    encoder.setModelPath(modelPath);
+    Map<String, String> trainingParams = new HashMap<String, String>();
+    // encoder.setLearningRate(0.5);
+    trainingParams.put("tasks", "5");
+    trainingParams.put("training.max.iterations", "3000");
+    trainingParams.put("training.batch.size", "200");
+    // encoder.train(conf, path, trainingParams);
+
+    double errorInstance = 0;
+    for (double[] instance : instanceList) {
+      DoubleVector vector = new DenseDoubleVector(instance);
+      DoubleVector decoded = encoder.getOutput(vector);
+      DoubleVector diff = vector.subtract(decoded);
+      double error = diff.dot(diff);
+      if (error > 0.1) {
+        ++errorInstance;
+      }
+    }
+    Log.info(String.format("Autoecoder error rate: %f%%\n", errorInstance * 100
+        / instanceList.size()));
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/ac8eaf8e/src/test/java/org/apache/horn/core/TestNeuron.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/horn/core/TestNeuron.java b/src/test/java/org/apache/horn/core/TestNeuron.java
new file mode 100644
index 0000000..f2fe4e1
--- /dev/null
+++ b/src/test/java/org/apache/horn/core/TestNeuron.java
@@ -0,0 +1,93 @@
+/**
+ * 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.horn.core;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.hadoop.io.DoubleWritable;
+import org.apache.hama.HamaConfiguration;
+import org.apache.horn.core.Neuron;
+import org.apache.horn.core.Synapse;
+import org.apache.horn.funcs.Sigmoid;
+
+public class TestNeuron extends TestCase {
+  private static double learningRate = 0.1;
+  private static double bias = -1;
+  private static double theta = 0.8;
+
+  public static class MyNeuron extends
+      Neuron<Synapse<DoubleWritable, DoubleWritable>> {
+
+    @Override
+    public void setup(HamaConfiguration conf) {
+    }
+
+    @Override
+    public void forward(
+        Iterable<Synapse<DoubleWritable, DoubleWritable>> messages)
+        throws IOException {
+      double sum = 0;
+      for (Synapse<DoubleWritable, DoubleWritable> m : messages) {
+        sum += m.getInput() * m.getWeight();
+      }
+      sum += (bias * theta);
+      this.feedforward(new Sigmoid().apply(sum));
+    }
+
+    @Override
+    public void backward(
+        Iterable<Synapse<DoubleWritable, DoubleWritable>> messages)
+        throws IOException {
+      for (Synapse<DoubleWritable, DoubleWritable> m : messages) {
+        // Calculates error gradient for each neuron
+        double gradient = new Sigmoid().applyDerivative(this.getOutput()) * (m.getDelta() * m.getWeight());
+
+        // Propagates to lower layer
+        backpropagate(gradient);
+
+        // Weight corrections
+        double weight = learningRate * this.getOutput() * m.getDelta();
+        this.push(weight);
+      }
+    }
+
+  }
+
+  public void testProp() throws IOException {
+    List<Synapse<DoubleWritable, DoubleWritable>> x = new ArrayList<Synapse<DoubleWritable, DoubleWritable>>();
+    x.add(new Synapse<DoubleWritable, DoubleWritable>(new DoubleWritable(
+        1.0), new DoubleWritable(0.5)));
+    x.add(new Synapse<DoubleWritable, DoubleWritable>(new DoubleWritable(
+        1.0), new DoubleWritable(0.4)));
+
+    MyNeuron n = new MyNeuron();
+    n.forward(x);
+    assertEquals(0.5249791874789399, n.getOutput());
+
+    x.clear();
+    x.add(new Synapse<DoubleWritable, DoubleWritable>(new DoubleWritable(
+        -0.1274), new DoubleWritable(-1.2)));
+    n.backward(x);
+    assertEquals(-0.006688234848481696, n.getUpdate());
+  }
+
+}