[mlpack-git] master, mlpack-1.0.x: First pass -- move files to match naming policy, change initialize() to Initialize(), standardize comment formatting, fix some Doxygen commands. No serious functionality changes. (519e791)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:51:31 EST 2015


Repository : https://github.com/mlpack/mlpack

On branches: master,mlpack-1.0.x
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

>---------------------------------------------------------------

commit 519e791ef5de4d5e66bea9a40d008e6e06746e46
Author: Ryan Curtin <ryan at ratml.org>
Date:   Mon Jul 7 14:07:54 2014 +0000

    First pass -- move files to match naming policy, change initialize() to
    Initialize(), standardize comment formatting, fix some Doxygen commands.  No
    serious functionality changes.


>---------------------------------------------------------------

519e791ef5de4d5e66bea9a40d008e6e06746e46
 src/mlpack/methods/perceptron/CMakeLists.txt       |   6 +-
 .../InitializationMethods/random_init.hpp          |  31 ------
 .../perceptron/InitializationMethods/zero_init.hpp |  34 ------
 .../CMakeLists.txt                                 |   0
 .../initialization_methods/random_init.hpp         |  35 ++++++
 .../initialization_methods/zero_init.hpp           |  37 +++++++
 .../CMakeLists.txt                                 |   2 +-
 .../simple_weight_update.hpp}                      |   0
 src/mlpack/methods/perceptron/perceptron.hpp       | 104 ++++++++----------
 src/mlpack/methods/perceptron/perceptron_impl.cpp  | 118 --------------------
 src/mlpack/methods/perceptron/perceptron_impl.hpp  | 122 +++++++++++++++++++++
 11 files changed, 243 insertions(+), 246 deletions(-)

diff --git a/src/mlpack/methods/perceptron/CMakeLists.txt b/src/mlpack/methods/perceptron/CMakeLists.txt
index c25c549..fa5e980 100644
--- a/src/mlpack/methods/perceptron/CMakeLists.txt
+++ b/src/mlpack/methods/perceptron/CMakeLists.txt
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 2.8)
 # Anything not in this list will not be compiled into MLPACK.
 set(SOURCES
   perceptron.hpp
-  perceptron_impl.cpp
+  perceptron_impl.hpp
 )
 
 # Add directory name to sources.
@@ -16,8 +16,8 @@ endforeach()
 # the parent scope).
 set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
 
