[mlpack-svn] r16824 - in mlpack/trunk/src/mlpack: methods methods/adaboost methods/decision_stump methods/perceptron tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Jul 15 07:26:09 EDT 2014


Author: saxena.udit
Date: Tue Jul 15 07:26:08 2014
New Revision: 16824

Log:
Adaboost design issues, to be discussed, then changed later on

Added:
   mlpack/trunk/src/mlpack/methods/adaboost/
   mlpack/trunk/src/mlpack/methods/adaboost/CMakeLists.txt
   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
Modified:
   mlpack/trunk/src/mlpack/methods/CMakeLists.txt
   mlpack/trunk/src/mlpack/methods/decision_stump/decision_stump.hpp
   mlpack/trunk/src/mlpack/methods/decision_stump/decision_stump_impl.hpp
   mlpack/trunk/src/mlpack/methods/perceptron/perceptron.hpp
   mlpack/trunk/src/mlpack/methods/perceptron/perceptron_impl.hpp
   mlpack/trunk/src/mlpack/tests/perceptron_test.cpp

Modified: mlpack/trunk/src/mlpack/methods/CMakeLists.txt
==============================================================================
--- mlpack/trunk/src/mlpack/methods/CMakeLists.txt	(original)
+++ mlpack/trunk/src/mlpack/methods/CMakeLists.txt	Tue Jul 15 07:26:08 2014
@@ -1,5 +1,6 @@
 # Recurse into each method mlpack provides.
 set(DIRS
+# adaboost 
   amf
   cf
   decision_stump

Added: mlpack/trunk/src/mlpack/methods/adaboost/CMakeLists.txt
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/methods/adaboost/CMakeLists.txt	Tue Jul 15 07:26:08 2014
@@ -0,0 +1,26 @@
+cmake_minimum_required(VERSION 2.8)
+
+# Define the files we need to compile.
+# Anything not in this list will not be compiled into MLPACK.
+set(SOURCES
+  adaboost.hpp
+  adaboost_impl.hpp
+)
+
+# Add directory name to sources.
+set(DIR_SRCS)
+foreach(file ${SOURCES})
+  set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
+endforeach()
+# Append sources (with directory name) to list of all MLPACK sources (used at
+# the parent scope).
+set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
+
+add_executable(adaboost
+  adaboost_main.cpp
+)
+target_link_libraries(adaboost
+  mlpack
+)
+
+install(TARGETS adaboost RUNTIME DESTINATION bin)

Added: mlpack/trunk/src/mlpack/methods/adaboost/adaboost.hpp
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/methods/adaboost/adaboost.hpp	Tue Jul 15 07:26:08 2014
@@ -0,0 +1,31 @@
+/**
+ * @file adaboost.hpp
+ * @author Udit Saxena
+ *
+ * AdaBoost header file
+ */
+
+#ifndef _MLPACK_METHODS_ADABOOST_ADABOOST_HPP
+#define _MLPACK_METHODS_ADABOOST_ADABOOST_HPP
+
+#include <mlpack/core.hpp>
+#include "../perceptron/main/perceptron.hpp"
+ 
+namespace mlpack {
+namespace adaboost {
+
+template <typename MatType = arma::mat, typename WeakLearner = 
+          mlpack::perceptron::Perceptron<> >
+class Adaboost 
+{
+public:
+  Adaboost(const MatType& data, const arma::Row<size_t>& labels,
+           int iterations, size_t classes, const WeakLearner& other);
+}; // class Adaboost
+
+} // namespace adaboost
+} // namespace mlpack
+
+#include "adaboost_impl.hpp"
+
+#endif
\ No newline at end of file

Added: mlpack/trunk/src/mlpack/methods/adaboost/adaboost_impl.hpp
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/methods/adaboost/adaboost_impl.hpp	Tue Jul 15 07:26:08 2014
@@ -0,0 +1,87 @@
+/*
+ * @file adaboost_impl.hpp
+ * @author Udit Saxena
+ *
+ * Implementation of the AdaBoost class
+ *
+ */
+
+#ifndef _MLPACK_METHODS_ADABOOST_ADABOOST_IMPL_HPP
+#define _MLPACK_METHODS_ADABOOST_ADABOOST_IMPL_HPP
+
+#include "adaboost.hpp"
+
+namespace mlpack {
+namespace adaboost {
+
+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)
+{
+  int j, i;
+  
+  // load the initial weights
+  
+  const double initWeight = 1 / (data.n_cols * classes);
+  arma::Row<double> D(data.n_cols);
+  D.fill(initWeight);
+
+  double rt, alphat = 0.0, zt;
+  arma::Row<size_t> predictedLabels(labels.n_cols);
+  MatType tempData(data);
+  // now start the boosting rounds
+  for (i = 0; i < iterations; i++)
+  {
+    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);
+    w.Classify(tempData, predictedLabels);
+
+    // Now, start calculation of alpha(t)
+
+    // building a helper rowvector, mispredict to help in calculations.
+    // this stores the value of Yi(l)*ht(xi,l)
+    
+    arma::Row<double> mispredict(predictedLabels.n_cols);
+    
+    for(j = 0;j < predictedLabels.n_cols; j++)
+    {
+      if (predictedLabels(j) != labels(j))
+        mispredict(j) = -predictedLabels(j);
+      else
+        mispredict(j) = predictedLabels(j);
+    }
+
+    // begin calculation of rt
+
+    for (j = 0;j < predictedLabels.n_cols; j++)
+      rt +=(D(j) * mispredict(j));
+
+    // end calculation of rt
+
+    alphat = 0.5 * log((1 + rt) / (1 - rt));
+
+    // end calculation of alphat
+    
+    for (j = 0;j < mispredict.n_cols; j++)
+    {
+      zt += D(i) * exp(-1 * alphat * mispredict(i));
+      D(i) = D(i) * exp(-1 * alphat * mispredict(i));
+    }
+
+    D = D / zt;
+
+  }
+
+}
+
+} // namespace adaboost
+} // namespace mlpack
+#endif
\ No newline at end of file

