[mlpack-svn] r16771 - in mlpack/trunk/src/mlpack/methods/perceptron: . InitializationMethods LearnPolicy initialization_methods learning_policies
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Jul 7 10:07:54 EDT 2014
Author: rcurtin
Date: Mon Jul 7 10:07:54 2014
New Revision: 16771
Log:
First pass -- move files to match naming policy, change initialize() to
Initialize(), standardize comment formatting, fix some Doxygen commands. No
serious functionality changes.
Added:
mlpack/trunk/src/mlpack/methods/perceptron/initialization_methods/
- copied from r16767, /mlpack/trunk/src/mlpack/methods/perceptron/InitializationMethods/
mlpack/trunk/src/mlpack/methods/perceptron/learning_policies/
- copied from r16706, /mlpack/trunk/src/mlpack/methods/perceptron/LearnPolicy/
mlpack/trunk/src/mlpack/methods/perceptron/learning_policies/simple_weight_update.hpp
- copied unchanged from r16706, /mlpack/trunk/src/mlpack/methods/perceptron/LearnPolicy/SimpleWeightUpdate.hpp
mlpack/trunk/src/mlpack/methods/perceptron/perceptron_impl.hpp
- copied, changed from r16706, /mlpack/trunk/src/mlpack/methods/perceptron/perceptron_impl.cpp
Removed:
mlpack/trunk/src/mlpack/methods/perceptron/InitializationMethods/
mlpack/trunk/src/mlpack/methods/perceptron/LearnPolicy/
mlpack/trunk/src/mlpack/methods/perceptron/learning_policies/SimpleWeightUpdate.hpp
mlpack/trunk/src/mlpack/methods/perceptron/perceptron_impl.cpp
Modified:
mlpack/trunk/src/mlpack/methods/perceptron/CMakeLists.txt
mlpack/trunk/src/mlpack/methods/perceptron/initialization_methods/random_init.hpp
mlpack/trunk/src/mlpack/methods/perceptron/initialization_methods/zero_init.hpp
mlpack/trunk/src/mlpack/methods/perceptron/learning_policies/CMakeLists.txt
mlpack/trunk/src/mlpack/methods/perceptron/perceptron.hpp
Modified: mlpack/trunk/src/mlpack/methods/perceptron/CMakeLists.txt
==============================================================================
--- mlpack/trunk/src/mlpack/methods/perceptron/CMakeLists.txt (original)
+++ mlpack/trunk/src/mlpack/methods/perceptron/CMakeLists.txt Mon Jul 7 10:07:54 2014
@@ -4,7 +4,7 @@
# 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 @@
# 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
Modified: mlpack/trunk/src/mlpack/methods/perceptron/initialization_methods/random_init.hpp
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/perceptron/InitializationMethods/random_init.hpp (original)
+++ mlpack/trunk/src/mlpack/methods/perceptron/initialization_methods/random_init.hpp Mon Jul 7 10:07:54 2014
@@ -1,31 +1,35 @@
-/*
- * @file: randominit.hpp
- * @author: Udit Saxena
+/**
+ * @file random_init.hpp
+ * @author Udit Saxena
*
+ * Random initialization for perceptron weights.
*/
-
-#ifndef _MLPACK_METHOS_PERCEPTRON_RANDOMINIT
-#define _MLPACK_METHOS_PERCEPTRON_RANDOMINIT
+#ifndef _MLPACK_METHOS_PERCEPTRON_INITIALIZATION_METHODS_RANDOM_INIT_HPP
+#define _MLPACK_METHOS_PERCEPTRON_INITIALIZATION_METHODS_RANDOM_INIT_HPP
#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
+
+/**
+ * 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)
{
- public:
- RandomInitialization()
- { }
-
- inline static void initialize(arma::mat& W, size_t row, size_t col)
- {
- W = arma::randu<arma::mat>(row,col);
- }
- }; // class RandomInitialization
+ W = arma::randu<arma::mat>(row, col);
+ }
+}; // class RandomInitialization
+
}; // namespace perceptron
}; // namespace mlpack
-#endif
\ No newline at end of file
+#endif
Modified: mlpack/trunk/src/mlpack/methods/perceptron/initialization_methods/zero_init.hpp
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/perceptron/InitializationMethods/zero_init.hpp (original)
+++ mlpack/trunk/src/mlpack/methods/perceptron/initialization_methods/zero_init.hpp Mon Jul 7 10:07:54 2014
@@ -1,34 +1,37 @@
-/*
- * @file: zeroinit.hpp
- * @author: Udit Saxena
+/**
+ * @file zero_init.hpp
+ * @author Udit Saxena
*
+ * Implementation of ZeroInitialization policy for perceptrons.
*/
-
-#ifndef _MLPACK_METHOS_PERCEPTRON_ZEROINIT
-#define _MLPACK_METHOS_PERCEPTRON_ZEROINIT
+#ifndef _MLPACK_METHOS_PERCEPTRON_INITIALIZATION_METHODS_ZERO_INIT_HPP
+#define _MLPACK_METHOS_PERCEPTRON_INITIALIZATION_METHODS_ZERO_INIT_HPP
#include <mlpack/core.hpp>
-/*
-This class is used to initialize the matrix
-weightVectors to zero.
-*/
+
namespace mlpack {
namespace perceptron {
- class ZeroInitialization
+
+/**
+ * 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)
{
- 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
+ arma::mat tempWeights(row, col);
+ tempWeights.fill(0.0);
+
+ W = tempWeights;
+ }
+}; // class ZeroInitialization
+
}; // namespace perceptron
}; // namespace mlpack
-#endif
\ No newline at end of file
+#endif
Modified: mlpack/trunk/src/mlpack/methods/perceptron/learning_policies/CMakeLists.txt
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/perceptron/LearnPolicy/CMakeLists.txt (original)
+++ mlpack/trunk/src/mlpack/methods/perceptron/learning_policies/CMakeLists.txt Mon Jul 7 10:07:54 2014
@@ -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.
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 Mon Jul 7 10:07:54 2014
@@ -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
\ No newline at end of file
+#endif
Copied: mlpack/trunk/src/mlpack/methods/perceptron/perceptron_impl.hpp (from r16706, /mlpack/trunk/src/mlpack/methods/perceptron/perceptron_impl.cpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/perceptron/perceptron_impl.cpp (original)
+++ mlpack/trunk/src/mlpack/methods/perceptron/perceptron_impl.hpp Mon Jul 7 10:07:54 2014
@@ -1,48 +1,54 @@
-/*
- * @file: perceptron_impl.hpp
- * @author: Udit Saxena
+/**
+ * @file perceptron_impl.hpp
+ * @author Udit Saxena
*
+ * Implementation of Perceptron class.
*/
-
-#ifndef _MLPACK_METHODS_PERCEPTRON_IMPL_CPP
-#define _MLPACK_METHODS_PERCEPTRON_IMPL_CPP
+#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 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)
+/**
+ * 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);
-
+ WIP.Initialize(weightVectors, uniqueLabels.n_elem, data.n_rows + 1);
+
// Start training.
- classLabels = labels;
+ classLabels = labels;
trainData = data;
- // inserting a row of 1's at the top of the training data set.
+ // 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;
+ size_t tempLabel;
arma::uword maxIndexRow, maxIndexCol;
double maxVal;
arma::mat tempLabelMat;
@@ -51,68 +57,66 @@
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.
+ // 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
+ // 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.
+ // 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;
- //checking whether prediction is correct.
- if(maxIndexRow != classLabels(0,j))
+ // Check whether prediction is correct.
+ if (maxIndexRow != classLabels(0, j))
{
- // due to incorrect prediction, convergence set to 0
+ // 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
+ 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
+/**
+ * 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++)
+
+ 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;
+ predictedLabels(0, i) = maxIndexRow;
}
}
}; // namespace perceptron
}; // namespace mlpack
-#endif
\ No newline at end of file
+#endif
More information about the mlpack-svn
mailing list