-add_subdirectory(InitializationMethods)
-add_subdirectory(LearnPolicy)
+add_subdirectory(initialization_methods)
+add_subdirectory(learning_policies)
 
 add_executable(percep
   perceptron_main.cpp
diff --git a/src/mlpack/methods/perceptron/InitializationMethods/random_init.hpp b/src/mlpack/methods/perceptron/InitializationMethods/random_init.hpp
deleted file mode 100644
index 7cdeb19..0000000
--- a/src/mlpack/methods/perceptron/InitializationMethods/random_init.hpp
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- *  @file: randominit.hpp
- *  @author: Udit Saxena
- *
- */
-
-#ifndef _MLPACK_METHOS_PERCEPTRON_RANDOMINIT
-#define _MLPACK_METHOS_PERCEPTRON_RANDOMINIT
-
-#include <mlpack/core.hpp>
-/*
-This class is used to initialize weights for the 
-weightVectors matrix in a random manner. 
-*/
-namespace mlpack {
-namespace perceptron {
-  class RandomInitialization
-  {
-  public:
-    RandomInitialization()
-    { }
-
-    inline static void initialize(arma::mat& W, size_t row, size_t col)
-    {
-      W = arma::randu<arma::mat>(row,col);
-    }
-  }; // class RandomInitialization
-}; // namespace perceptron
-}; // namespace mlpack
-
-#endif
\ No newline at end of file
diff --git a/src/mlpack/methods/perceptron/InitializationMethods/zero_init.hpp b/src/mlpack/methods/perceptron/InitializationMethods/zero_init.hpp
deleted file mode 100644
index 7115c81..0000000
--- a/src/mlpack/methods/perceptron/InitializationMethods/zero_init.hpp
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- *  @file: zeroinit.hpp
- *  @author: Udit Saxena
- *
- */
-
-#ifndef _MLPACK_METHOS_PERCEPTRON_ZEROINIT
-#define _MLPACK_METHOS_PERCEPTRON_ZEROINIT
-
-#include <mlpack/core.hpp>
-/*
-This class is used to initialize the matrix
-weightVectors to zero.
-*/
-namespace mlpack {
-namespace perceptron {
-  class ZeroInitialization
-  {
-  public:
-    ZeroInitialization()
-    { }
-
-    inline static void initialize(arma::mat& W, size_t row, size_t col)
-    {
-      arma::mat tempWeights(row, col);
-      tempWeights.fill(0.0);
-
-      W = tempWeights;
-    }
-  }; // class ZeroInitialization
-}; // namespace perceptron
-}; // namespace mlpack
-
-#endif
\ No newline at end of file
diff --git a/src/mlpack/methods/perceptron/InitializationMethods/CMakeLists.txt b/src/mlpack/methods/perceptron/initialization_methods/CMakeLists.txt
similarity index 100%
rename from src/mlpack/methods/perceptron/InitializationMethods/CMakeLists.txt
rename to src/mlpack/methods/perceptron/initialization_methods/CMakeLists.txt
diff --git a/src/mlpack/methods/perceptron/initialization_methods/random_init.hpp b/src/mlpack/methods/perceptron/initialization_methods/random_init.hpp
new file mode 100644
index 0000000..f88e952
--- /dev/null
+++ b/src/mlpack/methods/perceptron/initialization_methods/random_init.hpp
@@ -0,0 +1,35 @@
+/**
+ * @file random_init.hpp
+ * @author Udit Saxena
+ *
+ * Random initialization for perceptron weights.
+ */
+#ifndef _MLPACK_METHOS_PERCEPTRON_INITIALIZATION_METHODS_RANDOM_INIT_HPP
+#define _MLPACK_METHOS_PERCEPTRON_INITIALIZATION_METHODS_RANDOM_INIT_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack {
+namespace perceptron {
+
+/**
+ * This class is used to initialize weights for the weightVectors matrix in a
+ * random manner.
+ */
+class RandomInitialization
+{
+ public:
+  RandomInitialization() { }
+
+  inline static void Initialize(arma::mat& W,
+                                const size_t row,
+                                const size_t col)
+  {
+    W = arma::randu<arma::mat>(row, col);
+  }
+}; // class RandomInitialization
+
+}; // namespace perceptron
+}; // namespace mlpack
+
+#endif
diff --git a/src/mlpack/methods/perceptron/initialization_methods/zero_init.hpp b/src/mlpack/methods/perceptron/initialization_methods/zero_init.hpp
new file mode 100644
index 0000000..0a02459
--- /dev/null
+++ b/src/mlpack/methods/perceptron/initialization_methods/zero_init.hpp
@@ -0,0 +1,37 @@
+/**
+ * @file zero_init.hpp
+ * @author Udit Saxena
+ *
+ * Implementation of ZeroInitialization policy for perceptrons.
+ */
+#ifndef _MLPACK_METHOS_PERCEPTRON_INITIALIZATION_METHODS_ZERO_INIT_HPP
+#define _MLPACK_METHOS_PERCEPTRON_INITIALIZATION_METHODS_ZERO_INIT_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack {
+namespace perceptron {
+
+/**
+ * This class is used to initialize the matrix weightVectors to zero.
+ */
+class ZeroInitialization
+{
+ public:
+  ZeroInitialization() { }
+
+  inline static void Initialize(arma::mat& W,
+                                const size_t row,
+                                const size_t col)
+  {
+    arma::mat tempWeights(row, col);
+    tempWeights.fill(0.0);
+
+    W = tempWeights;
+  }
+}; // class ZeroInitialization
+
+}; // namespace perceptron
+}; // namespace mlpack
+
+#endif
diff --git a/src/mlpack/methods/perceptron/LearnPolicy/CMakeLists.txt b/src/mlpack/methods/perceptron/learning_policies/CMakeLists.txt
similarity index 93%
rename from src/mlpack/methods/perceptron/LearnPolicy/CMakeLists.txt
rename to src/mlpack/methods/perceptron/learning_policies/CMakeLists.txt
index a07bc01..119239d 100644
--- a/src/mlpack/methods/perceptron/LearnPolicy/CMakeLists.txt
+++ b/src/mlpack/methods/perceptron/learning_policies/CMakeLists.txt
@@ -1,7 +1,7 @@
 # Define the files we need to compile
 # Anything not in this list will not be compiled into MLPACK.
 set(SOURCES
-  SimpleWeightUpdate.hpp
+  simple_weight_update.hpp
 )
 
 # Add directory name to sources.
