[mlpack] 190/324: Adaboost and Perceptron modified (improved constructor), going for tests on one Weak L

Barak A. Pearlmutter barak+git at cs.nuim.ie
Sun Aug 17 08:22:09 UTC 2014


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch svn-trunk
in repository mlpack.

commit 59bd2cd0c634057973c31eb4ee4e528d54b779e3
Author: saxena.udit <saxena.udit at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date:   Wed Jul 16 18:26:17 2014 +0000

    Adaboost and Perceptron modified (improved constructor), going for tests on one Weak L
    
    git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@16832 9d5b8971-822b-0410-80eb-d18c1038ef23
---
 src/mlpack/methods/CMakeLists.txt                 |   2 +-
 src/mlpack/methods/adaboost/adaboost.hpp          |   2 +-
 src/mlpack/methods/adaboost/adaboost_impl.hpp     |  14 ++-
 src/mlpack/methods/adaboost/adaboost_main.cpp     |  40 ++++++--
 src/mlpack/methods/perceptron/perceptron.hpp      |  18 ++--
 src/mlpack/methods/perceptron/perceptron_impl.hpp | 108 ++++++++++++----------
 src/mlpack/methods/perceptron/perceptron_main.cpp |   4 +-
 7 files changed, 113 insertions(+), 75 deletions(-)

