[mlpack-git] master: Refactor constructors; rename Encode() to Train(). (458bd20)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Thu Dec 3 16:58:17 EST 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/9e76f0b82b8bc4fe038179abe77a78146b40c195...458bd20685bdfd8a7918e1b64c45161f965e0080
>---------------------------------------------------------------
commit 458bd20685bdfd8a7918e1b64c45161f965e0080
Author: ryan <ryan at ratml.org>
Date: Thu Dec 3 16:57:56 2015 -0500
Refactor constructors; rename Encode() to Train().
>---------------------------------------------------------------
458bd20685bdfd8a7918e1b64c45161f965e0080
src/mlpack/methods/sparse_coding/sparse_coding.hpp | 54 ++++++++++++++++++----
.../methods/sparse_coding/sparse_coding_impl.hpp | 47 +++++++++++++++----
.../methods/sparse_coding/sparse_coding_main.cpp | 10 ++--
3 files changed, 88 insertions(+), 23 deletions(-)
diff --git a/src/mlpack/methods/sparse_coding/sparse_coding.hpp b/src/mlpack/methods/sparse_coding/sparse_coding.hpp
index 59c1ef8..b1d33b5 100644
--- a/src/mlpack/methods/sparse_coding/sparse_coding.hpp
+++ b/src/mlpack/methods/sparse_coding/sparse_coding.hpp
@@ -109,21 +109,40 @@ class SparseCoding
{
public:
/**
- * Set the parameters to SparseCoding. lambda2 defaults to 0.
+ * Set the parameters to SparseCoding. lambda2 defaults to 0. This
+ * constructor will train the model. If that is not desired, call the other
+ * constructor that does not take a data matrix. This constructor will also
+ * initialize the dictionary using the DictionaryInitializer before training.
*
- * @param data Data matrix
- * @param atoms Number of atoms in dictionary
- * @param lambda1 Regularization parameter for l1-norm penalty
- * @param lambda2 Regularization parameter for l2-norm penalty
+ * @param data Data matrix.
+ * @param atoms Number of atoms in dictionary.
+ * @param lambda1 Regularization parameter for l1-norm penalty.
+ * @param lambda2 Regularization parameter for l2-norm penalty.
+ * @param maxIterations Maximum number of iterations to run algorithm. If 0,
+ * the algorithm will run until convergence (or forever).
+ * @param objTolerance Tolerance for objective function. When an iteration of
+ * the algorithm produces an improvement smaller than this, the algorithm
+ * will terminate.
+ * @param newtonTolerance Tolerance for the Newton's method dictionary
+ * optimization step.
*/
SparseCoding(const arma::mat& data,
const size_t atoms,
const double lambda1,
- const double lambda2 = 0);
+ const double lambda2 = 0,
+ const size_t maxIterations = 0,
+ const double objTolerance = 0.01,
+ const double newtonTolerance = 1e-6);
/**
- * Run Sparse Coding with Dictionary Learning.
+ * Set the parameters to SparseCoding. lambda2 defaults to 0. This
+ * constructor will not train the model, and a subsequent call to Train() will
+ * be required before the model can encode points with Code(). This
+ * constructor will initialize the dictionary, though.
*
+ * @param atoms Number of atoms in dictionary.
+ * @param lambda1 Regularization parameter for l1-norm penalty.
+ * @param lambda2 Regularization parameter for l2-norm penalty.
* @param maxIterations Maximum number of iterations to run algorithm. If 0,
* the algorithm will run until convergence (or forever).
* @param objTolerance Tolerance for objective function. When an iteration of
@@ -132,9 +151,17 @@ class SparseCoding
* @param newtonTolerance Tolerance for the Newton's method dictionary
* optimization step.
*/
- void Encode(const size_t maxIterations = 0,
- const double objTolerance = 0.01,
- const double newtonTolerance = 1e-6);
+ SparseCoding(const size_t atoms,
+ const double lambda1,
+ const double lambda2 = 0,
+ const size_t maxIterations = 0,
+ const double objTolerance = 0.01,
+ const double newtonTolerance = 1e-6);
+
+ /**
+ * Train the sparse coding model on the given dataset.
+ */
+ void Train(const arma::mat& data);
/**
* Sparse code each point via LARS.
@@ -201,6 +228,13 @@ class SparseCoding
//! l2 regularization term.
double lambda2;
+
+ //! Maximum number of iterations during training.
+ size_t maxIterations;
+ //! Tolerance for main objective.
+ double objTolerance;
+ //! Tolerance for Newton's method (dictionary training).
+ double newtonTolerance;
};
} // namespace sparse_coding
diff --git a/src/mlpack/methods/sparse_coding/sparse_coding_impl.hpp b/src/mlpack/methods/sparse_coding/sparse_coding_impl.hpp
index 35b0806..0da7e4e 100644
--- a/src/mlpack/methods/sparse_coding/sparse_coding_impl.hpp
+++ b/src/mlpack/methods/sparse_coding/sparse_coding_impl.hpp
@@ -15,32 +15,61 @@ namespace mlpack {
namespace sparse_coding {
template<typename DictionaryInitializer>
-SparseCoding<DictionaryInitializer>::SparseCoding(const arma::mat& data,
- const size_t atoms,
- const double lambda1,
- const double lambda2) :
+SparseCoding<DictionaryInitializer>::SparseCoding(
+ const arma::mat& data,
+ const size_t atoms,
+ const double lambda1,
+ const double lambda2,
+ const size_t maxIterations,
+ const double objTolerance,
+ const double newtonTolerance) :
atoms(atoms),
data(data),
codes(atoms, data.n_cols),
lambda1(lambda1),
- lambda2(lambda2)
+ lambda2(lambda2),
+ maxIterations(maxIterations),
+ objTolerance(objTolerance),
+ newtonTolerance(newtonTolerance)
+{
+ // Initialize the dictionary.
+ DictionaryInitializer::Initialize(data, atoms, dictionary);
+
+ Train(data);
+}
+
+template<typename DictionaryInitializer>
+SparseCoding<DictionaryInitializer>::SparseCoding(
+ const size_t atoms,
+ const double lambda1,
+ const double lambda2,
+ const size_t maxIterations,
+ const double objTolerance,
+ const double newtonTolerance) :
+ atoms(atoms),
+ data(data),
+ codes(atoms, data.n_cols),
+ lambda1(lambda1),
+ lambda2(lambda2),
+ maxIterations(maxIterations),
+ objTolerance(objTolerance),
+ newtonTolerance(newtonTolerance)
{
// Initialize the dictionary.
DictionaryInitializer::Initialize(data, atoms, dictionary);
}
template<typename DictionaryInitializer>
-void SparseCoding<DictionaryInitializer>::Encode(const size_t maxIterations,
- const double objTolerance,
- const double newtonTolerance)
+void SparseCoding<DictionaryInitializer>::Train(const arma::mat& data)
{
+ // Now, train.
Timer::Start("sparse_coding");
double lastObjVal = DBL_MAX;
// Take the initial coding step, which has to happen before entering the main
// optimization loop.
- Log::Info << "Initial Coding Step." << std::endl;
+ Log::Info << "Initial coding step." << std::endl;
OptimizeCode();
arma::uvec adjacencies = find(codes);
diff --git a/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp b/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp
index 70905f6..5c6ea25 100644
--- a/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp
+++ b/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp
@@ -114,7 +114,8 @@ int main(int argc, char* argv[])
// If there is an initial dictionary, be sure we do not initialize one.
if (initialDictionaryFile != "")
{
- SparseCoding<NothingInitializer> sc(matX, atoms, lambda1, lambda2);
+ SparseCoding<NothingInitializer> sc(atoms, lambda1, lambda2, maxIterations,
+ objTolerance, newtonTolerance);
// Load initial dictionary directly into sparse coding object.
data::Load(initialDictionaryFile, sc.Dictionary(), true);
@@ -135,7 +136,7 @@ int main(int argc, char* argv[])
}
// Run sparse coding.
- sc.Encode(maxIterations, objTolerance, newtonTolerance);
+ sc.Train(matX);
// Save the results.
Log::Info << "Saving dictionary matrix to '" << dictionaryFile << "'.\n";
@@ -146,10 +147,11 @@ int main(int argc, char* argv[])
else
{
// No initial dictionary.
- SparseCoding<> sc(matX, atoms, lambda1, lambda2);
+ SparseCoding<> sc(atoms, lambda1, lambda2, maxIterations, objTolerance,
+ newtonTolerance);
// Run sparse coding.
- sc.Encode(maxIterations, objTolerance, newtonTolerance);
+ sc.Train(matX);
// Save the results.
Log::Info << "Saving dictionary matrix to '" << dictionaryFile << "'.\n";
More information about the mlpack-git
mailing list