diff --git a/src/mlpack/methods/perceptron/LearnPolicy/SimpleWeightUpdate.hpp b/src/mlpack/methods/perceptron/learning_policies/simple_weight_update.hpp
similarity index 100%
rename from src/mlpack/methods/perceptron/LearnPolicy/SimpleWeightUpdate.hpp
rename to src/mlpack/methods/perceptron/learning_policies/simple_weight_update.hpp
diff --git a/src/mlpack/methods/perceptron/perceptron.hpp b/src/mlpack/methods/perceptron/perceptron.hpp
index 7d26875..7842e35 100644
--- a/src/mlpack/methods/perceptron/perceptron.hpp
+++ b/src/mlpack/methods/perceptron/perceptron.hpp
@@ -1,86 +1,72 @@
-/*
- * @file: perceptron.hpp
- * @author: Udit Saxena
+/**
+ * @file perceptron.hpp
+ * @author Udit Saxena
  *
- *
- * Definition of Perceptron
+ * Definition of Perceptron class.
  */
-
-#ifndef _MLPACK_METHODS_PERCEPTRON_HPP
-#define _MLPACK_METHODS_PERCEPTRON_HPP
+#ifndef __MLPACK_METHODS_PERCEPTRON_PERCEPTRON_HPP
+#define __MLPACK_METHODS_PERCEPTRON_PERCEPTRON_HPP
 
 #include <mlpack/core.hpp>
-#include "InitializationMethods/zero_init.hpp"
-#include "InitializationMethods/random_init.hpp"
-#include "LearnPolicy/SimpleWeightUpdate.hpp"
 
+#include "initialization_methods/zero_init.hpp"
+#include "initialization_methods/random_init.hpp"
+#include "learning_policies/simple_weight_update.hpp"
 
 namespace mlpack {
 namespace perceptron {
 
-template <typename LearnPolicy = SimpleWeightUpdate, 
-          typename WeightInitializationPolicy = ZeroInitialization, 
-          typename MatType = arma::mat>
+/**
+ * This class implements a simple perceptron (i.e., a single layer neural
+ * network).  It converges if the supplied training dataset is linearly
+ * separable.
+ *
+ * @tparam LearnPolicy Options of SimpleWeightUpdate and GradientDescent.
+ * @tparam WeightInitializationPolicy Option of ZeroInitialization and
+ *      RandomInitialization.
+ */
+template<typename LearnPolicy = SimpleWeightUpdate,
+         typename WeightInitializationPolicy = ZeroInitialization,
+         typename MatType = arma::mat>
 class Perceptron
 {
-  /*
-  This class implements a simple perceptron i.e. a single layer 
-  neural network. It converges if the supplied training dataset is 
-  linearly separable.
-
-  LearnPolicy: Options of SimpleWeightUpdate and GradientDescent.
-  WeightInitializationPolicy: Option of ZeroInitialization and 
-                              RandomInitialization.
-  */
-public:
-  /*
-  Constructor - Constructs the perceptron. Or rather, builds the weightVectors
-  matrix, which is later used in Classification. 
-  It adds a bias input vector of 1 to the input data to take care of the bias
-  weights.
-
-  @param: data - Input, training data.
-  @param: labels - Labels of dataset.
-  @param: iterations - maximum number of iterations the perceptron
-                       learn algorithm is to be run.
-  */
+ public:
+  /**
+   * Constructor - constructs the perceptron by building the weightVectors
+   * matrix, which is later used in Classification.  It adds a bias input vector
+   * of 1 to the input data to take care of the bias weights.
+   *
+   * @param data Input, training data.
+   * @param labels Labels of dataset.
+   * @param iterations Maximum number of iterations for the perceptron learning
+   *     algorithm.
+   */
   Perceptron(const MatType& data, const arma::Row<size_t>& labels, int iterations);
 
-  /*
-  Classification function. After training, use the weightVectors matrix to 
-  classify test, and put the predicted classes in predictedLabels.
-
-  @param: test - testing data or data to classify. 
-  @param: predictedLabels - vector to store the predicted classes after
-                            classifying test
-  */
+  /**
+   * Classification function. After training, use the weightVectors matrix to
+   * classify test, and put the predicted classes in predictedLabels.
+   *
+   * @param test Testing data or data to classify.
+   * @param predictedLabels Vector to store the predicted classes after
+   *     classifying test.
+   */
   void Classify(const MatType& test, arma::Row<size_t>& predictedLabels);
 
 private:
-  
-  /* Stores the class labels for the input data*/
+  //! Stores the class labels for the input data.
   arma::Row<size_t> classLabels;
 
-  /* Stores the weight vectors for each of the input class labels. */
+  //! Stores the weight vectors for each of the input class labels.
   arma::mat weightVectors;
 
-  /* Stores the training data to be used later on in UpdateWeights.*/
+  //! Stores the training data to be used later on in UpdateWeights.
   arma::mat trainData;
-
-  /*
-  This function is called by the constructor to update the weightVectors
-  matrix. It decreases the weights of the incorrectly classified class while
-  increasing the weight of the correct class it should have been classified to.
-
-  @param: rowIndex - index of the row which has been incorrectly predicted.
-  @param: labelIndex - index of the vector in trainData.
-  @param: vectorIndex - index of the class which should have been predicted.
-  */
-  // void UpdateWeights(size_t rowIndex, size_t labelIndex, size_t vectorIndex);
 };
+
 } // namespace perceptron
 } // namespace mlpack
 