diff --git a/src/mlpack/methods/CMakeLists.txt b/src/mlpack/methods/CMakeLists.txt
index 925e74d..1c0e61d 100644
--- a/src/mlpack/methods/CMakeLists.txt
+++ b/src/mlpack/methods/CMakeLists.txt
@@ -1,6 +1,6 @@
 # Recurse into each method mlpack provides.
 set(DIRS
-# adaboost 
+#  adaboost 
   amf
   cf
   decision_stump
diff --git a/src/mlpack/methods/adaboost/adaboost.hpp b/src/mlpack/methods/adaboost/adaboost.hpp
index 8aafb07..e9a30ce 100644
--- a/src/mlpack/methods/adaboost/adaboost.hpp
+++ b/src/mlpack/methods/adaboost/adaboost.hpp
@@ -9,7 +9,7 @@
 #define _MLPACK_METHODS_ADABOOST_ADABOOST_HPP
 
 #include <mlpack/core.hpp>
-#include "../perceptron/main/perceptron.hpp"
+#include <mlpack/methods/perceptron/perceptron.hpp>
  
 namespace mlpack {
 namespace adaboost {
diff --git a/src/mlpack/methods/adaboost/adaboost_impl.hpp b/src/mlpack/methods/adaboost/adaboost_impl.hpp
index da99936..8b335be 100644
--- a/src/mlpack/methods/adaboost/adaboost_impl.hpp
+++ b/src/mlpack/methods/adaboost/adaboost_impl.hpp
@@ -20,6 +20,9 @@ Adaboost<MatType, WeakLearner>::Adaboost(const MatType& data, const arma::Row<si
 {
   int j, i;
   
+  // note: put a fail safe for classes or remove it entirely
+  // by using unique function.
+
   // load the initial weights
   
   const double initWeight = 1 / (data.n_cols * classes);
@@ -34,14 +37,9 @@ Adaboost<MatType, WeakLearner>::Adaboost(const MatType& data, const arma::Row<si
   {
     rt = 0.0;
     zt = 0.0;
-
-    //transform data, as per rules for perceptron
-    for (j = 0;j < tempData.n_cols;j++)
-      tempData.col(i) = D(i) * tempData.col(i);
-
-    // for now, perceptron initialized with default parameters
-    //mlpack::perceptron::Perceptron<> p(tempData, labels, 1000);
-    WeakLearner w(other);
+    
+    // call the other weak learner and train the labels.
+    WeakLearner w(other, tempData, D, labels);
     w.Classify(tempData, predictedLabels);
 
     // Now, start calculation of alpha(t)
diff --git a/src/mlpack/methods/adaboost/adaboost_main.cpp b/src/mlpack/methods/adaboost/adaboost_main.cpp
index 1c30be0..28b6597 100644
--- a/src/mlpack/methods/adaboost/adaboost_main.cpp
+++ b/src/mlpack/methods/adaboost/adaboost_main.cpp
@@ -11,6 +11,7 @@
 using namespace mlpack;
 using namespace std;
 using namespace arma;
+using namespace mlpack::adaboost;
 
 PROGRAM_INFO("","");
 
@@ -25,7 +26,7 @@ PARAM_STRING("output", "The file in which the predicted labels for the test set"
     " will be written.", "o", "output.csv");
 PARAM_INT("iterations","The maximum number of boosting iterations "
   "to be run", "i", 1000);
-PARAM_INT("classes","The number of classes in the input label set.","c");
+PARAM_INT_REQ("classes","The number of classes in the input label set.","c");
 
 int main(int argc, char *argv[])
 {
@@ -38,7 +39,26 @@ int main(int argc, char *argv[])
   const string labelsFilename = CLI::GetParam<string>("labels_file");
   // Load labels.
   mat labelsIn;
-  data::Load(labelsFilename, labelsIn, true);
+  // data::Load(labelsFilename, labelsIn, true);
+
+  if (CLI::HasParam("labels_file"))
+  {
+    const string labelsFilename = CLI::GetParam<string>("labels_file");
+    // Load labels.
+    data::Load(labelsFilename, labelsIn, true);
+
+    // Do the labels need to be transposed?
+    if (labelsIn.n_rows == 1)
+      labelsIn = labelsIn.t();
+  }
+  else
+  {
+    // Extract the labels as the last
+    Log::Info << "Using the last dimension of training set as labels." << endl;
+
+    labelsIn = trainingData.row(trainingData.n_rows - 1).t();
+    trainingData.shed_row(trainingData.n_rows - 1);
+  }
 
   // helpers for normalizing the labels
   Col<size_t> labels;
@@ -61,15 +81,21 @@ int main(int argc, char *argv[])
         << ")!" << std::endl;
   int iterations = CLI::GetParam<int>("iterations");
   
+  int classes = 6;
+
+  // define your own weak learner, perceptron in this case.
+  int iter = 1000;
+  perceptron::Perceptron<> p(trainingData, labels, iter);
+  // 
   Timer::Start("Training");
-  Adaboost<> a(trainingData, labels, iterations, classes);
+  Adaboost<> a(trainingData, labels, iterations, classes, p);
   Timer::Stop("Training");
 
-  vec results;
-  data::RevertLabels(predictedLabels, mappings, results);
+  // vec results;
+  // data::RevertLabels(predictedLabels, mappings, results);
 
-  const string outputFilename = CLI::GetParam<string>("output");
-  data::Save(outputFilename, results, true, true);
+  // const string outputFilename = CLI::GetParam<string>("output");
+  // data::Save(outputFilename, results, true, true);
 
   return 0;
 }
\ No newline at end of file
diff --git a/src/mlpack/methods/perceptron/perceptron.hpp b/src/mlpack/methods/perceptron/perceptron.hpp
index 2ef86d2..a8878c6 100644
--- a/src/mlpack/methods/perceptron/perceptron.hpp
+++ b/src/mlpack/methods/perceptron/perceptron.hpp
@@ -58,16 +58,12 @@ class Perceptron
    *
    *
    */
-  Perceptron(const Perceptron<>& p);
+  Perceptron(const Perceptron<>& other, MatType& data, const arma::Row<double>& D, const arma::Row<size_t>& labels);
 
-  /**
-   *
-   *
-   *
-   *
-   ModifyData(MatType& data, const arma::Row<double>& D);
-   */
 private:
+  //! To store the number of iterations
+  size_t iter;
+
   //! Stores the class labels for the input data.
   arma::Row<size_t> classLabels;
 
@@ -76,6 +72,12 @@ private:
 
   //! Stores the training data to be used later on in UpdateWeights.
   arma::mat trainData;
+
+  /**
+   * Train function.
+   *
+   */
+  void Train();
 };
 
 } // namespace perceptron
diff --git a/src/mlpack/methods/perceptron/perceptron_impl.hpp b/src/mlpack/methods/perceptron/perceptron_impl.hpp
index 48c53d4..a199248 100644
--- a/src/mlpack/methods/perceptron/perceptron_impl.hpp
+++ b/src/mlpack/methods/perceptron/perceptron_impl.hpp
@@ -45,45 +45,11 @@ Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Perceptron(
   zOnes.fill(1);
   trainData.insert_rows(0, zOnes);
 
-  int j, i = 0;
-  bool converged = false;
-  size_t tempLabel;
-  arma::uword maxIndexRow, maxIndexCol;
-  arma::mat tempLabelMat;
-
-  LearnPolicy LP;
-
-  while ((i < iterations) && (!converged))
-  {
-    // This outer loop is for each iteration, and we use the 'converged'
-    // variable for noting whether or not convergence has been reached.
-    i++;
-    converged = true;
-
-    // Now this inner loop is for going through the dataset in each iteration.
-    for (j = 0; j < data.n_cols; j++)
-    {
-      // Multiply for each variable and check whether the current weight vector
-      // correctly classifies this.
-      tempLabelMat = weightVectors * trainData.col(j);
-
-      tempLabelMat.max(maxIndexRow, maxIndexCol);
-      
-      // Check whether prediction is correct.
-      if (maxIndexRow != classLabels(0, j))
-      {
-        // Due to incorrect prediction, convergence set to false.
-        converged = false;
-        tempLabel = labels(0, j);
-        // Send maxIndexRow for knowing which weight to update, send j to know
-        // the value of the vector to update it with.  Send tempLabel to know
-        // the correct class.
-        LP.UpdateWeights(trainData, weightVectors, j, tempLabel, maxIndexRow);
-      }
-    }
-  }
+  iter = iterations;
+  Train();
 }
 
+
 /**
  * Classification function. After training, use the weightVectors matrix to
  * classify test, and put the predicted classes in predictedLabels.
@@ -112,24 +78,70 @@ void Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Classify(
 
 template <typename LearnPolicy, typename WeightInitializationPolicy, typename MatType>
 Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Perceptron(
-  const Perceptron<>& p)
+  const Perceptron<>& other, MatType& data, const arma::Row<double>& D, const arma::Row<size_t>& labels)
 {
-  classLabels = p.classLabels;
+  int i;
+  //transform data, as per rules for perceptron
+  for (i = 0;i < data.n_cols; i++)
+    data.col(i) = D(i) * data.col(i);
 
-  weightVectors = p.weightVectors;
+  classLabels = labels;
+  trainData = data;
+  iter = other.iter;
 
-  trainData = p.trainData;
+  Train();
 }
 
-/*
-template <typename LearnPolicy, typename WeightInitializationPolicy, typename MatType>
-Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::ModifyData(
-  MatType& data, const arma::Row<double>& D)
+/**
+ *  Training Function. 
+ *
+ */
+template<
+    typename LearnPolicy,
+    typename WeightInitializationPolicy,
+    typename MatType
+>
+void Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Train()
 {
-  for (int j = 0;j < data.n_cols;j++)
-      data.col(i) = D(i) * data.col(i);
+  int j, i = 0;
+  bool converged = false;
+  size_t tempLabel;
+  arma::uword maxIndexRow, maxIndexCol;
+  arma::mat tempLabelMat;
+
+  LearnPolicy LP;
+
+  while ((i < iter) && (!converged))
+  {
+    // This outer loop is for each iteration, and we use the 'converged'
+    // variable for noting whether or not convergence has been reached.
+    i++;
+    converged = true;
+
+    // Now this inner loop is for going through the dataset in each iteration.
+    for (j = 0; j < trainData.n_cols; j++)
+    {
+      // Multiply for each variable and check whether the current weight vector
+      // correctly classifies this.
+      tempLabelMat = weightVectors * trainData.col(j);
+
+      tempLabelMat.max(maxIndexRow, maxIndexCol);
+      
+      // Check whether prediction is correct.
+      if (maxIndexRow != classLabels(0, j))
+      {
+        // Due to incorrect prediction, convergence set to false.
+        converged = false;
+        tempLabel = classLabels(0, j);
+        // Send maxIndexRow for knowing which weight to update, send j to know
+        // the value of the vector to update it with.  Send tempLabel to know
+        // the correct class.
+        LP.UpdateWeights(trainData, weightVectors, j, tempLabel, maxIndexRow);
+      }
+    }
+  }
 }
-*/
+
 }; // namespace perceptron
 }; // namespace mlpack
 
diff --git a/src/mlpack/methods/perceptron/perceptron_main.cpp b/src/mlpack/methods/perceptron/perceptron_main.cpp
index 2ca8218..01c5b23 100644
--- a/src/mlpack/methods/perceptron/perceptron_main.cpp
+++ b/src/mlpack/methods/perceptron/perceptron_main.cpp
@@ -38,8 +38,8 @@ PROGRAM_INFO("Perceptron",
 
 // Necessary parameters
 PARAM_STRING_REQ("train_file", "A file containing the training set.", "t");
-PARAM_STRING_REQ("labels_file", "A file containing labels for the training set.",
-  "l");
+PARAM_STRING("labels_file", "A file containing labels for the training set.",
+  "l","");
 PARAM_STRING_REQ("test_file", "A file containing the test set.", "T");
 
 // Optional parameters.

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list