Added: mlpack/trunk/src/mlpack/methods/adaboost/adaboost_main.cpp
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/methods/adaboost/adaboost_main.cpp	Tue Jul 15 07:26:08 2014
@@ -0,0 +1,75 @@
+/*
+ * @file: adaboost_main.cpp
+ * @author: Udit Saxena
+ *
+ *
+ */
+
+#include <mlpack/core.hpp>
+#include "adaboost.hpp"
+
+using namespace mlpack;
+using namespace std;
+using namespace arma;
+
+PROGRAM_INFO("","");
+
+//necessary parameters
+PARAM_STRING_REQ("train_file", "A file containing the training set.", "tr");
+PARAM_STRING_REQ("labels_file", "A file containing labels for the training set.",
+  "l");
+PARAM_STRING_REQ("test_file", "A file containing the test set.", "te");
+
+//optional parameters.
+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");
+
+int main(int argc, char *argv[])
+{
+  CLI::ParseCommandLine(argc, argv);
+
+  const string trainingDataFilename = CLI::GetParam<string>("train_file");
+  mat trainingData;
+  data::Load(trainingDataFilename, trainingData, true);
+
+  const string labelsFilename = CLI::GetParam<string>("labels_file");
+  // Load labels.
+  mat labelsIn;
+  data::Load(labelsFilename, labelsIn, true);
+
+  // helpers for normalizing the labels
+  Col<size_t> labels;
+  vec mappings;
+
+  // Do the labels need to be transposed?
+  if (labelsIn.n_rows == 1)
+    labelsIn = labelsIn.t();
+
+  // normalize the labels
+  data::NormalizeLabels(labelsIn.unsafe_col(0), labels, mappings);
+
+  const string testingDataFilename = CLI::GetParam<string>("test_file");
+  mat testingData;
+  data::Load(testingDataFilename, testingData, true);
+
+  if (testingData.n_rows != trainingData.n_rows)
+    Log::Fatal << "Test data dimensionality (" << testingData.n_rows << ") "
+        << "must be the same as training data (" << trainingData.n_rows - 1
+        << ")!" << std::endl;
+  int iterations = CLI::GetParam<int>("iterations");
+  
+  Timer::Start("Training");
+  Adaboost<> a(trainingData, labels, iterations, classes);
+  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/methods/decision_stump/decision_stump.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/decision_stump/decision_stump.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/decision_stump/decision_stump.hpp	Tue Jul 15 07:26:08 2014
@@ -53,6 +53,24 @@
    */
   void Classify(const MatType& test, arma::Row<size_t>& predictedLabels);
 
+  /**
+   *
+   *
+   *
+   *
+   */
+  DecisionStump(const DecisionStump<>& ds);
+
+  /**
+   *
+   *
+   *
+   *
+   *
+   *
+  ModifyData(MatType& data, const arma::Row<double>& D);
+  */
+  
   //! Access the splitting attribute.
   int SplitAttribute() const { return splitAttribute; }
   //! Modify the splitting attribute (be careful!).

Modified: mlpack/trunk/src/mlpack/methods/decision_stump/decision_stump_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/decision_stump/decision_stump_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/decision_stump/decision_stump_impl.hpp	Tue Jul 15 07:26:08 2014
@@ -104,6 +104,38 @@
 }
 
 /**
+ *
+ *
+ *
+ *
+ *
+ */
+template <typename MatType>
+DecisionStump<MatType>::DecisionStump(const DecisionStump<>& ds)
+{
+  numClass = ds.numClass;
+
+  splitAttribute = ds.splitAttribute;
+
+  bucketSize = ds.bucketSize;
+
+  split = ds.split;
+
+  binLabels = ds.binLabels;
+}
+
+/**
+ *
+ *
+ *
+ *
+ *
+ *
+template <typename MatType>
+DecisionStump<MatType>::ModifyData(MatType& data, const arma::Row<double>& D)
+ */
+
+/**
  * Sets up attribute as if it were splitting on it and finds entropy when
  * splitting on attribute.
  *

Modified: mlpack/trunk/src/mlpack/methods/perceptron/perceptron.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/perceptron/perceptron.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/perceptron/perceptron.hpp	Tue Jul 15 07:26:08 2014
@@ -53,6 +53,20 @@
    */
   void Classify(const MatType& test, arma::Row<size_t>& predictedLabels);
 
