[mlpack-svn] r16853 - in mlpack/trunk/src/mlpack: methods/adaboost tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Jul 24 16:34:38 EDT 2014


Author: saxena.udit
Date: Thu Jul 24 16:34:38 2014
New Revision: 16853

Log:
Adaboost test improved and now works. Improved adaboost.

Modified:
   mlpack/trunk/src/mlpack/methods/adaboost/adaboost.hpp
   mlpack/trunk/src/mlpack/methods/adaboost/adaboost_impl.hpp
   mlpack/trunk/src/mlpack/methods/adaboost/adaboost_main.cpp
   mlpack/trunk/src/mlpack/tests/adaboost_test.cpp

Modified: mlpack/trunk/src/mlpack/methods/adaboost/adaboost.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/adaboost/adaboost.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/adaboost/adaboost.hpp	Thu Jul 24 16:34:38 2014
@@ -19,15 +19,46 @@
 class Adaboost 
 {
 public:
-  arma::Row<size_t> finalHypothesis;
-
+  /**
+   *  Constructor. Currently runs the Adaboost.mh algorithm
+   *  
+   *  @param data Input data
+   *  @param labels Corresponding labels
+   *  @param iterations Number of boosting rounds 
+   *  @param other Weak Learner, which has been initialized already
+   */
   Adaboost(const MatType& data, const arma::Row<size_t>& labels,
-           int iterations, size_t classes, const WeakLearner& other);
+           int iterations, const WeakLearner& other);
 
+  /**
+   *  This function helps in building a classification Matrix which is of 
+   *  form: 
+   *  -1 if l is not the correct label
+   *  1 if l is the correct label
+   *
+   *  @param t The classification matrix to be built
+   *  @param l The labels from which the classification matrix is to be built.
+   */
   void buildClassificationMatrix(arma::mat& t, const arma::Row<size_t>& l);
 
+  /**
+   *  This function helps in building the Weight Distribution matrix
+   *  which is updated during every iteration. It calculates the 
+   *  "difficulty" in classifying a point by adding the weights for all 
+   *  instances, using D.
+   *  
+   *  @param D The 2 Dimensional weight matrix from which the weights are
+   *            to be calculated.
+   *  @param weights The output weight vector.
+   */
   void buildWeightMatrix(const arma::mat& D, arma::rowvec& weights);
 
+  // Stores the final classification of the Labels.
+  arma::Row<size_t> finalHypothesis;
+
+  // To check for the bound for the hammingLoss.
+  double ztAccumulator;
+
 }; // class Adaboost
 
 } // namespace adaboost

Modified: mlpack/trunk/src/mlpack/methods/adaboost/adaboost_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/adaboost/adaboost_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/adaboost/adaboost_impl.hpp	Thu Jul 24 16:34:38 2014
@@ -42,19 +42,21 @@
  *  @param data Input data
  *  @param labels Corresponding labels
  *  @param iterations Number of boosting rounds 
- *  @param classes Number of classes in labels
  *  @param other Weak Learner, which has been initialized already
  */
 template<typename MatType, typename WeakLearner>
 Adaboost<MatType, WeakLearner>::Adaboost(const MatType& data, 
         const arma::Row<size_t>& labels, int iterations, 
-        size_t classes, const WeakLearner& other)
+        const WeakLearner& other)
 {
-  // note: put a fail safe for the variable 'classes' or 
-  // remove it entirely by using unique function.
+  // Counting the number of classes into numClasses.
+  size_t numClasses = (arma::max(labels) - arma::min(labels)) + 1;
+
   int i, j, k;
   double rt, alphat = 0.0, zt;
   
+  ztAccumulator = 1.0; 
+  
   // To be used for prediction by the Weak Learner for prediction.
   arma::Row<size_t> predictedLabels(labels.n_cols);
   
@@ -62,7 +64,7 @@
   MatType tempData(data);
   
   // Build the classification Matrix yt from labels
-  arma::mat yt(predictedLabels.n_cols, classes);
+  arma::mat yt(predictedLabels.n_cols, numClasses);
   
   // Build a classification matrix of the form D(i,l)
   // where i is the ith instance
@@ -71,37 +73,32 @@
   
   // ht(x), to be loaded after a round of prediction every time the weak
   // learner is run, by using the buildClassificationMatrix function
-  arma::mat ht(predictedLabels.n_cols, classes);
+  arma::mat ht(predictedLabels.n_cols, numClasses);
 
   // This matrix is a helper matrix used to calculate the final hypothesis.
-  arma::mat sumFinalH(predictedLabels.n_cols, classes);
+  arma::mat sumFinalH(predictedLabels.n_cols, numClasses);
   sumFinalH.fill(0.0);
   
   // load the initial weights into a 2-D matrix
-  const double initWeight = (double) 1 / (data.n_cols * classes);
-  arma::mat D(data.n_cols, classes);
+  const double initWeight = (double) 1 / (data.n_cols * numClasses);
+  arma::mat D(data.n_cols, numClasses);
   D.fill(initWeight);
-  // D.print("The value of D after initialization.");
   
   // Weights are to be compressed into this rowvector
   // for focussing on the perceptron weights.
   arma::rowvec weights(predictedLabels.n_cols);
-  // weights.print("This is the value of weight just after initialization.");
+  
   // This is the final hypothesis.
   arma::Row<size_t> finalH(predictedLabels.n_cols);
 
-  
-  // int localErrorCount;
   // now start the boosting rounds
   for (i = 0; i < iterations; i++)
   {
-    
     // Initialized to zero in every round.
     rt = 0.0; 
     zt = 0.0;
     
     // Build the weight vectors
-
     buildWeightMatrix(D, weights);
     
     // call the other weak learner and train the labels.
@@ -111,15 +108,6 @@
     //Now from predictedLabels, build ht, the weak hypothesis
     buildClassificationMatrix(ht, predictedLabels);
     
-/*    localErrorCount = 0;
-    for (int m = 0; m < labels.n_cols; m++)
-      if (labels(m) != predictedLabels(m))
-      {
-        localErrorCount++;
-        // std::cout<<m<<"th error.\n";
-      }
-    std::cout<<"Local Error is: "<<localErrorCount<<"\n";
-    std::cout<<"Local Error Rate: "<<(double)localErrorCount/predictedLabels.n_cols<<"\n";*/
     // Now, start calculation of alpha(t) using ht
     
     // begin calculation of rt
@@ -140,7 +128,6 @@
     {
       for (k = 0;k < D.n_cols; k++)
       {  
-        
         // we calculate zt, the normalization constant
         zt += D(j,k) * exp(-1 * alphat * yt(j,k) * ht(j,k));
         D(j,k) = D(j,k) * exp(-1 * alphat * yt(j,k) * ht(j,k));
@@ -149,15 +136,17 @@
         sumFinalH(j,k) += (alphat * ht(j,k));
       }
     }
+    
     // normalization of D
-
     D = D / zt;
+    
+    // Accumulating the value of zt for the Hamming Loss bound.
+    ztAccumulator *= zt;
   }
 
   // Iterations are over, now build a strong hypothesis
   // from a weighted combination of these weak hypotheses.
   
-  // This step of storing it in a temporary row vector can be improved upon ? 
   arma::rowvec tempSumFinalH;
   arma::uword max_index;
   for (i = 0;i < sumFinalH.n_rows; i++)
@@ -167,18 +156,6 @@
     finalH(i) = max_index;
   }
   finalHypothesis = finalH;
