hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject [1/5] hama git commit: HAMA-961: Remove ann package
Date Mon, 23 Nov 2015 02:26:47 GMT
Repository: hama
Updated Branches:
  refs/heads/master 0225205a9 -> 3a3ea7a37


http://git-wip-us.apache.org/repos/asf/hama/blob/3a3ea7a3/ml/src/test/java/org/apache/hama/ml/perception/TestSmallMLPMessage.java
----------------------------------------------------------------------
diff --git a/ml/src/test/java/org/apache/hama/ml/perception/TestSmallMLPMessage.java b/ml/src/test/java/org/apache/hama/ml/perception/TestSmallMLPMessage.java
deleted file mode 100644
index ba2b8c4..0000000
--- a/ml/src/test/java/org/apache/hama/ml/perception/TestSmallMLPMessage.java
+++ /dev/null
@@ -1,147 +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.hama.ml.perception;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-
-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.junit.Test;
-
-/**
- * Test the functionalities of SmallMLPMessage
- * 
- */
-public class TestSmallMLPMessage {
-
-  @Test
-  public void testReadWriteWithoutPrevUpdate() {
-    int owner = 101;
-    double[][] mat = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
-    double[][] mat2 = { { 10, 20 }, { 30, 40 }, { 50, 60 } };
-    double[][][] mats = { mat, mat2 };
-
-    DenseDoubleMatrix[] matrices = new DenseDoubleMatrix[] {
-        new DenseDoubleMatrix(mat), new DenseDoubleMatrix(mat2) };
-
-    SmallMLPMessage message = new SmallMLPMessage(owner, true, matrices);
-
-    Configuration conf = new Configuration();
-    String strPath = "/tmp/testSmallMLPMessage";
-    Path path = new Path(strPath);
-    try {
-      FileSystem fs = FileSystem.get(new URI(strPath), conf);
-      FSDataOutputStream out = fs.create(path, true);
-      message.write(out);
-      out.close();
-
-      FSDataInputStream in = fs.open(path);
-      SmallMLPMessage outMessage = new SmallMLPMessage(0, false, null);
-      outMessage.readFields(in);
-
-      assertEquals(owner, outMessage.getOwner());
-      DenseDoubleMatrix[] outMatrices = outMessage.getWeightUpdatedMatrices();
-      // check each matrix
-      for (int i = 0; i < outMatrices.length; ++i) {
-        double[][] outMat = outMatrices[i].getValues();
-        for (int j = 0; j < outMat.length; ++j) {
-          assertArrayEquals(mats[i][j], outMat[j], 0.0001);
-        }
-      }
-
-      fs.delete(path, true);
-    } catch (IOException e) {
-      e.printStackTrace();
-    } catch (URISyntaxException e) {
-      e.printStackTrace();
-    }
-  }
-
-  @Test
-  public void testReadWriteWithPrevUpdate() {
-    int owner = 101;
-    double[][] mat = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
-    double[][] mat2 = { { 10, 20 }, { 30, 40 }, { 50, 60 } };
-    double[][][] mats = { mat, mat2 };
-
-    double[][] prevMat = { { 0.1, 0.2, 0.3 }, { 0.4, 0.5, 0.6 },
-        { 0.7, 0.8, 0.9 } };
-    double[][] prevMat2 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
-    double[][][] prevMats = { prevMat, prevMat2 };
-
-    DenseDoubleMatrix[] matrices = new DenseDoubleMatrix[] {
-        new DenseDoubleMatrix(mat), new DenseDoubleMatrix(mat2) };
-
-    DenseDoubleMatrix[] prevMatrices = new DenseDoubleMatrix[] {
-        new DenseDoubleMatrix(prevMat), new DenseDoubleMatrix(prevMat2) };
-
-    boolean terminated = false;
-    SmallMLPMessage message = new SmallMLPMessage(owner, terminated, matrices,
-        prevMatrices);
-
-    Configuration conf = new Configuration();
-    String strPath = "/tmp/testSmallMLPMessageWithPrevMatrices";
-    Path path = new Path(strPath);
-    try {
-      FileSystem fs = FileSystem.get(new URI(strPath), conf);
-      FSDataOutputStream out = fs.create(path, true);
-      message.write(out);
-      out.close();
-
-      FSDataInputStream in = fs.open(path);
-      SmallMLPMessage outMessage = new SmallMLPMessage(0, false, null);
-      outMessage.readFields(in);
-
-      assertEquals(owner, outMessage.getOwner());
-      assertEquals(terminated, outMessage.isTerminated());
-      DenseDoubleMatrix[] outMatrices = outMessage.getWeightUpdatedMatrices();
-      // check each matrix
-      for (int i = 0; i < outMatrices.length; ++i) {
-        double[][] outMat = outMatrices[i].getValues();
-        for (int j = 0; j < outMat.length; ++j) {
-          assertArrayEquals(mats[i][j], outMat[j], 0.0001);
-        }
-      }
-
-      DenseDoubleMatrix[] outPrevMatrices = outMessage
-          .getPrevWeightsUpdatedMatrices();
-      // check each matrix
-      for (int i = 0; i < outPrevMatrices.length; ++i) {
-        double[][] outMat = outPrevMatrices[i].getValues();
-        for (int j = 0; j < outMat.length; ++j) {
-          assertArrayEquals(prevMats[i][j], outMat[j], 0.0001);
-        }
-      }
-
-      fs.delete(path, true);
-    } catch (IOException e) {
-      e.printStackTrace();
-    } catch (URISyntaxException e) {
-      e.printStackTrace();
-    }
-  }
-}

