[mlpack-svn] r16567 - in mlpack/trunk/src/mlpack: methods methods/amf methods/amf/init_rules methods/amf/update_rules methods/cf methods/lmf methods/nmf tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue May 27 19:49:17 EDT 2014
Author: sumedhghaisas
Date: Tue May 27 19:49:17 2014
New Revision: 16567
Log:
Changing LMF(Latent Matrix Factorization) to AMF(Alternating Matrix Factorization). NMF and CF modules are updted.
Added:
mlpack/trunk/src/mlpack/methods/amf/
- copied from r16529, /mlpack/trunk/src/mlpack/methods/nmf/
mlpack/trunk/src/mlpack/methods/amf/amf.hpp
- copied, changed from r16529, /mlpack/trunk/src/mlpack/methods/nmf/nmf.hpp
mlpack/trunk/src/mlpack/methods/amf/amf_impl.hpp
- copied, changed from r16529, /mlpack/trunk/src/mlpack/methods/nmf/nmf_impl.hpp
mlpack/trunk/src/mlpack/methods/amf/amf_main.cpp
- copied, changed from r16529, /mlpack/trunk/src/mlpack/methods/nmf/nmf_main.cpp
mlpack/trunk/src/mlpack/methods/amf/init_rules/
mlpack/trunk/src/mlpack/methods/amf/init_rules/CMakeLists.txt
mlpack/trunk/src/mlpack/methods/amf/init_rules/random_acol_init.hpp
- copied, changed from r16529, /mlpack/trunk/src/mlpack/methods/nmf/random_acol_init.hpp
mlpack/trunk/src/mlpack/methods/amf/init_rules/random_init.hpp
- copied, changed from r16529, /mlpack/trunk/src/mlpack/methods/nmf/random_init.hpp
mlpack/trunk/src/mlpack/methods/amf/update_rules/
mlpack/trunk/src/mlpack/methods/amf/update_rules/CMakeLists.txt
mlpack/trunk/src/mlpack/methods/amf/update_rules/nmf_als.hpp
- copied, changed from r16529, /mlpack/trunk/src/mlpack/methods/nmf/als_update_rules.hpp
mlpack/trunk/src/mlpack/methods/amf/update_rules/nmf_mult_dist.hpp
- copied, changed from r16529, /mlpack/trunk/src/mlpack/methods/nmf/mult_dist_update_rules.hpp
mlpack/trunk/src/mlpack/methods/amf/update_rules/nmf_mult_div.hpp
- copied, changed from r16529, /mlpack/trunk/src/mlpack/methods/nmf/mult_div_update_rules.hpp
Removed:
mlpack/trunk/src/mlpack/methods/amf/als_update_rules.hpp
mlpack/trunk/src/mlpack/methods/amf/mult_dist_update_rules.hpp
mlpack/trunk/src/mlpack/methods/amf/mult_div_update_rules.hpp
mlpack/trunk/src/mlpack/methods/amf/nmf.hpp
mlpack/trunk/src/mlpack/methods/amf/nmf_impl.hpp
mlpack/trunk/src/mlpack/methods/amf/nmf_main.cpp
mlpack/trunk/src/mlpack/methods/amf/random_acol_init.hpp
mlpack/trunk/src/mlpack/methods/amf/random_init.hpp
mlpack/trunk/src/mlpack/methods/lmf/
mlpack/trunk/src/mlpack/methods/nmf/als_update_rules.hpp
mlpack/trunk/src/mlpack/methods/nmf/mult_dist_update_rules.hpp
mlpack/trunk/src/mlpack/methods/nmf/mult_div_update_rules.hpp
mlpack/trunk/src/mlpack/methods/nmf/nmf.hpp
mlpack/trunk/src/mlpack/methods/nmf/nmf_impl.hpp
mlpack/trunk/src/mlpack/methods/nmf/random_acol_init.hpp
mlpack/trunk/src/mlpack/methods/nmf/random_init.hpp
Modified:
mlpack/trunk/src/mlpack/methods/CMakeLists.txt
mlpack/trunk/src/mlpack/methods/amf/CMakeLists.txt
mlpack/trunk/src/mlpack/methods/cf/cf.hpp
mlpack/trunk/src/mlpack/methods/nmf/CMakeLists.txt
mlpack/trunk/src/mlpack/methods/nmf/nmf_main.cpp
mlpack/trunk/src/mlpack/tests/nmf_test.cpp
mlpack/trunk/src/mlpack/tests/to_string_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 May 27 19:49:17 2014
@@ -1,5 +1,6 @@
# Recurse into each method mlpack provides.
set(DIRS
+ amf
cf
det
emst
@@ -18,7 +19,7 @@
nca
neighbor_search
nmf
- lmf
+# lmf
pca
radical
range_search
Modified: mlpack/trunk/src/mlpack/methods/amf/CMakeLists.txt
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/nmf/CMakeLists.txt (original)
+++ mlpack/trunk/src/mlpack/methods/amf/CMakeLists.txt Tue May 27 19:49:17 2014
@@ -1,13 +1,8 @@
# Define the files we need to compile
# Anything not in this list will not be compiled into MLPACK.
set(SOURCES
- mult_dist_update_rules.hpp
- mult_div_update_rules.hpp
- als_update_rules.hpp
- random_init.hpp
- random_acol_init.hpp
- nmf.hpp
- nmf_impl.hpp
+ amf.hpp
+ amf_impl.hpp
)
# Add directory name to sources.
@@ -19,10 +14,13 @@
# the parent scope).
set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
-add_executable(nmf
- nmf_main.cpp
+add_subdirectory(update_rules)
+add_subdirectory(init_rules)
+
+add_executable(amf
+ amf_main.cpp
)
-target_link_libraries(nmf
+target_link_libraries(amf
mlpack
)
-install(TARGETS nmf RUNTIME DESTINATION bin)
+install(TARGETS amf RUNTIME DESTINATION bin)
Copied: mlpack/trunk/src/mlpack/methods/amf/amf.hpp (from r16529, /mlpack/trunk/src/mlpack/methods/nmf/nmf.hpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/nmf/nmf.hpp (original)
+++ mlpack/trunk/src/mlpack/methods/amf/amf.hpp Tue May 27 19:49:17 2014
@@ -1,78 +1,52 @@
-/**
- * @file nmf.hpp
- * @author Mohan Rajendran
- *
- * Defines the NMF class to perform Non-negative Matrix Factorization
- * on the given matrix.
- */
-#ifndef __MLPACK_METHODS_NMF_NMF_HPP
-#define __MLPACK_METHODS_NMF_NMF_HPP
+#ifndef __MLPACK_METHODS_LMF_LMF_HPP
+#define __MLPACK_METHODS_LMF_LMF_HPP
#include <mlpack/core.hpp>
-#include "mult_dist_update_rules.hpp"
-#include "random_init.hpp"
+#include "update_rules/nmf_mult_dist.hpp"
+#include "init_rules/random_init.hpp"
namespace mlpack {
-namespace nmf {
+namespace amf {
/**
- * This class implements the NMF on the given matrix V. Non-negative Matrix
+ * This class implements the AMF on the given matrix V. Alternaing Matrix
* Factorization decomposes V in the form \f$ V \approx WH \f$ where W is
* called the basis matrix and H is called the encoding matrix. V is taken
* to be of size n x m and the obtained W is n x r and H is r x m. The size r is
* called the rank of the factorization.
*
- * The implementation requires two template types; the first contains the update
- * rule for the W matrix during each iteration and the other contains the update
- * rule for the H matrix during each iteration. This templatization allows the
+ * The implementation requires two template types; the first contains the
+ * initialization rule for the W and H matrix and the other contains the update
+ * rule to be used during each iteration. This templatization allows the
* user to try various update rules (including ones not supplied with MLPACK)
* for factorization.
*
- * A simple example of how to run NMF is shown below.
+ * A simple example of how to run AMF is shown below.
*
* @code
- * extern arma::mat V; // Matrix that we want to perform NMF on.
+ * extern arma::mat V; // Matrix that we want to perform LMF on.
* size_t r = 10; // Rank of decomposition
* arma::mat W; // Basis matrix
* arma::mat H; // Encoding matrix
*
- * NMF<> nmf(); // Default options
- * nmf.Apply(V, W, H, r);
+ * AMF<> amf; // Default options
+ * amf.Apply(V, W, H, r);
* @endcode
*
- * For more information on non-negative matrix factorization, see the following
- * paper:
- *
- * @code
- * @article{
- * title = {{Learning the parts of objects by non-negative matrix
- * factorization}},
- * author = {Lee, Daniel D. and Seung, H. Sebastian},
- * journal = {Nature},
- * month = {Oct},
- * year = {1999},
- * number = {6755},
- * pages = {788--791},
- * publisher = {Nature Publishing Group},
- * url = {http://dx.doi.org/10.1038/44565}
- * }
- * @endcode
- *
- * @tparam WUpdateRule The update rule for calculating W matrix at each
- * iteration.
- * @tparam HUpdateRule The update rule for calculating H matrix at each
+ * @tparam InitializationRule The initialization rule for initializing W and H
+ * matrix.
+ * @tparam UpdateRule The update rule for calculating W and H matrix at each
* iteration.
*
- * @see WMultiplicativeDistanceRule, HMultiplicativeDistanceRule
+ * @see NMF_MultiplicativeDistanceUpdate
*/
template<typename InitializationRule = RandomInitialization,
- typename WUpdateRule = WMultiplicativeDistanceRule,
- typename HUpdateRule = HMultiplicativeDistanceRule>
-class NMF
+ typename UpdateRule = NMFMultiplicativeDistanceUpdate>
+class AMF
{
public:
/**
- * Create the NMF object and (optionally) set the parameters which NMF will
+ * Create the LMF object and (optionally) set the parameters which LMF will
* run with. The minimum residue refers to the root mean square of the
* difference between two subsequent iterations of the product W * H. A low
* residue indicates that subsequent iterations are not producing much change
@@ -85,19 +59,16 @@
* terminates.
* @param Initialize Optional Initialization object for initializing the
* W and H matrices
- * @param WUpdate Optional WUpdateRule object; for when the update rule for
- * the W vector has states that it needs to store.
- * @param HUpdate Optional HUpdateRule object; for when the update rule for
- * the H vector has states that it needs to store.
+ * @param Update Optional UpdateRule object; for when the update rule for
+ * the W and H vector has states that it needs to store
*/
- NMF(const size_t maxIterations = 10000,
+ AMF(const size_t maxIterations = 10000,
const double minResidue = 1e-10,
const InitializationRule initializeRule = InitializationRule(),
- const WUpdateRule wUpdate = WUpdateRule(),
- const HUpdateRule hUpdate = HUpdateRule());
+ const UpdateRule update = UpdateRule());
/**
- * Apply Non-Negative Matrix Factorization to the provided matrix.
+ * Apply Latent Matrix Factorization to the provided matrix.
*
* @param V Input matrix to be factorized.
* @param W Basis matrix to be output.
@@ -117,10 +88,8 @@
double minResidue;
//! Instantiated initialization Rule.
InitializationRule initializeRule;
- //! Instantiated W update rule.
- WUpdateRule wUpdate;
- //! Instantiated H update rule.
- HUpdateRule hUpdate;
+ //! Instantiated update rule.
+ UpdateRule update;
public:
//! Access the maximum number of iterations.
@@ -135,24 +104,17 @@
const InitializationRule& InitializeRule() const { return initializeRule; }
//! Modify the initialization rule.
InitializationRule& InitializeRule() { return initializeRule; }
- //! Access the W update rule.
- const WUpdateRule& WUpdate() const { return wUpdate; }
- //! Modify the W update rule.
- WUpdateRule& WUpdate() { return wUpdate; }
- //! Access the H update rule.
- const HUpdateRule& HUpdate() const { return hUpdate; }
- //! Modify the H update rule.
- HUpdateRule& HUpdate() { return hUpdate; }
-
- // Returns a string representation of this object.
- std::string ToString() const;
+ //! Access the update rule.
+ const UpdateRule& Update() const { return update; }
+ //! Modify the update rule.
+ UpdateRule& Update() { return update; }
-}; // class NMF
+}; // class AMF
-}; // namespace nmf
+}; // namespace amf
}; // namespace mlpack
// Include implementation.
-#include "nmf_impl.hpp"
+#include "amf_impl.hpp"
#endif
Copied: mlpack/trunk/src/mlpack/methods/amf/amf_impl.hpp (from r16529, /mlpack/trunk/src/mlpack/methods/nmf/nmf_impl.hpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/nmf/nmf_impl.hpp (original)
+++ mlpack/trunk/src/mlpack/methods/amf/amf_impl.hpp Tue May 27 19:49:17 2014
@@ -1,42 +1,31 @@
-/**
- * @file nmf.cpp
- * @author Mohan Rajendran
- *
- * Implementation of NMF class to perform Non-Negative Matrix Factorization
- * on the given matrix.
- */
-
namespace mlpack {
-namespace nmf {
+namespace amf {
/**
- * Construct the NMF object.
+ * Construct the LMF object.
*/
template<typename InitializationRule,
- typename WUpdateRule,
- typename HUpdateRule>
-NMF<InitializationRule, WUpdateRule, HUpdateRule>::NMF(
+ typename UpdateRule>
+AMF<InitializationRule, UpdateRule>::AMF(
const size_t maxIterations,
const double minResidue,
const InitializationRule initializeRule,
- const WUpdateRule wUpdate,
- const HUpdateRule hUpdate) :
+ const UpdateRule update) :
maxIterations(maxIterations),
minResidue(minResidue),
initializeRule(initializeRule),
- wUpdate(wUpdate),
- hUpdate(hUpdate)
+ update(update)
{
if (minResidue < 0.0)
{
- Log::Warn << "NMF::NMF(): minResidue must be a positive value ("
+ Log::Warn << "AMF::AMF(): minResidue must be a positive value ("
<< minResidue << " given). Setting to the default value of 1e-10.\n";
this->minResidue = 1e-10;
}
}
/**
- * Apply Non-Negative Matrix Factorization to the provided matrix.
+ * Apply Latent Matrix Factorization to the provided matrix.
*
* @param V Input matrix to be factorized
* @param W Basis matrix to be output
@@ -44,10 +33,9 @@
* @param r Rank r of the factorization
*/
template<typename InitializationRule,
- typename WUpdateRule,
- typename HUpdateRule>
+ typename UpdateRule>
template<typename MatType>
-void NMF<InitializationRule, WUpdateRule, HUpdateRule>::Apply(
+void AMF<InitializationRule, UpdateRule>::Apply(
const MatType& V,
const size_t r,
arma::mat& W,
@@ -72,8 +60,8 @@
{
// Update step.
// Update the value of W and H based on the Update Rules provided
- wUpdate.Update(V, W, H);
- hUpdate.Update(V, W, H);
+ update.WUpdate(V, W, H);
+ update.HUpdate(V, W, H);
// Calculate norm of WH after each iteration.
WH = W * H;
@@ -90,22 +78,9 @@
iteration++;
}
- Log::Info << "NMF converged to residue of " << sqrt(residue) << " in "
+ Log::Info << "LMF converged to residue of " << sqrt(residue) << " in "
<< iteration << " iterations." << std::endl;
}
-//Return a String of the object
-template<typename InitializationRule,
- typename WUpdateRule,
- typename HUpdateRule>
-std::string NMF<InitializationRule, WUpdateRule, HUpdateRule>::ToString() const
-{
- std::ostringstream convert;
- convert << "Non negative matrix factorization [" << this << "]" << std::endl;
- convert << " Max Iterations: " << maxIterations << std::endl;
- convert << " Minimum Residue: " << minResidue<< std::endl;
- return convert.str();
-}
-
}; // namespace nmf
}; // namespace mlpack
Copied: mlpack/trunk/src/mlpack/methods/amf/amf_main.cpp (from r16529, /mlpack/trunk/src/mlpack/methods/nmf/nmf_main.cpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/nmf/nmf_main.cpp (original)
+++ mlpack/trunk/src/mlpack/methods/amf/amf_main.cpp Tue May 27 19:49:17 2014
@@ -1,49 +1,47 @@
-/**
- * @file nmf_main.cpp
- * @author Mohan Rajendran
- *
- * Main executable to run NMF.
- */
#include <mlpack/core.hpp>
-#include "nmf.hpp"
+#include "amf.hpp"
-#include "random_init.hpp"
-#include "mult_dist_update_rules.hpp"
-#include "mult_div_update_rules.hpp"
-#include "als_update_rules.hpp"
+#include "init_rules/random_init.hpp"
+#include "update_rules/nmf_mult_dist.hpp"
+#include "update_rules/nmf_mult_div.hpp"
+#include "update_rules/nmf_als.hpp"
using namespace mlpack;
-using namespace mlpack::nmf;
+using namespace mlpack::amf;
using namespace std;
// Document program.
-PROGRAM_INFO("Non-negative Matrix Factorization", "This program performs "
- "non-negative matrix factorization on the given dataset, storing the "
+PROGRAM_INFO("Alternating Matrix Factorization", "This program performs "
+ "matrix factorization on the given dataset, storing the "
"resulting decomposed matrices in the specified files. For an input "
- "dataset V, NMF decomposes V into two matrices W and H such that "
+ "dataset V, LMF decomposes V into two matrices W and H such that "
"\n\n"
"V = W * H"
"\n\n"
- "where all elements in W and H are non-negative. If V is of size (n x m),"
+ "If V is of size (n x m),"
" then W will be of size (n x r) and H will be of size (r x m), where r is "
"the rank of the factorization (specified by --rank)."
"\n\n"
- "Optionally, the desired update rules for each NMF iteration can be chosen "
+ "Optionally, the desired update rules for each AMF iteration can be chosen "
"from the following list:"
"\n\n"
" - multdist: multiplicative distance-based update rules (Lee and Seung "
- "1999)\n"
+ "1999): non-negative matrix factorization. Matrix V should contain\n"
+ "non-negative elements.\n"
" - multdiv: multiplicative divergence-based update rules (Lee and Seung "
- "1999)\n"
- " - als: alternating least squares update rules (Paatero and Tapper 1994)"
- "\n\n"
+ "1999): non-negative matrix factorization. Matrix V should contain\n"
+ "non-negative elements.\n"
+ " - als: alternating least squares update rules (Paatero and Tapper 1994)\n"
+ "non-negative matrix factorization. Matrix V should contain\n"
+ "non-negative elements.\n"
+ "\n"
"The maximum number of iterations is specified with --max_iterations, and "
"the minimum residue required for algorithm termination is specified with "
"--min_residue.");
// Parameters for program.
-PARAM_STRING_REQ("input_file", "Input dataset to perform NMF on.", "i");
+PARAM_STRING_REQ("input_file", "Input dataset to perform AMF on.", "i");
PARAM_STRING_REQ("w_file", "File to save the calculated W matrix to.", "W");
PARAM_STRING_REQ("h_file", "File to save the calculated H matrix to.", "H");
PARAM_INT_REQ("rank", "Rank of the factorization.", "r");
@@ -102,28 +100,24 @@
// Perform NMF with the specified update rules.
if (updateRules == "multdist")
{
- Log::Info << "Performing NMF with multiplicative distance-based update "
+ Log::Info << "Performing AMF with multiplicative distance-based update(Non-negative Matrix Factorization) "
<< "rules." << std::endl;
- NMF<> nmf(maxIterations, minResidue);
- nmf.Apply(V, r, W, H);
+ AMF<> amf(maxIterations, minResidue);
+ amf.Apply(V, r, W, H);
}
else if (updateRules == "multdiv")
{
- Log::Info << "Performing NMF with multiplicative divergence-based update "
+ Log::Info << "Performing NMF with multiplicative divergence-based update(Non-negative Matrix Factorization) "
<< "rules." << std::endl;
- NMF<RandomInitialization,
- WMultiplicativeDivergenceRule,
- HMultiplicativeDivergenceRule> nmf(maxIterations, minResidue);
- nmf.Apply(V, r, W, H);
+ AMF<RandomInitialization,NMFMultiplicativeDivergenceUpdate> amf(maxIterations, minResidue);
+ amf.Apply(V, r, W, H);
}
else if (updateRules == "als")
{
- Log::Info << "Performing NMF with alternating least squared update rules."
+ Log::Info << "Performing NMF with alternating least squared update rules.(Non-negative Matrix Factorization)"
<< std::endl;
- NMF<RandomInitialization,
- WAlternatingLeastSquaresRule,
- HAlternatingLeastSquaresRule> nmf(maxIterations, minResidue);
- nmf.Apply(V, r, W, H);
+ AMF<RandomInitialization, NMFALSUpdate> amf(maxIterations, minResidue);
+ amf.Apply(V, r, W, H);
}
// Save results.
Added: mlpack/trunk/src/mlpack/methods/amf/init_rules/CMakeLists.txt
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/methods/amf/init_rules/CMakeLists.txt Tue May 27 19:49:17 2014
@@ -0,0 +1,15 @@
+# Define the files we need to compile
+# Anything not in this list will not be compiled into MLPACK.
+set(SOURCES
+ random_init.hpp
+ random_acol_init.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)
Copied: mlpack/trunk/src/mlpack/methods/amf/init_rules/random_acol_init.hpp (from r16529, /mlpack/trunk/src/mlpack/methods/nmf/random_acol_init.hpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/nmf/random_acol_init.hpp (original)
+++ mlpack/trunk/src/mlpack/methods/amf/init_rules/random_acol_init.hpp Tue May 27 19:49:17 2014
@@ -8,13 +8,13 @@
* This method sets each of the columns of W by averaging p randomly chosen
* columns of V.
*/
-#ifndef __MLPACK_METHODS_NMF_RANDOM_ACOL_INIT_HPP
-#define __MLPACK_METHODS_NMF_RANDOM_ACOL_INIT_HPP
+#ifndef __MLPACK_METHODS_LMF_RANDOM_ACOL_INIT_HPP
+#define __MLPACK_METHODS_LMF_RANDOM_ACOL_INIT_HPP
#include <mlpack/core.hpp>
namespace mlpack {
-namespace nmf {
+namespace amf {
/**
* This class initializes the W matrix of the NMF algorithm by averaging p
@@ -66,7 +66,7 @@
}
}; // Class RandomAcolInitialization
-}; // namespace nmf
+}; // namespace amf
}; // namespace mlpack
#endif
Copied: mlpack/trunk/src/mlpack/methods/amf/init_rules/random_init.hpp (from r16529, /mlpack/trunk/src/mlpack/methods/nmf/random_init.hpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/nmf/random_init.hpp (original)
+++ mlpack/trunk/src/mlpack/methods/amf/init_rules/random_init.hpp Tue May 27 19:49:17 2014
@@ -5,13 +5,13 @@
* Intialization rule for Non-Negative Matrix Factorization (NMF). This simple
* initialization is performed by assigning a random matrix to W and H.
*/
-#ifndef __MLPACK_METHODS_NMF_RANDOM_INIT_HPP
-#define __MLPACK_METHODS_NMF_RANDOM_INIT_HPP
+#ifndef __MLPACK_METHODS_LMF_RANDOM_INIT_HPP
+#define __MLPACK_METHODS_LMF_RANDOM_INIT_HPP
#include <mlpack/core.hpp>
namespace mlpack {
-namespace nmf {
+namespace amf {
class RandomInitialization
{
@@ -35,7 +35,7 @@
}
};
-}; // namespace nmf
+}; // namespace amf
}; // namespace mlpack
#endif
Added: mlpack/trunk/src/mlpack/methods/amf/update_rules/CMakeLists.txt
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/methods/amf/update_rules/CMakeLists.txt Tue May 27 19:49:17 2014
@@ -0,0 +1,16 @@
+# Define the files we need to compile
+# Anything not in this list will not be compiled into MLPACK.
+set(SOURCES
+ nmf_als.hpp
+ nmf_mult_dist.hpp
+ nmf_mult_div.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)
Copied: mlpack/trunk/src/mlpack/methods/amf/update_rules/nmf_als.hpp (from r16529, /mlpack/trunk/src/mlpack/methods/nmf/als_update_rules.hpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/nmf/als_update_rules.hpp (original)
+++ mlpack/trunk/src/mlpack/methods/amf/update_rules/nmf_als.hpp Tue May 27 19:49:17 2014
@@ -1,5 +1,5 @@
/**
- * @file als_update_rules.hpp
+ * @file nmf_als.hpp
* @author Mohan Rajendran
*
* Update rules for the Non-negative Matrix Factorization. This follows a method
@@ -10,38 +10,39 @@
* \f$ \sqrt{\sum_i \sum_j(V-WH)^2} \f$ by alternately calculating W and H
* respectively while holding the other matrix constant.
*/
-#ifndef __MLPACK_METHODS_NMF_ALS_UPDATE_RULES_HPP
-#define __MLPACK_METHODS_NMF_ALS_UPDATE_RULES_HPP
+#ifndef __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_ALS_HPP
+#define __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_ALS_HPP
#include <mlpack/core.hpp>
namespace mlpack {
-namespace nmf {
+namespace amf {
/**
- * The update rule for the basis matrix W. The formula used is
- * \f[
- * W^T = \frac{HV^T}{HH^T}
- * \f]
+ * The alternating least square update rules of matrices W and H.
*/
-class WAlternatingLeastSquaresRule
+class NMFALSUpdate
{
public:
- // Empty constructor required for the WUpdateRule template.
- WAlternatingLeastSquaresRule() { }
+ // Empty constructor required for the UpdateRule template.
+ NMFALSUpdate() { }
/**
- * The update function that actually updates the W matrix. The function takes
- * in all the matrices and only changes the value of the W matrix.
+ * The update rule for the basis matrix W. The formula used is
+ * \f[
+ * W^T = \frac{HV^T}{HH^T}
+ * \f]
+ * The function takes in all the matrices and only changes the
+ * value of the W matrix.
*
* @param V Input matrix to be factorized.
* @param W Basis matrix to be updated.
* @param H Encoding matrix.
*/
template<typename MatType>
- inline static void Update(const MatType& V,
- arma::mat& W,
- const arma::mat& H)
+ inline static void WUpdate(const MatType& V,
+ arma::mat& W,
+ const arma::mat& H)
{
// The call to inv() sometimes fails; so we are using the psuedoinverse.
// W = (inv(H * H.t()) * H * V.t()).t();
@@ -56,32 +57,23 @@
}
}
}
-};
-
-/**
- * The update rule for the encoding matrix H. The formula used is
- * \f[
- * H = \frac{W^TV}{W^TW}
- * \f]
- */
-class HAlternatingLeastSquaresRule
-{
- public:
- // Empty constructor required for the HUpdateRule template.
- HAlternatingLeastSquaresRule() { }
/**
- * The update function that actually updates the H matrix. The function takes
- * in all the matrices and only changes the value of the H matrix.
+ * The update rule for the encoding matrix H. The formula used is
+ * \f[
+ * H = \frac{W^TV}{W^TW}
+ * \f]
+ * The function takes in all the matrices and only changes the
+ * value of the H matrix.
*
* @param V Input matrix to be factorized.
* @param W Basis matrix.
* @param H Encoding matrix to be updated.
*/
template<typename MatType>
- inline static void Update(const MatType& V,
- const arma::mat& W,
- arma::mat& H)
+ inline static void HUpdate(const MatType& V,
+ const arma::mat& W,
+ arma::mat& H)
{
H = pinv(W.t() * W) * W.t() * V;
@@ -96,7 +88,7 @@
}
};
-}; // namespace nmf
+}; // namespace amf
}; // namespace mlpack
#endif
Copied: mlpack/trunk/src/mlpack/methods/amf/update_rules/nmf_mult_dist.hpp (from r16529, /mlpack/trunk/src/mlpack/methods/nmf/mult_dist_update_rules.hpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/nmf/mult_dist_update_rules.hpp (original)
+++ mlpack/trunk/src/mlpack/methods/amf/update_rules/nmf_mult_dist.hpp Tue May 27 19:49:17 2014
@@ -1,5 +1,5 @@
/**
- * @file mult_dist_update_rules.hpp
+ * @file nmf_mult_dist.hpp
* @author Mohan Rajendran
*
* Update rules for the Non-negative Matrix Factorization. This follows a method
@@ -9,73 +9,64 @@
* non-increasing between subsequent iterations. Both of the update rules
* for W and H are defined in this file.
*/
-#ifndef __MLPACK_METHODS_NMF_MULT_DIST_UPDATE_RULES_HPP
-#define __MLPACK_METHODS_NMF_MULT_DIST_UPDATE_RULES_HPP
+#ifndef __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_MULT_DIST_UPDATE_RULES_HPP
+#define __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_MULT_DIST_UPDATE_RULES_HPP
#include <mlpack/core.hpp>
namespace mlpack {
-namespace nmf {
+namespace amf {
/**
- * The update rule for the basis matrix W. The formula used is
- * \f[
- * W_{ia} \leftarrow W_{ia} \frac{(VH^T)_{ia}}{(WHH^T)_{ia}}
- * \f]
+ * The multiplicative distance update rules for matrices W and H.
*/
-class WMultiplicativeDistanceRule
+class NMFMultiplicativeDistanceUpdate
{
public:
- // Empty constructor required for the WUpdateRule template.
- WMultiplicativeDistanceRule() { }
+ // Empty constructor required for the UpdateRule template.
+ NMFMultiplicativeDistanceUpdate() { }
/**
- * The update function that actually updates the W matrix. The function takes
- * in all the matrices and only changes the value of the W matrix.
+ * The update rule for the basis matrix W. The formula used is
+ * \f[
+ * W_{ia} \leftarrow W_{ia} \frac{(VH^T)_{ia}}{
+ * The function takes in all the matrices and only changes the
+ * value of the W matrix.
*
* @param V Input matrix to be factorized.
* @param W Basis matrix to be updated.
* @param H Encoding matrix.
*/
template<typename MatType>
- inline static void Update(const MatType& V,
- arma::mat& W,
- const arma::mat& H)
+ inline static void WUpdate(const MatType& V,
+ arma::mat& W,
+ const arma::mat& H)
{
W = (W % (V * H.t())) / (W * H * H.t());
}
-};
-
-/**
- * The update rule for the encoding matrix H. The formula used is
- * \f[
- * H_{a\mu} \leftarrow H_{a\mu} \frac{(W^T V)_{a\mu}}{(W^T WH)_{a\mu}}
- * \f]
- */
-class HMultiplicativeDistanceRule
-{
- public:
- // Empty constructor required for the HUpdateRule template.
- HMultiplicativeDistanceRule() { }
/**
- * The update function that actually updates the H matrix. The function takes
- * in all the matrices and only changes the value of the H matrix.
+ * The update rule for the encoding matrix H. The formula used is
+ * \f[
+ * H_{a\mu} \leftarrow H_{a\mu} \frac{(W^T V)_{a\mu}}{(W^T WH)_{a\mu}}
+ * \f]
+ * The function takes in all the matrices and only changes the
+ * value of the H matrix.
*
* @param V Input matrix to be factorized.
* @param W Basis matrix.
* @param H Encoding matrix to be updated.
*/
template<typename MatType>
- inline static void Update(const MatType& V,
- const arma::mat& W,
- arma::mat& H)
+ inline static void HUpdate(const MatType& V,
+ const arma::mat& W,
+ arma::mat& H)
{
H = (H % (W.t() * V)) / (W.t() * W * H);
}
};
-}; // namespace nmf
+}; // namespace amf
}; // namespace mlpack
#endif
Copied: mlpack/trunk/src/mlpack/methods/amf/update_rules/nmf_mult_div.hpp (from r16529, /mlpack/trunk/src/mlpack/methods/nmf/mult_div_update_rules.hpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/nmf/mult_div_update_rules.hpp (original)
+++ mlpack/trunk/src/mlpack/methods/amf/update_rules/nmf_mult_div.hpp Tue May 27 19:49:17 2014
@@ -10,37 +10,36 @@
* non-increasing between subsequent iterations. Both of the update rules
* for W and H are defined in this file.
*/
-#ifndef __MLPACK_METHODS_NMF_MULT_DIV_UPDATE_RULES_HPP
-#define __MLPACK_METHODS_NMF_MULT_DIV_UPDATE_RULES_HPP
+#ifndef __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_MULT_DIV_HPP
+#define __MLPACK_METHODS_LMF_UPDATE_RULES_NMF_MULT_DIV_HPP
#include <mlpack/core.hpp>
namespace mlpack {
-namespace nmf {
+namespace amf {
-/**
- * The update rule for the basis matrix W. The formula used is
- * \f[
- * W_{ia} \leftarrow W_{ia} \frac{\sum_{\mu} H_{a\mu} V_{i\mu}/(WH)_{i\mu}}
- * {\sum_{\nu} H_{a\nu}}
- * \f]
- */
-class WMultiplicativeDivergenceRule
+
+class NMFMultiplicativeDivergenceUpdate
{
public:
// Empty constructor required for the WUpdateRule template.
- WMultiplicativeDivergenceRule() { }
+ NMFMultiplicativeDivergenceUpdate() { }
/**
- * The update function that actually updates the W matrix. The function takes
- * in all the matrices and only changes the value of the W matrix.
+ * The update rule for the basis matrix W. The formula used is
+ * \f[
+ * W_{ia} \leftarrow W_{ia} \frac{\sum_{\mu} H_{a\mu} V_{i\mu}/(WH)_{i\mu}}
+ * {\sum_{\nu} H_{a\nu}}
+ * \f]
+ * The function takes in all the matrices and only changes the
+ * value of the W matrix.
*
* @param V Input matrix to be factorized.
* @param W Basis matrix to be updated.
* @param H Encoding matrix.
*/
template<typename MatType>
- inline static void Update(const MatType& V,
+ inline static void WUpdate(const MatType& V,
arma::mat& W,
const arma::mat& H)
{
@@ -60,44 +59,29 @@
t2.set_size(H.n_cols);
for (size_t k = 0; k < t2.n_elem; ++k)
{
- // This may produce NaNs if V(i, k) = 0.
- // Technically the math in the paper does not define what to do in
- // this case, but considering the basic intent of the update rules,
- // we'll make this modification and take t2(k) = 0.0.
t2(k) = H(j, k) * V(i, k) / t1(i, k);
- if (t2(k) != t2(k))
- t2(k) = 0.0;
}
- W(i, j) *= sum(t2) / sum(H.row(j));
+ W(i, j) = W(i, j) * sum(t2) / sum(H.row(j));
}
}
}
-};
-
-/**
- * The update rule for the encoding matrix H. The formula used is
- * \f[
- * H_{a\mu} \leftarrow H_{a\mu} \frac{\sum_{i} W_{ia} V_{i\mu}/(WH)_{i\mu}}
- * {\sum_{k} H_{ka}}
- * \f]
- */
-class HMultiplicativeDivergenceRule
-{
- public:
- // Empty constructor required for the HUpdateRule template.
- HMultiplicativeDivergenceRule() { }
/**
- * The update function that actually updates the H matrix. The function takes
- * in all the matrices and only changes the value of the H matrix.
+ * The update rule for the encoding matrix H. The formula used is
+ * \f[
+ * H_{a\mu} \leftarrow H_{a\mu} \frac{\sum_{i} W_{ia} V_{i\mu}/(WH)_{i\mu}}
+ * {\sum_{k} H_{ka}}
+ * \f]
+ * The function takes in all the matrices and only changes the value
+ * of the H matrix.
*
* @param V Input matrix to be factorized.
* @param W Basis matrix.
* @param H Encoding matrix to updated.
*/
template<typename MatType>
- inline static void Update(const MatType& V,
+ inline static void HUpdate(const MatType& V,
const arma::mat& W,
arma::mat& H)
{
@@ -117,22 +101,16 @@
t2.set_size(W.n_rows);
for (size_t k = 0; k < t2.n_elem; ++k)
{
- // This may produce NaNs if V(i, k) = 0.
- // Technically the math in the paper does not define what to do in
- // this case, but considering the basic intent of the update rules,
- // we'll make this modification and take t2(k) = 0.0.
t2(k) = W(k, i) * V(k, j) / t1(k, j);
- if (t2(k) != t2(k))
- t2(k) = 0.0;
}
- H(i, j) *= sum(t2) / sum(W.col(i));
+ H(i,j) = H(i,j) * sum(t2) / sum(W.col(i));
}
}
}
};
-}; // namespace nmf
+}; // namespace amf
}; // namespace mlpack
#endif
Modified: mlpack/trunk/src/mlpack/methods/cf/cf.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/cf/cf.hpp (original)
+++ mlpack/trunk/src/mlpack/methods/cf/cf.hpp Tue May 27 19:49:17 2014
@@ -12,8 +12,8 @@
#include <mlpack/core.hpp>
#include <mlpack/methods/neighbor_search/neighbor_search.hpp>
-#include <mlpack/methods/lmf/lmf.hpp>
-#include <mlpack/methods/lmf/update_rules/nmf_als.hpp>
+#include <mlpack/methods/amf/amf.hpp>
+#include <mlpack/methods/amf/update_rules/nmf_als.hpp>
#include <set>
#include <map>
#include <iostream>
@@ -54,8 +54,8 @@
* Apply(arma::sp_mat& data, size_t rank, arma::mat& W, arma::mat& H).
*/
template<
- typename FactorizerType = lmf::LMF<lmf::RandomInitialization,
- lmf::NMF_ALSUpdate> >
+ typename FactorizerType = amf::AMF<amf::RandomInitialization,
+ amf::NMFALSUpdate> >
class CF
{
public:
Modified: mlpack/trunk/src/mlpack/methods/nmf/CMakeLists.txt
==============================================================================
--- mlpack/trunk/src/mlpack/methods/nmf/CMakeLists.txt (original)
+++ mlpack/trunk/src/mlpack/methods/nmf/CMakeLists.txt Tue May 27 19:49:17 2014
@@ -1,24 +1,3 @@
-# Define the files we need to compile
-# Anything not in this list will not be compiled into MLPACK.
-set(SOURCES
- mult_dist_update_rules.hpp
- mult_div_update_rules.hpp
- als_update_rules.hpp
- random_init.hpp
- random_acol_init.hpp
- nmf.hpp
- nmf_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(nmf
nmf_main.cpp
)
Modified: mlpack/trunk/src/mlpack/methods/nmf/nmf_main.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/nmf/nmf_main.cpp (original)
+++ mlpack/trunk/src/mlpack/methods/nmf/nmf_main.cpp Tue May 27 19:49:17 2014
@@ -6,15 +6,15 @@
*/
#include <mlpack/core.hpp>
-#include "nmf.hpp"
+#include <mlpack/methods/amf/amf.hpp>
-#include "random_init.hpp"
-#include "mult_dist_update_rules.hpp"
-#include "mult_div_update_rules.hpp"
-#include "als_update_rules.hpp"
+#include <mlpack/methods/amf/init_rules/random_init.hpp>
+#include <mlpack/methods/amf/update_rules/nmf_mult_dist.hpp>
+#include <mlpack/methods/amf/update_rules/nmf_mult_div.hpp>
+#include <mlpack/methods/amf/update_rules/nmf_als.hpp>
using namespace mlpack;
-using namespace mlpack::nmf;
+using namespace mlpack::amf;
using namespace std;
// Document program.
@@ -104,26 +104,22 @@
{
Log::Info << "Performing NMF with multiplicative distance-based update "
<< "rules." << std::endl;
- NMF<> nmf(maxIterations, minResidue);
- nmf.Apply(V, r, W, H);
+ AMF<> amf(maxIterations, minResidue);
+ amf.Apply(V, r, W, H);
}
else if (updateRules == "multdiv")
{
Log::Info << "Performing NMF with multiplicative divergence-based update "
<< "rules." << std::endl;
- NMF<RandomInitialization,
- WMultiplicativeDivergenceRule,
- HMultiplicativeDivergenceRule> nmf(maxIterations, minResidue);
- nmf.Apply(V, r, W, H);
+ AMF<RandomInitialization, NMFMultiplicativeDivergenceUpdate> amf(maxIterations, minResidue);
+ amf.Apply(V, r, W, H);
}
else if (updateRules == "als")
{
Log::Info << "Performing NMF with alternating least squared update rules."
<< std::endl;
- NMF<RandomInitialization,
- WAlternatingLeastSquaresRule,
- HAlternatingLeastSquaresRule> nmf(maxIterations, minResidue);
- nmf.Apply(V, r, W, H);
+ AMF<RandomInitialization, NMFALSUpdate> amf(maxIterations, minResidue);
+ amf.Apply(V, r, W, H);
}
// Save results.
Modified: mlpack/trunk/src/mlpack/tests/nmf_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/nmf_test.cpp (original)
+++ mlpack/trunk/src/mlpack/tests/nmf_test.cpp Tue May 27 19:49:17 2014
@@ -5,10 +5,10 @@
* Test file for NMF class.
*/
#include <mlpack/core.hpp>
-#include <mlpack/methods/nmf/nmf.hpp>
-#include <mlpack/methods/nmf/random_acol_init.hpp>
-#include <mlpack/methods/nmf/mult_div_update_rules.hpp>
-#include <mlpack/methods/nmf/als_update_rules.hpp>
+#include <mlpack/methods/amf/amf.hpp>
+#include <mlpack/methods/amf/init_rules/random_acol_init.hpp>
+#include <mlpack/methods/amf/update_rules/nmf_mult_div.hpp>
+#include <mlpack/methods/amf/update_rules/nmf_als.hpp>
#include <boost/test/unit_test.hpp>
#include "old_boost_test_definitions.hpp"
@@ -18,7 +18,7 @@
using namespace std;
using namespace arma;
using namespace mlpack;
-using namespace mlpack::nmf;
+using namespace mlpack::amf;
/**
* Check the if the product of the calculated factorization is close to the
@@ -31,7 +31,7 @@
mat v = w * h;
size_t r = 16;
- NMF<> nmf;
+ AMF<> nmf;
nmf.Apply(v, r, w, h);
mat wh = w * h;
@@ -52,7 +52,7 @@
mat v = w * h;
size_t r = 16;
- NMF<RandomAcolInitialization<> > nmf;
+ AMF<RandomAcolInitialization<> > nmf;
nmf.Apply(v, r, w, h);
mat wh = w * h;
@@ -73,9 +73,7 @@
mat v = w * h;
size_t r = 16;
- NMF<RandomInitialization,
- WMultiplicativeDivergenceRule,
- HMultiplicativeDivergenceRule> nmf;
+ AMF<RandomInitialization, NMFMultiplicativeDivergenceUpdate> nmf;
nmf.Apply(v, r, w, h);
mat wh = w * h;
@@ -97,9 +95,7 @@
mat v = w * h;
size_t r = 16;
- NMF<RandomInitialization,
- WAlternatingLeastSquaresRule,
- HAlternatingLeastSquaresRule> nmf(50000, 1e-15);
+ AMF<RandomInitialization, NMFALSUpdate> nmf(50000, 1e-15);
nmf.Apply(v, r, w, h);
const mat wh = w * h;
@@ -125,7 +121,7 @@
size_t r = 18;
// It seems to hit the iteration limit first.
- NMF<> nmf(10000, 1e-20);
+ AMF<> nmf(10000, 1e-20);
mlpack::math::RandomSeed(1000); // Set random seed so results are the same.
nmf.Apply(v, r, w, h);
mlpack::math::RandomSeed(1000);
@@ -163,7 +159,7 @@
mat dw, dh;
size_t r = 16;
- NMF<RandomAcolInitialization<> > nmf;
+ AMF<RandomAcolInitialization<> > nmf;
mlpack::math::RandomSeed(1000); // Set random seed so results are the same.
nmf.Apply(v, r, w, h);
mlpack::math::RandomSeed(1000);
@@ -201,9 +197,7 @@
mat dw, dh;
size_t r = 16;
- NMF<RandomInitialization,
- WMultiplicativeDivergenceRule,
- HMultiplicativeDivergenceRule> nmf;
+ AMF<RandomInitialization, NMFMultiplicativeDivergenceUpdate> nmf;
mlpack::math::RandomSeed(10); // Set random seed so the results are the same.
nmf.Apply(v, r, w, h);
mlpack::math::RandomSeed(10);
@@ -241,9 +235,7 @@
mat dw, dh;
size_t r = 8;
- NMF<RandomInitialization,
- WAlternatingLeastSquaresRule,
- HAlternatingLeastSquaresRule> nmf;
+ AMF<RandomInitialization, NMFALSUpdate> nmf;
mlpack::math::RandomSeed(40);
nmf.Apply(v, r, w, h);
mlpack::math::RandomSeed(40);
Modified: mlpack/trunk/src/mlpack/tests/to_string_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/to_string_test.cpp (original)
+++ mlpack/trunk/src/mlpack/tests/to_string_test.cpp Tue May 27 19:49:17 2014
@@ -47,7 +47,7 @@
#include <mlpack/methods/logistic_regression/logistic_regression.hpp>
#include <mlpack/methods/lsh/lsh_search.hpp>
#include <mlpack/methods/neighbor_search/neighbor_search.hpp>
-#include <mlpack/methods/nmf/nmf.hpp>
+#include <mlpack/methods/amf/amf.hpp>
#include <mlpack/methods/nca/nca.hpp>
#include <mlpack/methods/pca/pca.hpp>
#include <mlpack/methods/radical/radical.hpp>
@@ -428,15 +428,17 @@
BOOST_REQUIRE_NE(s, "");
}
+/*
BOOST_AUTO_TEST_CASE(NMFString)
{
arma::mat c(40, 40);
c.randn();
- mlpack::nmf::NMF<> d;
+ mlpack::amf::AMF<> d;
Log::Debug << d;
std::string s = d.ToString();
BOOST_REQUIRE_NE(s, "");
}
+*/
BOOST_AUTO_TEST_CASE(NCAString)
{
More information about the mlpack-svn
mailing list