-  // labels.print("These are the labels.");
-  // finalH.print("This is the final hypothesis.");
-  /*int counterror = 0;
-  for (i = 0; i < labels.n_cols; i++)
-    if(labels(i) != finalH(i))
-    { 
-      std::cout<<i<<"th prediction not correct!\n";
-      counterror++;
-    }
-  std::cout<<"\nFinally - There are "<<counterror<<" number of misclassified records.\n";  
-  std::cout<<"The error rate is: "<<(double)counterror/labels.n_cols;*/
-  //finalH is the final hypothesis.
 }
 
 /**

Modified: mlpack/trunk/src/mlpack/methods/adaboost/adaboost_main.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/adaboost/adaboost_main.cpp	(original)
+++ mlpack/trunk/src/mlpack/methods/adaboost/adaboost_main.cpp	Thu Jul 24 16:34:38 2014
@@ -2,7 +2,7 @@
  * @file: adaboost_main.cpp
  * @author: Udit Saxena
  *
- *
+ * 
  */
 
 #include <mlpack/core.hpp>
@@ -81,22 +81,15 @@
         << ")!" << std::endl;
   int iterations = CLI::GetParam<int>("iterations");
   
-  int classes = 3;
-  
   // define your own weak learner, perceptron in this case.
-  int iter = 4000;
+  // defining the number of iterations of the perceptron.
+  int iter = 400;
   
   perceptron::Perceptron<> p(trainingData, labels.t(), iter);
   
   Timer::Start("Training");
-  Adaboost<> a(trainingData, labels.t(), iterations, classes, p);
+  Adaboost<> a(trainingData, labels.t(), iterations, p);
   Timer::Stop("Training");
 
-  // vec results;
-  // data::RevertLabels(predictedLabels, mappings, results);
-
-  // const string outputFilename = CLI::GetParam<string>("output");
-  // data::Save(outputFilename, results, true, true);
-
   return 0;
 }
\ No newline at end of file

Modified: mlpack/trunk/src/mlpack/tests/adaboost_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/adaboost_test.cpp	(original)
+++ mlpack/trunk/src/mlpack/tests/adaboost_test.cpp	Thu Jul 24 16:34:38 2014
@@ -17,7 +17,12 @@
 
 BOOST_AUTO_TEST_SUITE(AdaboostTest);
 
-BOOST_AUTO_TEST_CASE(IrisSet)
+/**
+ *  This test case runs the Adaboost.mh algorithm on the UCI Iris dataset.
+ *  It checks whether the hamming loss breaches the upperbound, which
+ *  is provided by ztAccumulator.
+ */
+BOOST_AUTO_TEST_CASE(HammingLossBound)
 {
   arma::mat inputData;
 
@@ -38,17 +43,15 @@
   perceptron::Perceptron<> p(inputData, labels.row(0), perceptron_iter);
 
   // Define parameters for the adaboost
-  int iterations = 15;
-  int classes = 3;
-  Adaboost<> a(inputData, labels.row(0), iterations, classes, p);
+  int iterations = 100;
+  Adaboost<> a(inputData, labels.row(0), iterations, p);
   int countError = 0;
   for (size_t i = 0; i < labels.n_cols; i++)
     if(labels(i) != a.finalHypothesis(i))
-    { 
-      std::cout<<i<<" prediction not correct!\n";
       countError++;
-    }
-  std::cout<<"\nFinally - There are "<<countError<<" number of misclassified records.\n";  
-  std::cout<<"The error rate is: "<<(double)countError * 100/labels.n_cols<<"%\n";
+  double hammingLoss = (double) countError / labels.n_cols;
+
+  BOOST_REQUIRE(hammingLoss <= a.ztAccumulator);
 }
+
 BOOST_AUTO_TEST_SUITE_END();
\ No newline at end of file



More information about the mlpack-svn mailing list