[mlpack] 87/324: * Changes in AMF abstraction, Termination policy is made first template parameter * fixed minor formatting issues
Barak A. Pearlmutter
barak+git at cs.nuim.ie
Sun Aug 17 08:21:58 UTC 2014
This is an automated email from the git hooks/post-receive script.
bap pushed a commit to branch svn-trunk
in repository mlpack.
commit 9c6b2e9f4b80c709010ad69905229c4c1b41fb74
Author: sumedhghaisas <sumedhghaisas at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date: Fri Jun 27 00:16:05 2014 +0000
* Changes in AMF abstraction, Termination policy is made first template parameter
* fixed minor formatting issues
git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@16725 9d5b8971-822b-0410-80eb-d18c1038ef23
---
src/mlpack/methods/amf/CMakeLists.txt | 8 --
src/mlpack/methods/amf/amf.hpp | 22 ++--
src/mlpack/methods/amf/amf_impl.hpp | 27 +++--
.../amf/termination_policies/CMakeLists.txt | 4 +-
.../simple_residue_termination.hpp | 100 +++++++++---------
.../simple_tolerance_termination.hpp | 116 +++++++++++----------
src/mlpack/methods/cf/cf.hpp | 4 +-
src/mlpack/methods/nmf/nmf_main.cpp | 12 ++-
src/mlpack/tests/nmf_test.cpp | 18 ++--
9 files changed, 157 insertions(+), 154 deletions(-)
diff --git a/src/mlpack/methods/amf/CMakeLists.txt b/src/mlpack/methods/amf/CMakeLists.txt
index 2abbe85..2eb1add 100644
--- a/src/mlpack/methods/amf/CMakeLists.txt
+++ b/src/mlpack/methods/amf/CMakeLists.txt
@@ -17,11 +17,3 @@ set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
add_subdirectory(update_rules)
add_subdirectory(init_rules)
add_subdirectory(termination_policies)
-
-add_executable(amf
- amf_main.cpp
-)
-target_link_libraries(amf
- mlpack
-)
-install(TARGETS amf RUNTIME DESTINATION bin)
diff --git a/src/mlpack/methods/amf/amf.hpp b/src/mlpack/methods/amf/amf.hpp
index fa54fba..55c5398 100644
--- a/src/mlpack/methods/amf/amf.hpp
+++ b/src/mlpack/methods/amf/amf.hpp
@@ -1,9 +1,9 @@
/**
- * @file nmf_als.hpp
+ * @file amf.hpp
* @author Sumedh Ghaisas
*/
-#ifndef __MLPACK_METHODS_LMF_LMF_HPP
-#define __MLPACK_METHODS_LMF_LMF_HPP
+#ifndef __MLPACK_METHODS_AMF_AMF_HPP
+#define __MLPACK_METHODS_AMF_AMF_HPP
#include <mlpack/core.hpp>
#include <mlpack/methods/amf/update_rules/nmf_mult_dist.hpp>
@@ -45,9 +45,9 @@ namespace amf {
*
* @see NMF_MultiplicativeDistanceUpdate
*/
-template<typename InitializationRule = RandomInitialization,
- typename UpdateRule = NMFMultiplicativeDistanceUpdate,
- typename TerminationPolicy = SimpleResidueTermination>
+template<typename TerminationPolicy = SimpleResidueTermination,
+ typename InitializationRule = RandomInitialization,
+ typename UpdateRule = NMFMultiplicativeDistanceUpdate>
class AMF
{
public:
@@ -68,9 +68,9 @@ class AMF
* @param Update Optional UpdateRule object; for when the update rule for
* the W and H vector has states that it needs to store
*/
- AMF(const InitializationRule& initializeRule = InitializationRule(),
- const UpdateRule& update = UpdateRule(),
- const TerminationPolicy& t_policy = TerminationPolicy());
+ AMF(const TerminationPolicy& t_policy = TerminationPolicy(),
+ const InitializationRule& initializeRule = InitializationRule(),
+ const UpdateRule& update = UpdateRule());
/**
* Apply Latent Matrix Factorization to the provided matrix.
@@ -87,12 +87,12 @@ class AMF
arma::mat& H);
private:
+ //! termination policy
+ TerminationPolicy t_policy;
//! Instantiated initialization Rule.
InitializationRule initializeRule;
//! Instantiated update rule.
UpdateRule update;
- //! termination policy
- TerminationPolicy t_policy;
public:
//! Access the initialization rule.
diff --git a/src/mlpack/methods/amf/amf_impl.hpp b/src/mlpack/methods/amf/amf_impl.hpp
index f226cd1..4621422 100644
--- a/src/mlpack/methods/amf/amf_impl.hpp
+++ b/src/mlpack/methods/amf/amf_impl.hpp
@@ -1,5 +1,5 @@
/**
- * @file nmf_als.hpp
+ * @file amf_impl.hpp
* @author Sumedh Ghaisas
*/
namespace mlpack {
@@ -8,16 +8,16 @@ namespace amf {
/**
* Construct the LMF object.
*/
-template<typename InitializationRule,
- typename UpdateRule,
- typename TerminationPolicy>
-AMF<InitializationRule, UpdateRule, TerminationPolicy>::AMF(
+template<typename TerminationPolicy,
+ typename InitializationRule,
+ typename UpdateRule>
+AMF<TerminationPolicy, InitializationRule, UpdateRule>::AMF(
+ const TerminationPolicy& t_policy,
const InitializationRule& initializeRule,
- const UpdateRule& update,
- const TerminationPolicy& t_policy) :
+ const UpdateRule& update) :
+ t_policy(t_policy),
initializeRule(initializeRule),
- update(update),
- t_policy(t_policy)
+ update(update)
{ }
/**
@@ -28,11 +28,11 @@ AMF<InitializationRule, UpdateRule, TerminationPolicy>::AMF(
* @param H Encoding matrix to output
* @param r Rank r of the factorization
*/
-template<typename InitializationRule,
- typename UpdateRule,
- typename TerminationPolicy>
+template<typename TerminationPolicy,
+ typename InitializationRule,
+ typename UpdateRule>
template<typename MatType>
-double AMF<InitializationRule, UpdateRule, TerminationPolicy>::Apply(
+double AMF<TerminationPolicy, InitializationRule, UpdateRule>::Apply(
const MatType& V,
const size_t r,
arma::mat& W,
@@ -69,4 +69,3 @@ double AMF<InitializationRule, UpdateRule, TerminationPolicy>::Apply(
}; // namespace nmf
}; // namespace mlpack
-
diff --git a/src/mlpack/methods/amf/termination_policies/CMakeLists.txt b/src/mlpack/methods/amf/termination_policies/CMakeLists.txt
index 988153a..1a08f32 100644
--- a/src/mlpack/methods/amf/termination_policies/CMakeLists.txt
+++ b/src/mlpack/methods/amf/termination_policies/CMakeLists.txt
@@ -1,8 +1,8 @@
# Define the files we need to compile
# Anything not in this list will not be compiled into MLPACK.
set(SOURCES
- simple_residue_termination.hpp
- simple_tolerance_termination.hpp
+ simple_residue_termination.hpp
+ simple_tolerance_termination.hpp
)
# Add directory name to sources.
diff --git a/src/mlpack/methods/amf/termination_policies/simple_residue_termination.hpp b/src/mlpack/methods/amf/termination_policies/simple_residue_termination.hpp
index 1935a39..b41aebd 100644
--- a/src/mlpack/methods/amf/termination_policies/simple_residue_termination.hpp
+++ b/src/mlpack/methods/amf/termination_policies/simple_residue_termination.hpp
@@ -1,73 +1,77 @@
+/**
+ * @file simple_residue_termination.hpp
+ * @author Sumedh Ghaisas
+ */
#ifndef _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
#define _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
#include <mlpack/core.hpp>
-namespace mlpack
-{
-namespace amf
-{
+namespace mlpack {
+namespace amf {
+
class SimpleResidueTermination
{
-public:
- SimpleResidueTermination(const double minResidue = 1e-10,
- const size_t maxIterations = 10000)
+ public:
+ SimpleResidueTermination(const double minResidue = 1e-10,
+ const size_t maxIterations = 10000)
: minResidue(minResidue), maxIterations(maxIterations) { }
- template<typename MatType>
- void Initialize(MatType& V)
- {
- residue = minResidue;
- iteration = 1;
- normOld = 0;
+ template<typename MatType>
+ void Initialize(MatType& V)
+ {
+ residue = minResidue;
+ iteration = 1;
+ normOld = 0;
- const size_t n = V.n_rows;
- const size_t m = V.n_cols;
+ const size_t n = V.n_rows;
+ const size_t m = V.n_cols;
- nm = n * m;
- }
+ nm = n * m;
+ }
- bool IsConverged()
- {
- if(residue < minResidue || iteration > maxIterations) return true;
- else return false;
- }
+ bool IsConverged()
+ {
+ if(residue < minResidue || iteration > maxIterations) return true;
+ else return false;
+ }
- template<typename MatType>
- void Step(const MatType& W, const MatType& H)
- {
- // Calculate norm of WH after each iteration.
- arma::mat WH;
+ template<typename MatType>
+ void Step(const MatType& W, const MatType& H)
+ {
+ // Calculate norm of WH after each iteration.
+ arma::mat WH;
- WH = W * H;
- double norm = sqrt(accu(WH % WH) / nm);
+ WH = W * H;
+ double norm = sqrt(accu(WH % WH) / nm);
- if (iteration != 0)
- {
- residue = fabs(normOld - norm);
- residue /= normOld;
- }
+ if (iteration != 0)
+ {
+ residue = fabs(normOld - norm);
+ residue /= normOld;
+ }
- normOld = norm;
+ normOld = norm;
- iteration++;
- }
+ iteration++;
+ }
- const double& Index() { return residue; }
- const size_t& Iteration() { return iteration; }
+ const double& Index() { return residue; }
+ const size_t& Iteration() { return iteration; }
public:
- double minResidue;
- size_t maxIterations;
+ double minResidue;
+ size_t maxIterations;
+
+ double residue;
+ size_t iteration;
+ double normOld;
- double residue;
- size_t iteration;
- double normOld;
+ size_t nm;
+}; // class SimpleResidueTermination
- size_t nm;
-};
-}
-}
+}; // namespace amf
+}; // namespace mlpack
#endif // _MLPACK_METHODS_AMF_SIMPLERESIDUETERMINATION_HPP_INCLUDED
diff --git a/src/mlpack/methods/amf/termination_policies/simple_tolerance_termination.hpp b/src/mlpack/methods/amf/termination_policies/simple_tolerance_termination.hpp
index 197a36f..92f9525 100644
--- a/src/mlpack/methods/amf/termination_policies/simple_tolerance_termination.hpp
+++ b/src/mlpack/methods/amf/termination_policies/simple_tolerance_termination.hpp
@@ -1,76 +1,80 @@
+/**
+ * @file simple_tolerance_termination.hpp
+ * @author Sumedh Ghaisas
+ */
#ifndef _MLPACK_METHODS_AMF_SIMPLE_TOLERANCE_TERMINATION_HPP_INCLUDED
#define _MLPACK_METHODS_AMF_SIMPLE_TOLERANCE_TERMINATION_HPP_INCLUDED
#include <mlpack/core.hpp>
-namespace mlpack
-{
-namespace amf
-{
+namespace mlpack {
+namespace amf {
+
class SimpleToleranceTermination
{
-public:
- SimpleToleranceTermination(const double tolerance = 1e-5,
- const size_t maxIterations = 10000)
+ public:
+ SimpleToleranceTermination(const double tolerance = 1e-5,
+ const size_t maxIterations = 10000)
: tolerance(tolerance), maxIterations(maxIterations) {}
- template<typename MatType>
- void Initialize(MatType& V)
+ template<typename MatType>
+ void Initialize(MatType& V)
+ {
+ residueOld = DBL_MAX;
+ iteration = 1;
+ normOld = 0;
+ residue = DBL_MIN;
+
+ const size_t n = V.n_rows;
+ const size_t m = V.n_cols;
+
+ nm = n * m;
+ }
+
+ bool IsConverged()
+ {
+ if(((residueOld - residue) / residueOld < tolerance && iteration > 4)
+ || iteration > maxIterations) return true;
+ else return false;
+ }
+
+ template<typename MatType>
+ void Step(const MatType& W, const MatType& H)
+ {
+ // Calculate norm of WH after each iteration.
+ arma::mat WH;
+
+ WH = W * H;
+ double norm = sqrt(accu(WH % WH) / nm);
+
+ if (iteration != 0)
{
- residueOld = DBL_MAX;
- iteration = 1;
- normOld = 0;
- residue = DBL_MIN;
-
- const size_t n = V.n_rows;
- const size_t m = V.n_cols;
-
- nm = n * m;
+ residueOld = residue;
+ residue = fabs(normOld - norm);
+ residue /= normOld;
}
- bool IsConverged()
- {
- if(((residueOld - residue) / residueOld < tolerance && iteration > 4) || iteration > maxIterations) return true;
- else return false;
- }
+ normOld = norm;
- template<typename MatType>
- void Step(const MatType& W, const MatType& H)
- {
- // Calculate norm of WH after each iteration.
- arma::mat WH;
-
- WH = W * H;
- double norm = sqrt(accu(WH % WH) / nm);
-
- if (iteration != 0)
- {
- residueOld = residue;
- residue = fabs(normOld - norm);
- residue /= normOld;
- }
-
- normOld = norm;
-
- iteration++;
- }
+ iteration++;
+ }
- const double& Index() { return residue; }
- const size_t& Iteration() { return iteration; }
+ const double& Index() { return residue; }
+ const size_t& Iteration() { return iteration; }
-private:
- double tolerance;
- size_t maxIterations;
+ private:
+ double tolerance;
+ size_t maxIterations;
- size_t iteration;
- double residueOld;
- double residue;
- double normOld;
+ size_t iteration;
+ double residueOld;
+ double residue;
+ double normOld;
- size_t nm;
-};
-}
-}
+ size_t nm;
+}; // class SimpleToleranceTermination
+}; // namespace amf
+}; // namespace mlpack
#endif // _MLPACK_METHODS_AMF_SIMPLE_TOLERANCE_TERMINATION_HPP_INCLUDED
diff --git a/src/mlpack/methods/cf/cf.hpp b/src/mlpack/methods/cf/cf.hpp
index 7ca02cb..bbb8883 100644
--- a/src/mlpack/methods/cf/cf.hpp
+++ b/src/mlpack/methods/cf/cf.hpp
@@ -14,6 +14,7 @@
#include <mlpack/methods/neighbor_search/neighbor_search.hpp>
#include <mlpack/methods/amf/amf.hpp>
#include <mlpack/methods/amf/update_rules/nmf_als.hpp>
+#include <mlpack/methods/amf/termination_policies/simple_residue_termination.hpp>
#include <set>
#include <map>
#include <iostream>
@@ -54,7 +55,8 @@ namespace cf /** Collaborative filtering. */{
* Apply(arma::sp_mat& data, size_t rank, arma::mat& W, arma::mat& H).
*/
template<
- typename FactorizerType = amf::AMF<amf::RandomInitialization,
+ typename FactorizerType = amf::AMF<amf::SimpleResidueTermination,
+ amf::RandomInitialization,
amf::NMFALSUpdate> >
class CF
{
diff --git a/src/mlpack/methods/nmf/nmf_main.cpp b/src/mlpack/methods/nmf/nmf_main.cpp
index 3bbe659..4338570 100644
--- a/src/mlpack/methods/nmf/nmf_main.cpp
+++ b/src/mlpack/methods/nmf/nmf_main.cpp
@@ -107,7 +107,7 @@ int main(int argc, char** argv)
<< "rules." << std::endl;
SimpleResidueTermination srt(minResidue, maxIterations);
- AMF<> amf(RandomInitialization(), NMFMultiplicativeDistanceUpdate(), srt);
+ AMF<> amf(srt);
amf.Apply(V, r, W, H);
}
else if (updateRules == "multdiv")
@@ -115,8 +115,9 @@ int main(int argc, char** argv)
Log::Info << "Performing NMF with multiplicative divergence-based update "
<< "rules." << std::endl;
SimpleResidueTermination srt(minResidue, maxIterations);
- AMF<RandomInitialization, NMFMultiplicativeDivergenceUpdate>
- amf(RandomInitialization(), NMFMultiplicativeDivergenceUpdate(), srt);
+ AMF<SimpleResidueTermination,
+ RandomInitialization,
+ NMFMultiplicativeDivergenceUpdate> amf(srt);
amf.Apply(V, r, W, H);
}
else if (updateRules == "als")
@@ -124,8 +125,9 @@ int main(int argc, char** argv)
Log::Info << "Performing NMF with alternating least squared update rules."
<< std::endl;
SimpleResidueTermination srt(minResidue, maxIterations);
- AMF<RandomInitialization, NMFALSUpdate>
- amf(RandomInitialization(), NMFALSUpdate(), srt);
+ AMF<SimpleResidueTermination,
+ RandomInitialization,
+ NMFALSUpdate> amf(srt);
amf.Apply(V, r, W, H);
}
diff --git a/src/mlpack/tests/nmf_test.cpp b/src/mlpack/tests/nmf_test.cpp
index 1f36bdf..c66ffc3 100644
--- a/src/mlpack/tests/nmf_test.cpp
+++ b/src/mlpack/tests/nmf_test.cpp
@@ -56,8 +56,8 @@ BOOST_AUTO_TEST_CASE(NMFAcolDistTest)
const size_t r = 12;
SimpleResidueTermination srt(1e-7, 10000);
- AMF<RandomAcolInitialization<> >
- nmf(RandomAcolInitialization<>(), NMFMultiplicativeDistanceUpdate(), srt);
+ AMF<SimpleResidueTermination,RandomAcolInitialization<> >
+ nmf(srt);
nmf.Apply(v, r, w, h);
mat wh = w * h;
@@ -78,7 +78,9 @@ BOOST_AUTO_TEST_CASE(NMFRandomDivTest)
mat v = w * h;
size_t r = 12;
- AMF<RandomInitialization, NMFMultiplicativeDivergenceUpdate> nmf;
+ AMF<SimpleResidueTermination,
+ RandomInitialization,
+ NMFMultiplicativeDivergenceUpdate> nmf;
nmf.Apply(v, r, w, h);
mat wh = w * h;
@@ -102,8 +104,8 @@ BOOST_AUTO_TEST_CASE(NMFALSTest)
size_t r = 12;
SimpleResidueTermination srt(1e-12, 50000);
- AMF<RandomAcolInitialization<>, NMFALSUpdate>
- nmf(RandomAcolInitialization<>(), NMFALSUpdate(), srt);
+ AMF<SimpleResidueTermination, RandomAcolInitialization<>, NMFALSUpdate>
+ nmf(srt);
nmf.Apply(v, r, w, h);
const mat wh = w * h;
@@ -143,8 +145,7 @@ BOOST_AUTO_TEST_CASE(SparseNMFAcolDistTest)
size_t r = 15;
SimpleResidueTermination srt(1e-10, 10000);
- AMF<RandomAcolInitialization<> >
- nmf(RandomAcolInitialization<>(), NMFMultiplicativeDistanceUpdate(), srt);
+ AMF<SimpleResidueTermination, RandomAcolInitialization<> > nmf(srt);
const size_t seed = mlpack::math::RandInt(1000000);
mlpack::math::RandomSeed(seed); // Set random seed so results are the same.
nmf.Apply(v, r, w, h);
@@ -192,8 +193,7 @@ BOOST_AUTO_TEST_CASE(SparseNMFALSTest)
size_t r = 5;
SimpleResidueTermination srt(1e-10, 10000);
- AMF<RandomInitialization, NMFALSUpdate>
- nmf(RandomInitialization(), NMFALSUpdate(), srt);
+ AMF<SimpleResidueTermination, RandomInitialization, NMFALSUpdate> nmf(srt);
const size_t seed = mlpack::math::RandInt(1000000);
mlpack::math::RandomSeed(seed);
nmf.Apply(v, r, w, h);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git
More information about the debian-science-commits
mailing list