+  /**
+   *
+   *
+   *
+   */
+  Perceptron(const Perceptron<>& p);
+
+  /**
+   *
+   *
+   *
+   *
+   ModifyData(MatType& data, const arma::Row<double>& D);
+   */
 private:
   //! Stores the class labels for the input data.
   arma::Row<size_t> classLabels;

Modified: mlpack/trunk/src/mlpack/methods/perceptron/perceptron_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/perceptron/perceptron_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/perceptron/perceptron_impl.hpp	Tue Jul 15 07:26:08 2014
@@ -110,6 +110,26 @@
   }
 }
 
+template <typename LearnPolicy, typename WeightInitializationPolicy, typename MatType>
+Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Perceptron(
+  const Perceptron<>& p)
+{
+  classLabels = p.classLabels;
+
+  weightVectors = p.weightVectors;
+
+  trainData = p.trainData;
+}
+
+/*
+template <typename LearnPolicy, typename WeightInitializationPolicy, typename MatType>
+Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::ModifyData(
+  MatType& data, const arma::Row<double>& D)
+{
+  for (int j = 0;j < data.n_cols;j++)
+      data.col(i) = D(i) * data.col(i);
+}
+*/
 }; // namespace perceptron
 }; // namespace mlpack
 

Modified: mlpack/trunk/src/mlpack/tests/perceptron_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/perceptron_test.cpp	(original)
+++ mlpack/trunk/src/mlpack/tests/perceptron_test.cpp	Tue Jul 15 07:26:08 2014
@@ -149,4 +149,21 @@
   BOOST_CHECK_EQUAL(predictedLabels(0, 3), 1);
 }
 
+BOOST_AUTO_TEST_CASE(SecondaryConstructor)
+{
+  mat trainData;
+  trainData << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8
+            << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << endr
+            << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1
+            << 2 << 2 << 2 << 2 << 2 << 2 << 2 << 2 << endr;
+
+  Mat<size_t> labels;
+  labels << 0 << 0 << 0 << 1 << 0 << 1 << 1 << 1
+         << 0 << 0 << 0 << 1 << 0 << 1 << 1 << 1;
+         
+  Perceptron<> p1(trainData, labels.row(0), 1000);
+
+  Perceptron<> p2(p1);
+}
+
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-svn mailing list