-#include "perceptron_impl.cpp"
+#include "perceptron_impl.hpp"
 
 #endif
diff --git a/src/mlpack/methods/perceptron/perceptron_impl.cpp b/src/mlpack/methods/perceptron/perceptron_impl.cpp
deleted file mode 100644
index b29c722..0000000
--- a/src/mlpack/methods/perceptron/perceptron_impl.cpp
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- *  @file: perceptron_impl.hpp
- *  @author: Udit Saxena
- *
- */
-
-#ifndef _MLPACK_METHODS_PERCEPTRON_IMPL_CPP
-#define _MLPACK_METHODS_PERCEPTRON_IMPL_CPP
-
-#include "perceptron.hpp"
-
-namespace mlpack {
-namespace perceptron {
-
-/*
-  Constructor - Constructs the perceptron. Or rather, builds the weightVectors
-  matrix, which is later used in Classification. 
-  It adds a bias input vector of 1 to the input data to take care of the bias
-  weights.
-
-  @param: data - Input, training data.
-  @param: labels - Labels of dataset.
-   @param: iterations - maximum number of iterations the perceptron
-                       learn algorithm is to be run.
-*/
-template <typename LearnPolicy, typename WeightInitializationPolicy, typename MatType>
-Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Perceptron(const MatType& data,
-                                const arma::Row<size_t>& labels, int iterations)
-{
-  arma::Row<size_t> uniqueLabels = arma::unique(labels);
-
-  WeightInitializationPolicy WIP;
-  WIP.initialize(weightVectors, uniqueLabels.n_elem, data.n_rows + 1);
-  
-  // Start training.
-  classLabels = labels; 
-
-  trainData = data;
-  // inserting a row of 1's at the top of the training data set.
-  MatType zOnes(1, data.n_cols);
-  zOnes.fill(1);
-  trainData.insert_rows(0, zOnes);
-
-  int j, i = 0, converged = 0;
-  size_t tempLabel; 
-  arma::uword maxIndexRow, maxIndexCol;
-  double maxVal;
-  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 = 1;
-
-    // Now this inner loop is for going through the dataset in each iteration
-    for (j = 0; j < data.n_cols; j++)
-    {
-      // Multiplying for each variable and checking 
-      // whether the current weight vector correctly classifies this.
-      tempLabelMat = weightVectors * trainData.col(j);
-
-      maxVal = tempLabelMat.max(maxIndexRow, maxIndexCol);
-      maxVal *= 2;
-      //checking whether prediction is correct.
-      if(maxIndexRow != classLabels(0,j))
-      {
-        // due to incorrect prediction, convergence set to 0
-        converged = 0;
-        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);
-      }
-    }
-  }
-}
-
-/*
-  Classification function. After training, use the weightVectors matrix to 
-  classify test, and put the predicted classes in predictedLabels.
-
-  @param: test - testing data or data to classify. 
-  @param: predictedLabels - vector to store the predicted classes after
-                            classifying test
- */
-template <typename LearnPolicy, typename WeightInitializationPolicy, typename MatType>
-void Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Classify(
-                const MatType& test, arma::Row<size_t>& predictedLabels)
-{
-  int i;
-  arma::mat tempLabelMat;
-  arma::uword maxIndexRow, maxIndexCol;
-  double maxVal;
-  MatType testData = test;
-  
-  MatType zOnes(1, test.n_cols);
-  zOnes.fill(1);
-  testData.insert_rows(0, zOnes);
-  
-  for (i = 0; i < test.n_cols; i++)
-  {
-    tempLabelMat = weightVectors * testData.col(i);
-    maxVal = tempLabelMat.max(maxIndexRow, maxIndexCol);
-    maxVal *= 2;
-    predictedLabels(0,i) = maxIndexRow;
-  }
-}
-
-}; // namespace perceptron
-}; // namespace mlpack
-
-#endif
\ No newline at end of file
diff --git a/src/mlpack/methods/perceptron/perceptron_impl.hpp b/src/mlpack/methods/perceptron/perceptron_impl.hpp
new file mode 100644
index 0000000..cb63017
--- /dev/null
+++ b/src/mlpack/methods/perceptron/perceptron_impl.hpp
@@ -0,0 +1,122 @@
+/**
+ * @file perceptron_impl.hpp
+ * @author Udit Saxena
+ *
+ * Implementation of Perceptron class.
+ */
+#ifndef __MLPACK_METHODS_PERCEPTRON_PERCEPTRON_IMPL_HPP
+#define __MLPACK_METHODS_PERCEPTRON_PERCEPTRON_IMPL_HPP
+
+#include "perceptron.hpp"
+
+namespace mlpack {
+namespace perceptron {
+
+/**
+ * Constructor - constructs the perceptron. Or rather, builds the weightVectors
+ * matrix, which is later used in Classification.
+ * It adds a bias input vector of 1 to the input data to take care of the bias
+ * weights.
+ *
+ * @param data Input, training data.
+ * @param labels Labels of dataset.
+ * @param iterations Maximum number of iterations for the perceptron learning
+ *      algorithm.
+ */
+template<
+    typename LearnPolicy,
+    typename WeightInitializationPolicy,
+    typename MatType
+>
+Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Perceptron(
+    const MatType& data,
+    const arma::Row<size_t>& labels,
+    int iterations)
+{
+  arma::Row<size_t> uniqueLabels = arma::unique(labels);
+
+  WeightInitializationPolicy WIP;
+  WIP.Initialize(weightVectors, uniqueLabels.n_elem, data.n_rows + 1);
+
+  // Start training.
+  classLabels = labels;
+
+  trainData = data;
+  // Insert a row of ones at the top of the training data set.
+  MatType zOnes(1, data.n_cols);
+  zOnes.fill(1);
+  trainData.insert_rows(0, zOnes);
+
+  int j, i = 0, converged = 0;
+  size_t tempLabel;
+  arma::uword maxIndexRow, maxIndexCol;
+  double maxVal;
+  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 = 1;
+
+    // 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);
+
+      maxVal = tempLabelMat.max(maxIndexRow, maxIndexCol);
+      maxVal *= 2;
+      // Check whether prediction is correct.
+      if (maxIndexRow != classLabels(0, j))
+      {
+        // Due to incorrect prediction, convergence set to 0.
+        converged = 0;
+        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);
+      }
+    }
+  }
+}
+
+/**
+ * Classification function. After training, use the weightVectors matrix to
+ * classify test, and put the predicted classes in predictedLabels.
+ *
+ * @param test testing data or data to classify.
+ * @param predictedLabels vector to store the predicted classes after
+ *      classifying test
+ */
+template <typename LearnPolicy, typename WeightInitializationPolicy, typename MatType>
+void Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Classify(
+                const MatType& test, arma::Row<size_t>& predictedLabels)
+{
+  arma::mat tempLabelMat;
+  arma::uword maxIndexRow, maxIndexCol;
+  double maxVal;
+  MatType testData = test;
+
+  MatType zOnes(1, test.n_cols);
+  zOnes.fill(1);
+  testData.insert_rows(0, zOnes);
+
+  for (int i = 0; i < test.n_cols; i++)
+  {
+    tempLabelMat = weightVectors * testData.col(i);
+    maxVal = tempLabelMat.max(maxIndexRow, maxIndexCol);
+    maxVal *= 2;
+    predictedLabels(0, i) = maxIndexRow;
+  }
+}
+
+}; // namespace perceptron
+}; // namespace mlpack
+
+#endif



More information about the mlpack-git mailing list