http://git-wip-us.apache.org/repos/asf/hama/blob/3a3ea7a3/ml/src/test/java/org/apache/hama/ml/perception/TestSmallMultiLayerPerceptron.java
----------------------------------------------------------------------
diff --git a/ml/src/test/java/org/apache/hama/ml/perception/TestSmallMultiLayerPerceptron.java
b/ml/src/test/java/org/apache/hama/ml/perception/TestSmallMultiLayerPerceptron.java
deleted file mode 100644
index 02fa2da..0000000
--- a/ml/src/test/java/org/apache/hama/ml/perception/TestSmallMultiLayerPerceptron.java
+++ /dev/null
@@ -1,524 +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.hama.ml.perception;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-
-import java.io.IOException;
-import java.net.URI;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Random;
-
-import org.apache.commons.lang.SerializationUtils;
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.fs.FSDataOutputStream;
-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.hadoop.io.WritableUtils;
-import org.apache.hama.commons.io.MatrixWritable;
-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.junit.Test;
-import org.mortbay.log.Log;
-
-public class TestSmallMultiLayerPerceptron {
-
-  /**
-   * Write and read the parameters of MLP.
-   */
-  @Test
-  public void testWriteReadMLP() {
-    String modelPath = "/tmp/sampleModel-testWriteReadMLP.data";
-    double learningRate = 0.3;
-    double regularization = 0.0; // no regularization
-    double momentum = 0; // no momentum
-    String squashingFunctionName = "Sigmoid";
-    String costFunctionName = "SquaredError";
-    int[] layerSizeArray = new int[] { 3, 2, 2, 3 };
-    MultiLayerPerceptron mlp = new SmallMultiLayerPerceptron(learningRate,
-        regularization, momentum, squashingFunctionName, costFunctionName,
-        layerSizeArray);
-    FeatureTransformer transformer = new DefaultFeatureTransformer();
-    mlp.setFeatureTransformer(transformer);
-    try {
-      mlp.writeModelToFile(modelPath);
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-
-    try {
-      // read the meta-data
-      Configuration conf = new Configuration();
-      FileSystem fs = FileSystem.get(conf);
-      mlp = new SmallMultiLayerPerceptron(modelPath);
-      assertEquals(mlp.getClass().getName(), mlp.getMLPType());
-      assertEquals(learningRate, mlp.getLearningRate(), 0.001);
-      assertEquals(regularization, mlp.isRegularization(), 0.001);
-      assertEquals(layerSizeArray.length, mlp.getNumberOfLayers());
-      assertEquals(momentum, mlp.getMomentum(), 0.001);
-      assertEquals(squashingFunctionName, mlp.getSquashingFunctionName());
-      assertEquals(costFunctionName, mlp.getCostFunctionName());
-      assertArrayEquals(layerSizeArray, mlp.getLayerSizeArray());
-      assertEquals(transformer.getClass().getName(), mlp.getFeatureTransformer().getClass().getName());
-      // delete test file
-      fs.delete(new Path(modelPath), true);
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-  }
-
-  /**
-   * Test the output of an example MLP.
-   */
-  @Test
-  public void testOutput() {
-    // write the MLP meta-data manually
-    String modelPath = "/tmp/sampleModel-testOutput.data";
-    Configuration conf = new Configuration();
-    try {
-      FileSystem fs = FileSystem.get(conf);
-      FSDataOutputStream output = fs.create(new Path(modelPath), true);
-
-      String MLPType = SmallMultiLayerPerceptron.class.getName();
-      double learningRate = 0.5;
-      double regularization = 0.0;
-      double momentum = 0.1;
-      String squashingFunctionName = "Sigmoid";
-      String costFunctionName = "SquaredError";
-      int[] layerSizeArray = new int[] { 3, 2, 3, 3 };
-      int numberOfLayers = layerSizeArray.length;
-
-      WritableUtils.writeString(output, MLPType);
-      output.writeDouble(learningRate);
-      output.writeDouble(regularization);
-      output.writeDouble(momentum);
-      output.writeInt(numberOfLayers);
-      WritableUtils.writeString(output, squashingFunctionName);
-      WritableUtils.writeString(output, costFunctionName);
-
-      // write the number of neurons for each layer
-      for (int i = 0; i < numberOfLayers; ++i) {
-        output.writeInt(layerSizeArray[i]);
-      }
-
-      double[][] matrix01 = { // 4 by 2
-      { 0.5, 0.2 }, { 0.1, 0.1 }, { 0.2, 0.5 }, { 0.1, 0.5 } };
-
-      double[][] matrix12 = { // 3 by 3
-      { 0.1, 0.2, 0.5 }, { 0.2, 0.5, 0.2 }, { 0.5, 0.5, 0.1 } };
-
-      double[][] matrix23 = { // 4 by 3
-      { 0.2, 0.5, 0.2 }, { 0.5, 0.1, 0.5 }, { 0.1, 0.2, 0.1 },
-          { 0.1, 0.2, 0.5 } };
-
-      DoubleMatrix[] matrices = { new DenseDoubleMatrix(matrix01),
-          new DenseDoubleMatrix(matrix12), new DenseDoubleMatrix(matrix23) };
-      for (DoubleMatrix mat : matrices) {
-        MatrixWritable.write(mat, output);
-      }
-
-      // serialize the feature transformer
-      FeatureTransformer transformer = new DefaultFeatureTransformer();
-      Class<? extends FeatureTransformer> featureTransformerCls = transformer.getClass();
-      byte[] featureTransformerBytes = SerializationUtils.serialize(featureTransformerCls);
-      output.writeInt(featureTransformerBytes.length);
-      output.write(featureTransformerBytes);
-      
-      output.close();
-
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-
-    // initial the mlp with existing model meta-data and get the output
-    MultiLayerPerceptron mlp = new SmallMultiLayerPerceptron(modelPath);
-    DoubleVector input = new DenseDoubleVector(new double[] { 1, 2, 3 });
-    try {
-      DoubleVector result = mlp.output(input);
-      assertArrayEquals(new double[] { 0.6636557, 0.7009963, 0.7213835 },
-          result.toArray(), 0.0001);
-    } catch (Exception e1) {
-      e1.printStackTrace();
-    }
-
-    // delete meta-data
-    try {
-      FileSystem fs = FileSystem.get(conf);
-      fs.delete(new Path(modelPath), true);
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-
-  }
-
-  /**
-   * Test training with squared error on the XOR problem.
-   */
-  @Test
-  public void testTrainWithSquaredError() {
-    // generate training data
-    DoubleVector[] trainingData = new DenseDoubleVector[] {
-        new DenseDoubleVector(new double[] { 0, 0, 0 }),
-        new DenseDoubleVector(new double[] { 0, 1, 1 }),
-        new DenseDoubleVector(new double[] { 1, 0, 1 }),
-        new DenseDoubleVector(new double[] { 1, 1, 0 }) };
-
-    // set parameters
-    double learningRate = 0.3;
-    double regularization = 0.02; // no regularization
-    double momentum = 0; // no momentum
-    String squashingFunctionName = "Sigmoid";
-    String costFunctionName = "SquaredError";
-    int[] layerSizeArray = new int[] { 2, 5, 1 };
-    SmallMultiLayerPerceptron mlp = new SmallMultiLayerPerceptron(learningRate,
-        regularization, momentum, squashingFunctionName, costFunctionName,
-        layerSizeArray);
-
-    try {
-      // train by multiple instances
-      Random rnd = new Random();
-      for (int i = 0; i < 100000; ++i) {
-        DenseDoubleMatrix[] weightUpdates = mlp
-            .trainByInstance(trainingData[rnd.nextInt(4)]);
-        mlp.updateWeightMatrices(weightUpdates);
-      }
-
-      // System.out.printf("Weight matrices: %s\n",
-      // mlp.weightsToString(mlp.getWeightMatrices()));
-      for (int i = 0; i < trainingData.length; ++i) {
-        DenseDoubleVector testVec = (DenseDoubleVector) trainingData[i]
-            .slice(2);
-        double expected = trainingData[i].toArray()[2];
-        double actual = mlp.output(testVec).toArray()[0];
-        if (expected < 0.5 && actual >= 0.5 || expected >= 0.5 &&
actual < 0.5) {
-          Log.info("Neural network failes to lear the XOR.");
-        }
-      }
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-  }
-
-  /**
-   * Test training with cross entropy on the XOR problem.
-   */
-  @Test
-  public void testTrainWithCrossEntropy() {
-    // generate training data
-    DoubleVector[] trainingData = new DenseDoubleVector[] {
-        new DenseDoubleVector(new double[] { 0, 0, 0 }),
-        new DenseDoubleVector(new double[] { 0, 1, 1 }),
-        new DenseDoubleVector(new double[] { 1, 0, 1 }),
-        new DenseDoubleVector(new double[] { 1, 1, 0 }) };
-
-    // set parameters
-    double learningRate = 0.3;
-    double regularization = 0.0; // no regularization
-    double momentum = 0; // no momentum
-    String squashingFunctionName = "Sigmoid";
-    String costFunctionName = "CrossEntropy";
-    int[] layerSizeArray = new int[] { 2, 7, 1 };
-    SmallMultiLayerPerceptron mlp = new SmallMultiLayerPerceptron(learningRate,
-        regularization, momentum, squashingFunctionName, costFunctionName,
-        layerSizeArray);
-
-    try {
-      // train by multiple instances
-      Random rnd = new Random();
-      for (int i = 0; i < 50000; ++i) {
-        DenseDoubleMatrix[] weightUpdates = mlp
-            .trainByInstance(trainingData[rnd.nextInt(4)]);
-        mlp.updateWeightMatrices(weightUpdates);
-      }
-
-      // System.out.printf("Weight matrices: %s\n",
-      // mlp.weightsToString(mlp.getWeightMatrices()));
-      for (int i = 0; i < trainingData.length; ++i) {
-        DenseDoubleVector testVec = (DenseDoubleVector) trainingData[i]
-            .slice(2);
-        double expected = trainingData[i].toArray()[2];
-        double actual = mlp.output(testVec).toArray()[0];
-        if (expected < 0.5 && actual >= 0.5 || expected >= 0.5 &&
actual < 0.5) {
-          Log.info("Neural network failes to lear the XOR.");
-        }
-      }
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-  }
-
-  /**
-   * Test training with regularizatiion.
-   */
-  @Test
-  public void testWithRegularization() {
-    // generate training data
-    DoubleVector[] trainingData = new DenseDoubleVector[] {
-        new DenseDoubleVector(new double[] { 0, 0, 0 }),
-        new DenseDoubleVector(new double[] { 0, 1, 1 }),
-        new DenseDoubleVector(new double[] { 1, 0, 1 }),
-        new DenseDoubleVector(new double[] { 1, 1, 0 }) };
-
-    // set parameters
-    double learningRate = 0.3;
-    double regularization = 0.02; // regularization should be a tiny number
-    double momentum = 0; // no momentum
-    String squashingFunctionName = "Sigmoid";
-    String costFunctionName = "CrossEntropy";
-    int[] layerSizeArray = new int[] { 2, 7, 1 };
-    SmallMultiLayerPerceptron mlp = new SmallMultiLayerPerceptron(learningRate,
-        regularization, momentum, squashingFunctionName, costFunctionName,
-        layerSizeArray);
-
-    try {
-      // train by multiple instances
-      Random rnd = new Random();
-      for (int i = 0; i < 20000; ++i) {
-        DenseDoubleMatrix[] weightUpdates = mlp
-            .trainByInstance(trainingData[rnd.nextInt(4)]);
-        mlp.updateWeightMatrices(weightUpdates);
-      }
-
-      // System.out.printf("Weight matrices: %s\n",
-      // mlp.weightsToString(mlp.getWeightMatrices()));
-      for (int i = 0; i < trainingData.length; ++i) {
-        DenseDoubleVector testVec = (DenseDoubleVector) trainingData[i]
-            .slice(2);
-        double expected = trainingData[i].toArray()[2];
-        double actual = mlp.output(testVec).toArray()[0];
-        if (expected < 0.5 && actual >= 0.5 || expected >= 0.5 &&
actual < 0.5) {
-          Log.info("Neural network failes to lear the XOR.");
-        }
-      }
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-  }
-
-  /**
-   * Test training with momentum. The MLP can converge faster.
-   */
-  @Test
-  public void testWithMomentum() {
-    // generate training data
-    DoubleVector[] trainingData = new DenseDoubleVector[] {
-        new DenseDoubleVector(new double[] { 0, 0, 0 }),
-        new DenseDoubleVector(new double[] { 0, 1, 1 }),
-        new DenseDoubleVector(new double[] { 1, 0, 1 }),
-        new DenseDoubleVector(new double[] { 1, 1, 0 }) };
-
-    // set parameters
-    double learningRate = 0.3;
-    double regularization = 0.02; // regularization should be a tiny number
-    double momentum = 0.5; // no momentum
-    String squashingFunctionName = "Sigmoid";
-    String costFunctionName = "CrossEntropy";
-    int[] layerSizeArray = new int[] { 2, 7, 1 };
-    SmallMultiLayerPerceptron mlp = new SmallMultiLayerPerceptron(learningRate,
-        regularization, momentum, squashingFunctionName, costFunctionName,
-        layerSizeArray);
-
-    try {
-      // train by multiple instances
-      Random rnd = new Random();
-      for (int i = 0; i < 5000; ++i) {
-        DenseDoubleMatrix[] weightUpdates = mlp
-            .trainByInstance(trainingData[rnd.nextInt(4)]);
-        mlp.updateWeightMatrices(weightUpdates);
-      }
-
-      // System.out.printf("Weight matrices: %s\n",
-      // mlp.weightsToString(mlp.getWeightMatrices()));
-      for (int i = 0; i < trainingData.length; ++i) {
-        DenseDoubleVector testVec = (DenseDoubleVector) trainingData[i]
-            .slice(2);
-        double expected = trainingData[i].toArray()[2];
-        double actual = mlp.output(testVec).toArray()[0];
-        if (expected < 0.5 && actual >= 0.5 || expected >= 0.5 &&
actual < 0.5) {
-          Log.info("Neural network failes to lear the XOR.");
-        }
-      }
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-  }
-  
-  @Test
-  public void testByRunningJobs() {
-    this.testTrainingByXOR();
-    this.testFeatureTransformer();
-  }
-
-  /**
-   * Test the XOR problem.
-   */
-  public void testTrainingByXOR() {
-    // write in some training instances
-    Configuration conf = new Configuration();
-    String strDataPath = "/tmp/xor-training-by-xor";
-    Path dataPath = new Path(strDataPath);
-
-    // generate training data
-    DoubleVector[] trainingData = new DenseDoubleVector[] {
-        new DenseDoubleVector(new double[] { 0, 0, 0 }),
-        new DenseDoubleVector(new double[] { 0, 1, 1 }),
-        new DenseDoubleVector(new double[] { 1, 0, 1 }),
-        new DenseDoubleVector(new double[] { 1, 1, 0 }) };
-
-    try {
-      URI uri = new URI(strDataPath);
-      FileSystem fs = FileSystem.get(uri, conf);
-      fs.delete(dataPath, true);
-      if (!fs.exists(dataPath)) {
-        fs.createNewFile(dataPath);
-        SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
-            dataPath, LongWritable.class, VectorWritable.class);
-
-        for (int i = 0; i < 1000; ++i) {
-          VectorWritable vecWritable = new VectorWritable(trainingData[i % 4]);
-          writer.append(new LongWritable(i), vecWritable);
-        }
-        writer.close();
-      }
-
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-
-    // begin training
-    String modelPath = "/tmp/xorModel-training-by-xor.data";
-    double learningRate = 0.6;
-    double regularization = 0.02; // no regularization
-    double momentum = 0.3; // no momentum
-    String squashingFunctionName = "Tanh";
-    String costFunctionName = "SquaredError";
-    int[] layerSizeArray = new int[] { 2, 5, 1 };
-    SmallMultiLayerPerceptron mlp = new SmallMultiLayerPerceptron(learningRate,
-        regularization, momentum, squashingFunctionName, costFunctionName,
-        layerSizeArray);
-
-    Map<String, String> trainingParams = new HashMap<String, String>();
-    trainingParams.put("training.iteration", "2000");
-    trainingParams.put("training.mode", "minibatch.gradient.descent");
-    trainingParams.put("training.batch.size", "100");
-    trainingParams.put("tasks", "3");
-    trainingParams.put("modelPath", modelPath);
-
-    try {
-      mlp.train(dataPath, trainingParams);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-
-    // test the model
-    for (int i = 0; i < trainingData.length; ++i) {
-      DenseDoubleVector testVec = (DenseDoubleVector) trainingData[i].slice(2);
-      try {
-        double expected = trainingData[i].toArray()[2];
-        double actual = mlp.output(testVec).toArray()[0];
-        if (expected < 0.5 && actual >= 0.5 || expected >= 0.5 &&
actual < 0.5) {
-          Log.info("Neural network failes to lear the XOR.");
-        }
-      } catch (Exception e) {
-        e.printStackTrace();
-      }
-    }
-  }
-  
-  /**
-   * Use transformer to extract the first half features of the original features.
-   */
-  public void testFeatureTransformer() {
- // write in some training instances
-    Configuration conf = new Configuration();
-    String strDataPath = "/tmp/xor-training-by-xor";
-    Path dataPath = new Path(strDataPath);
-
-    // generate training data
-    DoubleVector[] trainingData = new DenseDoubleVector[] {
-        new DenseDoubleVector(new double[] { 0, 0, 0 }),
-        new DenseDoubleVector(new double[] { 0, 1, 1 }),
-        new DenseDoubleVector(new double[] { 1, 0, 1 }),
-        new DenseDoubleVector(new double[] { 1, 1, 0 }) };
-    
-    try {
-      URI uri = new URI(strDataPath);
-      FileSystem fs = FileSystem.get(uri, conf);
-      fs.delete(dataPath, true);
-      if (!fs.exists(dataPath)) {
-        fs.createNewFile(dataPath);
-        SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
-            dataPath, LongWritable.class, VectorWritable.class);
-
-        for (int i = 0; i < 1000; ++i) {
-          VectorWritable vecWritable = new VectorWritable(trainingData[i % 4]);
-          writer.append(new LongWritable(i), vecWritable);
-        }
-        writer.close();
-      }
-
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-
-    // begin training
-    String modelPath = "/tmp/xorModel-training-by-xor.data";
-    double learningRate = 0.6;
-    double regularization = 0.02; // no regularization
-    double momentum = 0.3; // no momentum
-    String squashingFunctionName = "Tanh";
-    String costFunctionName = "SquaredError";
-    int[] layerSizeArray = new int[] { 1, 5, 1 };
-    SmallMultiLayerPerceptron mlp = new SmallMultiLayerPerceptron(learningRate,
-        regularization, momentum, squashingFunctionName, costFunctionName,
-        layerSizeArray);
-    
-    mlp.setFeatureTransformer(new FeatureTransformer() {
-
-      @Override
-      public DoubleVector transform(DoubleVector originalFeatures) {
-        return originalFeatures.sliceUnsafe(originalFeatures.getDimension() / 2);
-      }
-      
-    });
-
-    Map<String, String> trainingParams = new HashMap<String, String>();
-    trainingParams.put("training.iteration", "2000");
-    trainingParams.put("training.mode", "minibatch.gradient.descent");
-    trainingParams.put("training.batch.size", "100");
-    trainingParams.put("tasks", "3");
-    trainingParams.put("modelPath", modelPath);
-
-    try {
-      mlp.train(dataPath, trainingParams);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/hama/blob/3a3ea7a3/ml/src/test/java/org/apache/hama/ml/regression/TestLinearRegression.java
----------------------------------------------------------------------
diff --git a/ml/src/test/java/org/apache/hama/ml/regression/TestLinearRegression.java b/ml/src/test/java/org/apache/hama/ml/regression/TestLinearRegression.java
deleted file mode 100644
index 54c473b..0000000
--- a/ml/src/test/java/org/apache/hama/ml/regression/TestLinearRegression.java
+++ /dev/null
@@ -1,133 +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.hama.ml.regression;
-
-import java.io.BufferedReader;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-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 functionalities of the linear regression model.
- * 
- */
-public class TestLinearRegression {
-
-  @Test
-  public void testLinearRegressionSimple() {
-    // y = 2.1 * x_1 + 0.7 * x_2 * 0.1 * x_3
-    double[][] instances = { { 1, 1, 1, 2.9 }, { 5, 2, 3, 12.2 },
-        { 2, 5, 8, 8.5 }, { 0.5, 0.1, 0.2, 1.14 }, { 10, 20, 30, 38 },
-        { 0.6, 20, 5, 16.76 } };
-
-    LinearRegression regression = new LinearRegression(instances[0].length - 1);
-    regression.setLearningRate(0.001);
-    regression.setMomemtumWeight(0.1);
-
-    int iterations = 100;
-    for (int i = 0; i < iterations; ++i) {
-      for (int j = 0; j < instances.length; ++j) {
-        regression.trainOnline(new DenseDoubleVector(instances[j]));
-      }
-    }
-
-    double relativeError = 0;
-    for (int i = 0; i < instances.length; ++i) {
-      DoubleVector test = new DenseDoubleVector(instances[i]);
-      double expected = test.get(test.getDimension() - 1);
-      test = test.slice(test.getDimension() - 1);
-      double actual = regression.getOutput(test).get(0);
-      relativeError += Math.abs((expected - actual) / expected);
-    }
-
-    relativeError /= instances.length;
-    Log.info(String.format("Relative error %f%%\n", relativeError));
-  }
-
-  @Test
-  public void testLinearRegressionOnlineTraining() {
-    // read linear regression data
-    String filepath = "src/test/resources/linear_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) {
-        if (line.startsWith("#")) { // ignore comments
-          continue;
-        }
-        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();
-    }
-    // divide dataset into training and testing
-    List<double[]> testInstances = new ArrayList<double[]>();
-    testInstances.addAll(instanceList.subList(instanceList.size() - 20,
-        instanceList.size()));
-    List<double[]> trainingInstances = instanceList.subList(0,
-        instanceList.size() - 20);
-
-    int dimension = instanceList.get(0).length - 1;
-
-    LinearRegression regression = new LinearRegression(dimension);
-    regression.setLearningRate(0.00000005);
-    regression.setMomemtumWeight(0.1);
-    regression.setRegularizationWeight(0.05);
-    int iterations = 2000;
-    for (int i = 0; i < iterations; ++i) {
-      for (double[] trainingInstance : trainingInstances) {
-        regression.trainOnline(new DenseDoubleVector(trainingInstance));
-      }
-    }
-
-    double relativeError = 0.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 = regression.getOutput(instance).get(0);
-      if (expected == 0) {
-        expected = 0.0000001;
-      }
-      relativeError += Math.abs((expected - actual) / expected);
-    }
-    relativeError /= testInstances.size();
-
-    Log.info(String.format("Relative error: %f%%\n", relativeError * 100));
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/hama/blob/3a3ea7a3/ml/src/test/java/org/apache/hama/ml/regression/TestLogisticRegression.java
----------------------------------------------------------------------
diff --git a/ml/src/test/java/org/apache/hama/ml/regression/TestLogisticRegression.java b/ml/src/test/java/org/apache/hama/ml/regression/TestLogisticRegression.java
deleted file mode 100644
index ed76d03..0000000
--- a/ml/src/test/java/org/apache/hama/ml/regression/TestLogisticRegression.java
+++ /dev/null
@@ -1,130 +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.hama.ml.regression;
-
-import java.io.BufferedReader;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-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 functionalities of LogisticRegression.
- * 
- */
-public class TestLogisticRegression {
-
-  @Test
-  public void testLogisticRegressionLocal() {
-    // read 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) {
-        if (line.startsWith("#")) { // ignore comments
-          continue;
-        }
-        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();
-    }
-
-    int dimension = instanceList.get(0).length - 1;
-
-    // min-max normalization
-    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 < instance.length - 1; ++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 < instance.length - 1; ++i) {
-        double range = maxs[i] - mins[i];
-        if (range != 0) {
-          instance[i] = (instance[i] - mins[i]) / range;
-        }
-      }
-    }
-
-    // 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);
-
-    LogisticRegression regression = new LogisticRegression(dimension);
-    regression.setLearningRate(0.2);
-    regression.setMomemtumWeight(0.1);
-    regression.setRegularizationWeight(0.1);
-    int iterations = 1000;
-    for (int i = 0; i < iterations; ++i) {
-      for (double[] trainingInstance : trainingInstances) {
-        regression.trainOnline(new DenseDoubleVector(trainingInstance));
-      }
-    }
-
-    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);
-      DoubleVector features = instance.slice(instance.getDimension() - 1);
-      double actual = regression.getOutput(features).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));
-  }
-
-}


Mime
View raw message